欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2019 ICPC Asia Nanjing Regional K. Triangle(计算几何+二分)

程序员文章站 2022-03-30 16:34:47
...

 把板子都抄上了所以代码特别长,其实只用到了几个函数,先判断给定的点p是不是在顶点上,如果是的是的话直接输出对边的中点,如果正好在某条边的中点上的话,同理。

另外情况需要二分,如果点在ab上且在靠近a的地方,那么另一个点在bc上,否则在ac上,其余边上同理,然后二分找就可以了,叉积算三角形面积。

// #pragma GCC optimize(2)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO                       \
	ios::sync_with_stdio(false); \
	// cout.tie(0);
#define lson(x) (x << 1)
#define rson(x) (x << 1 | 1)
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e6 + 10;
const int maxm = 1e7 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
// int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int cmp(double x)
{
	if (fabs(x) < eps)
		return 0;
	if (x > 0)
		return 1;
	return -1;
}
inline double sqr(double x)
{
	return x * x;
}
struct point
{
	double x, y;
	point() {}
	point(double a, double b) : x(a), y(b) {}
	friend point operator+(const point &a, const point &b)
	{
		return point(a.x + b.x, a.y + b.y);
	}
	friend point operator-(const point &a, const point &b)
	{
		return point(a.x - b.x, a.y - b.y);
	}
	friend bool operator==(const point &a, const point &b)
	{
		return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
	}
	friend point operator*(const point &a, const double &b)
	{
		return point(a.x * b, a.y * b);
	}
	friend point operator*(const double &a, const point &b)
	{
		return point(a * b.x, a * b.y);
	}
	friend point operator/(const point &a, const double &b)
	{
		return point(a.x / b, a.y / b);
	}
	double norm()
	{
		return sqrt(sqr(x) + sqr(y));
	}
} a, b, c, p;
double det(const point &a, const point &b)
{
	return a.x * b.y - a.y * b.x;
}
double dot(const point &a, const point &b)
{
	return a.x * b.x + a.y * b.y;
}
double dist(const point &a, const point &b)
{
	return (a - b).norm();
}
point rotate_point(const point &p, double A)
{
	double tx = p.x, ty = p.y;
	return point(tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A));
}
struct line
{
	point a, b;
	line() {}
	line(point x, point y) : a(x), b(y) {}
};
line point_make_line(const point a, const point b)
{
	return line(a, b);
}
double dis_point_segment(const point p, const point s, point t)
{
	if (cmp(dot(p - s, t - s)) < 0)
		return (p - s).norm();
	if (cmp(dot(p - t, s - t)) < 0)
		return (p - t).norm();
	return fabs(det(s - p, t - p) / dist(s, t));
}
void PointProjline(const point p, const point s, const point t, point &cp)
{
	double r = dot((t - s), (p - s)) / dot(t - s, t - s);
	cp = s + r * (t - s);
}
bool PointOnSegment(point p, point s, point t)
{
	return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t)) <= 0;
}
bool parallel(line a, line b)
{
	return !cmp(det(a.a - a.b, b.a - b.b));
}
bool line_make_point(line a, line b, point &res)
{
	if (parallel(a, b))
		return false;
	double s1 = det(a.a - b.a, b.b - b.a);
	double s2 = det(a.b - b.a, b.b - b.a);
	res = (s1 * a.b - s2 * a.a) / (s1 - s2);
	return true;
}
line move_d(line a, const double &len)
{
	point d = a.b - a.a;
	d = d / d.norm();
	d = rotate_point(d, pi / 2);
	return line(a.a + d * len, a.b + d * len);
}
double s;

point BinSear(point p, point L, point R)
{
	point pp = R;
	point mid;
	for (int i = 0; i < 100; i++)
	{
		mid = (L + R) / 2.0;
		double area = fabs(det(pp - mid, pp - p));
		if (cmp(area - s) > 0)
			L = mid;
		else if (cmp(area - s) < 0)
			R = mid;
		else
			break;
	}
	return mid;
}
int main()
{
#ifdef WXY
	freopen("in.txt", "r", stdin);
	//  freopen("out.txt", "w", stdout);
#endif
	// IO;
	int T;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &p.x, &p.y);
		s = abs(det(a - c, a - b) * 0.5);
		if (p == a)
		{
			point t = (b + c) / 2;
			printf("%.8lf %.8lf\n", t.x, t.y);
			continue;
		}
		if (p == b)
		{
			point t = (a + c) / 2;
			printf("%.8lf %.8lf\n", t.x, t.y);
			continue;
		}
		if (p == c)
		{
			point t = (b + a) / 2;
			printf("%.8lf %.8lf\n", t.x, t.y);
			continue;
		}
		if (PointOnSegment(p, a, b))
		{
			point t = (a + b) / 2;
			if (p == t)
			{
				printf("%.8lf %.8lf\n", c.x, c.y);
				continue;
			}
			if (PointOnSegment(p, a, t))
			{
				point ans = BinSear(p, c, b);
				printf("%.8lf %.8lf\n", ans.x, ans.y);
			}
			else
			{
				point ans = BinSear(p, c, a);
				printf("%.8lf %.8lf\n", ans.x, ans.y);
			}
			continue;
		}
		else if (PointOnSegment(p, b, c))
		{
			point t = (b + c) / 2;
			if (p == t)
			{
				printf("%.8lf %.8lf\n", a.x, a.y);
				continue;
			}
			if (PointOnSegment(p, b, t))
			{
				point ans = BinSear(p, a, c);
				printf("%.8lf %.8lf\n", ans.x, ans.y);
			}
			else
			{
				point ans = BinSear(p, a, b);
				printf("%.8lf %.8lf\n", ans.x, ans.y);
			}
			continue;
		}
		else if (PointOnSegment(p, a, c))
		{
			point t = (a + c) / 2;
			if (p == t)
			{
				printf("%.8lf %.8lf\n", b.x, b.y);
				continue;
			}
			if (PointOnSegment(p, a, t))
			{
				point ans = BinSear(p, b, c);
				printf("%.8lf %.8lf\n", ans.x, ans.y);
			}
			else
			{
				point ans = BinSear(p, b, a);
				printf("%.8lf %.8lf\n", ans.x, ans.y);
			}
			continue;
		}
		else
		{
			printf("-1\n");
		}
	}
	return 0;
}