本文主要是介绍poj 1269 Intersecting Lines(计算几何:线段相交),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
给出两条线段,问对应哪三种情况:
不相交,重合,相交于一点
代码如下:
/* ***********************************************Author :yinhuaEmail :yinwoods@163.comFile Name :poj1269.cppCreated Time :2014年12月02日 星期二 16时49分10秒************************************************ */#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 1010
#define eps 1e-6
#define LL long long
using namespace std;double ansx, ansy;struct Point {double x, y;Point() {}Point(double _x, double _y) {x = _x, y = _y;}Point operator -(const Point &b) const {return Point(x-b.x, y-b.y);}double operator *(const Point &b) const {return x*b.x+y*b.y;}double operator ^(const Point &b) const {return x*b.y-y*b.x;}
};int sgn(double x) {if(fabs(x) < eps) return 0;if(x < 0) return -1;return 1;
}struct Line {Point s, e;Line() {}Line(Point _s, Point _e) {s = _s, e = _e;}pair<Point, int> operator &(const Line &b) const {Point res = s;if(sgn((s-e)^(b.s-b.e)) == 0) {if(sgn((b.s-s)^(b.e-s)) == 0)return make_pair(res, 0);else return make_pair(res, 1);}double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));res.x += (e.x-s.x)*t;res.y += (e.y-s.y)*t;return make_pair(res, 2);}
};int main() {int n;while(~scanf("%d", &n)) {puts("INTERSECTING LINES OUTPUT");Line l1, l2;while(n--) {scanf("%lf%lf%lf%lf", &l1.s.x, &l1.s.y, &l1.e.x, &l1.e.y);scanf("%lf%lf%lf%lf", &l2.s.x, &l2.s.y, &l2.e.x, &l2.e.y);pair<Point, int> ans = l1 & l2;if(ans.second == 2) printf("POINT %.2lf %.2lf\n", ans.first.x, ans.first.y);else if(ans.second == 0) puts("LINE");else puts("NONE");}puts("END OF OUTPUT");}return 0;
}
这篇关于poj 1269 Intersecting Lines(计算几何:线段相交)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!