牛客竞赛计算几何专题班二维基础
作者:互联网
D.Operation Love
题意:
在二维坐标图给出一个手掌状图形,判断该图形是左手还是右手。
题解:
枚举所有线段,通过线段长度找到A,B,C三个点,若 B C ⃗ \vec{BC} BC 在 A B ⃗ \vec{AB} AB 的逆时针方向为左手,否则为右手。
代码:
t B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator ==(const Point& a, const Point& b) { return !sgn(a.x - b.x) && !sgn(a.y - b.y); }
//点积,可用于判断角度
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
//叉积
double Cross(Vector A, Vector B) { return A.x * B.y - B.x * A.y; }
//模长
double Length(Vector A) { return sqrt(Dot(A, A)); }
//判断 bc 是不是在 ab 的逆时针方向,向量夹角<90
bool ToLeftTest(Point a, Point b, Point c) { return Cross(b - a, c - b) > 0; }
int T;
int main() {
ios::sync_with_stdio(0);
cin >> T;
while (T--) {
Point p[30], P1, P2, P3, P4;
for (int i = 1; i <= 20; i++)cin >> p[i].x >> p[i].y;
p[21] = p[1], p[22] = p[2];
p[0] = p[20];
for (int i = 1; i <= 20; i++) {
double L = Length(p[i + 1] - p[i]);
if (L - 8.5 > eps) {//找到线段AB
// P1 = p[i], P2 = p[i + 1];
double L1 = Length(p[i - 1] - p[i]), L2 = Length(p[i + 2] - p[i + 1]);
//根据长度区分C点(P3)
if (L2 - L1 > eps)P1 = p[i], P2 = p[i + 1], P3 = p[i - 1];
if (L1 - L2 > eps)P1 = p[i + 1], P2 = p[i], P3 = p[i + 2];
break;
}
}
if (!ToLeftTest(P2, P1, P3))cout << "right\n";
else cout << "left\n";
}
return 0;
}
标签:P2,P3,专题,return,P1,double,牛客,二维,Vector 来源: https://blog.csdn.net/choice__/article/details/122464931