POJ-1269-Intersecting Lines
题意
给你一对直线,判断两条位置关系
做法
先用叉积判断两条直线是否平行,如果不平行则是相交,平行的话判一下一条直线上的点是否在另一条直线上,若在则是重合,不在则是平行,。
代码
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;const double eps = 1e-12;
const double inf = 1e20;
const double pi = acos(-1.0);
const int maxp = 1010;
int sgn( 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 _x, double _y) {
x = _x, y = _y;}void input() {
scanf("%Lf%Lf", &x, &y);}void output() {
cout << x << " " << y << endl; }bool operator == (Point b) const {
return sgn(x - b.x) == 0 && sgn(y - b.y) == 0;}bool operator < (Point b) const {
return sgn(x - b.x) == 0 ? sgn(y - b.y) < 0 : x < b.x;}Point operator - (const Point &b) const {
return Point(x - b.x, y - b.y);}double operator ^ (const Point &b) const {
return x * b.y - y * b.x;}double operator * (const Point & b) const {
return x * b.x + y * b.y;}double len() {
return hypot(x, y);}double len2() {
return x * x + y * y;}double distance(Point p) {
return hypot(x - p.x, y - p.y);}Point operator + (const Point &b) const {
return Point(x + b.x, y + b.y);}Point operator * (const double &k) const {
return Point(x * k, y * k);}Point operator / (const double &k) const {
return Point (x / k, y / k);}double rad(Point a, Point b) {
Point p = *this;return fabs(atan2( fabs((a - p) ^ (b - p)), (a - p) * (b - p) ));}Point trunc(double r) {
double l = len();if (!sgn(l)) return *this;r /= l;return Point(x * r, y * r);}
};
struct Line {
Point s, e;Line() {
}Line(Point _s, Point _e) {
s = _s, e = _e;}bool operator == (Line v) {
return (s == v.s) && (e == v.e);}Line (Point p, double angle) {
s = p;if (sgn(angle - pi / 2) == 0)e = (s + Point (0, 1));elsee = (s + Point (1, tan(angle)));}Line(double a, double b, double c) {
if (sgn(a) == 0) {
s = Point(0, -c / b);e = Point(1, -c / b);}else if (sgn(b) == 0) {
s = Point(-c / a, 0);e = Point(-c / a, 1);}else {
s = Point(0, -c / b);e = Point(1, (-c-a) / b);}}Point getV() {
return e-s;}void input() {
s.input(); e.input();}void adjust() {
if (e < s) swap(s, e);}double length() {
return s.distance(e);}double angle() {
double k = atan2(e.y - s.y, e.x - s.x);if (sgn(k) < 0) k += pi;if (sgn(k - pi) == 0) k -= pi;return k;}int relation(Point p) {
int c = sgn( (p - s) ^ (e - s) );if (c < 0) return 1;else if (c > 0) return 2;return 3;}bool pointonseg(Point p) {
return sgn((p - s) ^ (e - s)) == 0 && sgn((p - s) * (p - e)) <= 0;}bool parallel(Line v) {
return sgn((e - s) ^ (v.e - v.s)) == 0;}Point lineprog(Point p) {
return s + ( ((e - s) * ((e - s) * (p - s))) / ((e - s).len2()) );}Point symmetrypoint(Point p) {
Point q = lineprog(p);return Point(2.0 * q.x - p.x, 2.0 * q.y - p.y);}double dispointtoline(Point p) {
return fabs((p - s) ^ (e - s)) / length();}double dispointtoseg(Point p) {
if (sgn((p-s)*(e-s))<0 || sgn((p-e)*(s-e))<0)return min(p.distance(s), p.distance(e));return dispointtoline(p);}int linecrossline(Line v){
if((*this).parallel(v)) return v.relation(s)==3;return 2;}Point crosspoint(Line v){
double a1=(v.e-v.s)^(s-v.s);double a2=(v.e-v.s)^(e-v.s);return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));}
};
int main()
{
int n;double xa,xb,ya,yb,xc,yc,xd,yd;while(scanf("%d",&n)!=EOF){
printf("INTERSECTING LINES OUTPUT\n");while(n--){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&xa,&ya,&xb,&yb,&xc,&yc,&xd,&yd);Line L1(Point(xa,ya),Point(xb,yb));Line L2(Point(xc,yc),Point(xd,yd));int tmp=L1.linecrossline(L2);if(tmp==0) printf("NONE\n");else if(tmp==1) printf("LINE\n");else{
Point ans=L1.crosspoint(L2);printf("POINT %.2f %.2f\n",ans.x,ans.y);}}printf("END OF OUTPUT\n");}
}