P17709 [ICPC 2019 Nanjing R] Triangle 题解
题意
给出一个三角形的三个顶点和一个点 。是否存在一个在三角形边上的点 使得线段 把三角形分成面积相等的两部分。
题解
首先点 应当也落在三角形边上。如果没有落在三角形边上输出 -1。这一步用向量叉积是好求的。
方法 1
考虑 可能出现在哪些边上,然后它分割出来的一部分的面积显然是有单调性的,于是就可以对于它的位置进行二分。
由于我并没有这么写,所以没有代码(
方法 2
数学老师教过。
首先设点 落在的边的两个端点为 ,第三个点为 。设 。
- 若 , 应落在 上,且 ;
- 若 , 应落在 上,且 。
然后就做好了。
判断部分好像可以不用这样一堆 if。但是我只会这样写(悲
Code
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
namespace Lemon {
using ll = long long;
const double eps = 1e-8;
const double pi = acos(-1.0);
struct Point {
double x, y;
Point() {}
Point(double _x, double _y) : x(_x), y(_y) {}
Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }
Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }
Point operator*(double k) const { return Point(x * k, y * k); }
Point operator/(double k) const { return Point(x / k, y / k); }
bool operator<(const Point& p) const {
if (fabs(x - p.x) > eps) return x < p.x;
return y < p.y;
}
bool operator>(const Point& p) const {
if (fabs(x - p.x) > eps) return x > p.x;
return y > p.y;
}
bool operator==(const Point& p) const {
return fabs(x - p.x) < eps && fabs(y - p.y) < eps;
}
friend double dot(const Point& a, const Point& b) {
return a.x * b.x + a.y * b.y;
}
double operator*(const Point& p) const {
return x * p.x + y * p.y;
}
friend double cross(const Point& a, const Point& b) {
return a.x * b.y - a.y * b.x;
}
friend double cross(const Point& a, const Point& b, const Point& c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
double operator^(const Point& p) const {
return x * p.y - y * p.x;
}
friend double len(const Point& a) {
return __builtin_sqrtl(a.x * a.x + a.y * a.y);
}
double dis(const Point& a, const Point& b) {
return hypot(a.x - b.x, a.y - b.y);
}
Point unit(const Point& v) {
double l = len(v);
return Point(v.x / l, v.y / l);
}
Point rotate(const Point& v, double rad) {
double c = cos(rad), s = sin(rad);
return Point(v.x * c - v.y * s, v.x * s + v.y * c);
}
Point normal(const Point& v) {
return Point(-v.y, v.x);
}
};
int sgn(double x) {
if (fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
inline bool check(Point a, Point b, Point p) {
double d = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
if (fabs(d) > eps) return false;
return min(a.x, b.x) <= p.x && p.x <= max(a.x, b.x) && min(a.y, b.y) <= p.y && p.y <= max(a.y, b.y);
}
inline void solve() {
Point p1, p2, p3, q; cin >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y >> q.x >> q.y;
bool f1 = check(p1, p2, q), f2 = check(p1, p3, q), f3 = check(p2, p3, q);
if (!(f1 || f2 || f3)) return cout << "-1\n", void();
Point p;
if (f1) {
double t = hypot(q.x - p1.x, q.y - p1.y) / hypot(p2.x - p1.x, p2.y - p1.y);
if (t >= 0.5) {
double r = 1.0 / (2.0 * t);
p = p1 + (p3 - p1) * r;
} else {
double r = 1.0 / (2.0 * (1.0 - t));
p = p2 + (p3 - p2) * r;
}
} else if (f2) {
double t = hypot(q.x - p1.x, q.y - p1.y) / hypot(p3.x - p1.x, p3.y - p1.y);
if (t >= 0.5) {
double r = 1.0 / (2.0 * t);
p = p1 + (p2 - p1) * r;
} else {
double r = 1.0 / (2.0 * (1.0 - t));
p = p3 + (p2 - p3) * r;
}
} else {
double t = hypot(q.x - p2.x, q.y - p2.y) / hypot(p3.x - p2.x, p3.y - p2.y);
if (t >= 0.5) {
double r = 1.0 / (2.0 * t);
p = p2 + (p1 - p2) * r;
} else {
double r = 1.0 / (2.0 * (1.0 - t));
p = p3 + (p1 - p3) * r;
}
}
cout << fixed << setprecision(10) << p.x << " " << p.y << "\n";
}
}
signed main() {
IOS;
int t = 1;
cin >> t;
while (t--) Lemon::solve();
return 0;
}