# Poj 1269

http://acm.pku.edu.cn/JudgeOnline/problem?id=1269

(p1-p0)X(p2-p0)=0

(p3-p0)X(p2-p0)=0

(y1-y2)x0+(x2-x1)y0+x1y2-x2y1=0

(y3-y4)x0+(x4-x3)y0+x3y4-x4y3=0

x0,y0作为变量求解二元一次方程组。

a1x+b1y+c1=0;

a2x+b2y+c2=0

x=(c1*b2-c2*b1)/(a2*b1-a1*b2);

y=(a2*c1-a1*c2)/(a1*b2-a2*b1);

/* Wrote by Dream , poj 1269, double 的零值判断不能直接写==0*/

#include <string>

#include <iostream>

#include <algorithm>

#include <cmath>

using namespace std;

struct Point

{

double x;

double y;

};

Point p1,p2,p3,p4;

const double ERR = 0.000001;

void DealData();

double Direction(Point p1, Point p2, Point p3);

int main()

{

//freopen("input.txt","r",stdin);

int n = 0;

scanf("%d", &n);

printf("INTERSECTING LINES OUTPUT/n");

for (int i = 0; i < n; ++i)

{

scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &p1.x, &p1.y, &p2.x, &p2.y, &p3.x, &p3.y, &p4.x, &p4.y);

DealData();

}

printf("END OF OUTPUT/n");

return 0;

}

/* p1p2 与 p1p3的叉乘 */

double Direction(Point p1, Point p2, Point p3)

{

return (p2.x - p1.x)*(p3.y - p1.y) - (p3.x - p1.x)*(p2.y - p1.y);

}

void DealData()

{

if (fabs(Direction(p1,p2,p3)) <= ERR && fabs(Direction(p1,p2,p4)) <= ERR)

{

printf("LINE/n");

}

else if ((p2.x - p1.x) * (p4.y - p3.y) == (p4.x - p3.x) * (p2.y - p1.y))

{

printf("NONE/n");

}

else

{

double a1 = p1.y - p2.y;

double b1 = p2.x - p1.x;

double c1 = p1.x*p2.y - p2.x*p1.y;

double a2 = p3.y - p4.y;

double b2 = p4.x - p3.x;

double c2 = p3.x*p4.y - p4.x*p3.y;

double x = (b1*c2 - b2*c1)/(a1*b2 - a2*b1);

double y = (a2*c1 - a1*c2)/(a1*b2 - a2*b1);

printf("POINT %.2lf %.2lf/n", x, y);

}

}

• 本文已收录于以下专栏：

举报原因： 您举报文章：Poj 1269 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)