三分
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
using namespace std;
double lz, lr, Lx, Ly, Rx, Ry, ans1, mid1, mid2, mid3, mid4, midx, midy, ans2, x1, x2, x3, p, q, y_1, y2, y3, l1, l2, l3;
double ax, ay;
int f1;
double accu(double x, double y, double zx, double zy)
{
double s, k;
double l1, l2, l3, x0, y0;
l1 = sqrt((x - zx) * (x - zx) + (y - zy) * (y - zy));
l2 = sqrt((x - p) * (x - p) + (y - q) * (y - q));
l3 = sqrt((p - zx) * (p - zx) + (q - zy) * (q - zy));
s = l1 + l2 + l3;
k = s / 2;
k = sqrt(k * (k - l1) * (k - l2) * (k - l3));
return k;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
double s, k, la, ly, zx, zy;
int i, j, n, m, t, flag = 0, flag1 = 0, flag2 = 0, flag3 = 0;
scanf("%d", &t);
for (int kcase = 1; kcase <= t; kcase++)
{
flag1 = 0, flag2 = 0, flag3 = 0,flag=0;
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x1, &y_1, &x2, &y2, &x3, &y3, &p, &q);
if (p > x1 && p > x2 && p > x3 || (q > y_1 && q > y2 && q > y3) || (q < y_1 && q < y2 && q < y3) || p < x1 && p < x2 && p < x3)
{
printf("-1\n");
continue;
}
else
{
if(x1==x2&&((q>y_1&&q>y2)||((q<y_1&&q<y2))))
{
printf("-1\n");
continue;
}
else if(x1==x3&&((q>y_1&&q>y3)||q<y_1&&q<y3))
{
printf("-1\n");
continue;
}
else if(x2==x3&&((q>y2&&q>y3)||(q<y2&&q<y3)))
{
printf("-1\n");
continue;
}
else if(y_1==y2&&((p>x1&&p>x2)||((p<x1&&p<x2))))
{
printf("-1\n");
continue;
}
else if(y_1==y3&&((p>x1&&p>x3)||((p<x1&&p<x3))))
{
printf("-1\n");
continue;
}
else if(y2==y3&&((p>x2&&p>x3)||((p<x2&&p<x3))))
{
printf("-1");
continue;
}
}
if (((x1 - x2) * (q - y2)) == ((p - x2) * (y_1 - y2)))
{
flag1 = 1;
}
if (((x1 - x3) * (q - y3)) == ((p - x3) * (y_1 - y3)))
{
flag2 = 1;
}
if (((x2 - x3) * (q - y3)) == ((p - x3) * (y2 - y3)))
{
flag3 = 1;
}
if (flag1 != 1 && flag2 != 1 && flag3 != 1)
{
printf("-1\n");
continue;
}
if (flag1 == 1)
{
Lx = x1, Ly = y_1;
Rx = x2, Ry = y2;
midx = (Lx + Rx) / 2;
midy = (Ly + Ry) / 2;
if (p <= midx && q <= midy)
{
if (x1 <= midx && y_1 <= midy)
{
zx = x2, zy = y2;
}
else
{
zx = x1, zy = y_1;
}
}
else
{
if (x1 >= midx && y_1 >= midy)
{
zx = x2, zy = y2;
}
else
{
zx = x1, zy = y_1;
}
}
}
else if (flag2 == 1)
{
Lx = x1, Ly = y_1;
Rx = x3, Ry = y3;
midx = (Lx + Rx) / 2;
midy = (Ly + Ry) / 2;
if (p <= midx && q <= midy)
{
if (x1 <= midx && y_1 <= midy)
{
zx = x3, zy = y3;
}
else
{
zx = x1, zy = y_1;
}
}
else
{
if (x1 >= midx && y_1 >= midy)
{
zx = x3, zy = y3;
}
else
{
zx = x1, zy = y_1;
}
}
}
else if (flag3 == 1)
{
Lx = x2, Ly = y2;
Rx = x3, Ry = y3;
midx = (Lx + Rx) / 2;
midy = (Ly + Ry) / 2;
if (p <= midx && q <= midy)
{
if (x2 <= midx && y2 <= midy)
{
zx = x3, zy = y3;
}
else
{
zx = x2, zy = y2;
}
}
else
{
if (x2 >= midx && y2 >= midy)
{
zx = x3, zy = y3;
}
else
{
zx = x2, zy = y2;
}
}
}
l1 = sqrt((x1 - x2) * (x1 - x2) + (y_1 - y2) * (y_1 - y2));
l2 = sqrt((x1 - x3) * (x1 - x3) + (y_1 - y3) * (y_1 - y3));
l3 = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
s = l1 + l2 + l3;
k = s / 2;
k = sqrt(k * (k - l1) * (k - l2) * (k - l3));
k = k / 2;
for (int nm = 1; nm <= 3; nm++)
{
if (nm == 1)
{
Lx = x1, Ly = y_1;
Rx = x2, Ry = y2;
flag = ((x1 - x2) * (q - y2)) == ((p - x2) * (y_1 - y2));
}
if (nm == 2)
{
Lx = x1, Ly = y_1;
Rx = x3, Ry = y3;
flag = ((x1 - x3) * (q - y3)) == ((p - x3) * (y_1 - y3));
}
if (nm == 3)
{
Lx = x2, Ly = y2;
Rx = x3, Ry = y3;
flag = ((x2 - x3) * (q - y3)) == ((p - x3) * (y2 - y3));
}
if (!flag&&((Lx==zx&&Ly==zy)||(Rx==zx&&Ry==zy)))
{
for (i = 0; i < 100; i++)
{
midx = (Lx + Rx) / 2;
midy = (Ly + Ry) / 2;
mid1 = (Lx + midx) / 2;
mid2 = (Ly + midy) / 2;
mid3 = (Rx + midx) / 2;
mid4 = (Ry + midy) / 2;
ans1 = accu(mid1, mid2, zx, zy);
ans2 = accu(mid3, mid4, zx, zy);
if (fabs(ans1 - k) > fabs(ans2 - k))
{
Lx = mid1, Ly = mid2;
ax=Lx,ay=Ly;
}
else
{
Rx = mid3, Ry = mid4;
ax=Rx,ay=Ry;
}
}
}
}
printf("%.12lf %.12lf\n", ax, ay);
}
return 0;
}
叉积(队友代码)
#include<bits/stdc++.h>
#define eps 1e-8
#define zero(x) (!(((x)>0?(x):-(x))<eps)) // x = 0 返回 false
using namespace std;
struct point
{
double x, y;
point() {} point(double x, double y): x(x), y(y) {}
};
double cross(point a, point b, point c)
{
return fabs((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x));
}
void solve(point a, point b, point c, point d, double S)
{
double k = S / cross(b, d, c); // S = cross(kcb * db)
printf("%.12f %.12f\n", (c.x - b.x)*k + b.x, (c.y - b.y)*k + b.y);
}
int main()
{
int T;
scanf("%d", &T);
while (T --) {
point a, b, c, d;
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &d.x, &d.y);
double S = cross(a, b, c), s1 = cross(d, a, b), s2 = cross(d, b, c), s3 = cross(d, c, a);
if ((zero(S - s1 - s2 - s3)) || zero(s1) && zero(s2) && zero(s3)) printf("-1\n");
else if (!zero(s1)) {
if (s2 > s3) solve(a, b, c, d, S / 2);
else solve(b, a, c, d, S / 2);
}
else if (!zero(s2)) {
if (s1 > s3) solve(c, b, a, d, S / 2);
else solve(b, c, a, d, S / 2);
}
else if (!zero(s3)) {
if (s1 > s2) solve(c, a, b, d, S / 2);
else solve(a, c, b, d, S / 2);
}
}
return 0;
}