【计算几何入门】poj 1269

来源:互联网 发布:石川智晶 知乎 编辑:程序博客网 时间:2024/06/06 01:14

嗯终于决定开始补计算几何了

从最基础的题目开始做

这题意思就是判断线段的三种位置关系没什么可说的

因为输出double用了 lf 调试了很长时间。。


哦对有个问题,那就是这个题目好像没有考虑两个线段首尾相连且斜率相同的情况?


本题的收获:计算几何因为对精度的要求很高,所以不能用解析几何的办法求斜率什么的 ,必须化除法为乘法,这样才能保证精度。

其次,能用叉乘的地方尽量用叉积,真的是非常好用的运算!


#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#define rep(i, j, k) for(int i = j; i <= k; i++)#define vector point#define eps 1e-8using namespace std;struct point{double x, y;point (double x = 0, double y = 0) : x (x), y (y) {}};struct line{point a, b;};vector operator + (point a, point b){return vector (a.x + b.x, a.y + b.y);}vector operator - (point a, point b){return vector (a.x - b.x, a.y - b.y);}vector operator * (point a, double b){return vector (a.x * b, a.y * b);}vector operator / (point a, double b){return vector (a.x / b, a.y / b);}bool operator == (point a, point b){return fabs (a.x - b.x) < eps && fabs (a.y - b.y) < eps;}double length (vector x){return sqrt (x.x * x.x + x.y * x.y);}double cross (vector a, vector b){//printf ("cross %.2lf\n", a.x * b.y - a.y * b.x);return a.x * b.y - a.y * b.x;}point GetLineIntersection (point p, vector v, point q, vector w){vector u = p - q;double t = cross (w, u) / cross (v, w);//print (p), print (v); printf ("length ------------%.2lf\n", t); print (p + v * t);return p + v * t;}void print (point x){printf ("%.2lf %.2lf\n", x.x, x.y);}int main(){int Time;cout << "INTERSECTING LINES OUTPUT" << endl;cin >> Time;while (Time--){line u, v;scanf ("%lf%lf%lf%lf%lf%lf%lf%lf", &u.a.x ,&u.a.y, &u.b.x, &u.b.y, &v.a.x, &v.a.y, &v.b.x, &v.b.y);if (fabs (cross (u.b - u.a, v.b - v.a)) < eps){if (fabs (cross (u.b - u.a, v.a - u.a)) < eps)printf ("LINE\n");elseprintf ("NONE\n");continue;}point p = u.a, q = v.a;vector vv = u.b - u.a, w = v.b - v.a;vv = vv / length (vv);w = w / length (w);point ans = GetLineIntersection (p, vv, q, w);printf ("POINT %.2f %.2f\n", ans.x, ans.y);}printf ("END OF OUTPUT\n");return 0;}


0 0
原创粉丝点击