Intersecting Lines

来源:互联网 发布:网络约租车管理办法 编辑:程序博客网 时间:2024/05/16 07:35

源自POJ1269

Description

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect. 
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000. 

Input

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

Output

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".

Sample Input

50 0 4 4 0 4 4 05 0 7 6 1 0 2 35 0 7 6 3 -6 4 -32 0 2 27 1 5 18 50 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUTPOINT 2.00 2.00NONELINEPOINT 2.00 5.00POINT 1.07 2.20END OF OUTPUT

分析:判断先后顺序-是否共线->是否平行->相交

CODE:

#include<stdio.h>#include<math.h>const double ERR = 0.000001;struct point{    double x;    double y;};void get_ans(point a[]){    if((fabs((a[1].x-a[0].x)*(a[2].y-a[1].y)-(a[1].y-a[0].y)*(a[2].x-a[1].x))<=ERR)&&(fabs((a[1].x-a[0].x)*(a[3].y-a[1].y)-(a[1].y-a[0].y)*(a[3].x-a[1].x))<=ERR))//共线        printf("LINE\n");    else if(((a[0].x-a[1].x)*(a[2].y-a[3].y)-(a[0].y-a[1].y)*(a[2].x-a[3].x))==0)//平行        printf("NONE\n");    else//相交    {        double a1=a[0].y-a[1].y,b1=a[1].x-a[0].x,c1=a[0].x*a[1].y-a[0].y*a[1].x;        double a2=a[2].y-a[3].y,b2=a[3].x-a[2].x,c2=a[2].x*a[3].y-a[2].y*a[3].x;        printf("POINT %.2lf %.2lf\n",(c1*b2-c2*b1)/(a2*b1-a1*b2),(a2*c1-a1*c2)/(a1*b2-a2*b1));    }}int main(){    int n;    scanf("%d",&n);    printf("INTERSECTING LINES OUTPUT\n");    while(n--)    {        point a[4];        for(int i=0;i<4;i++)            scanf("%lf %lf",&a[i].x,&a[i].y);        get_ans(a);    }    printf("END OF OUTPUT\n");    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 四岁宝宝不会写字怎么办 4岁宝宝不写字怎么办 四岁宝宝不写字怎么办 孩子学习粗心计算能力差怎么办 一年级的小朋友不爱看书怎么办 马上要生了害怕怎么办 孩子做题不爱读题怎么办 孩子作业写的慢怎么办 孩子学习不好怎么办我们有绝招 英语不会做题怎么办呢? 小学二年级孩子厌学怎么办 狗狗拉肚子不吃东西怎么办 小孩做作业时容易发呆怎么办 一上高速就犯困怎么办 孩子初中数学学不好怎么办 高三注意力不集中怎么办 考砸了家长打我怎么办? 高三学生困疲劳怎么办 高三晚上很困怎么办 孩子上高三压力大不想上学怎么办 高三的孩子压力大怎么办 高三复读压力大怎么办 孩子一年级做数学粗心怎么办 一年级的孩子数学总粗心怎么办 天生手脚笨的人怎么办 高三的孩子厌学怎么办 二年级小孩学习笨怎么办 孩子高二不想上怎么办 高三孩子玩手机怎么办 孩子考试粗心丢题怎么办 工作中总出错是怎么办 工作上做错事了怎么办 惹她不开心了怎么办 惹到别人不开心怎么办 孩子高三不愿意写作业怎么办? 小孩特别懒不爱学习怎么办 小孩上三年级不爱学习怎么办 一年级孩子做题粗心怎么办 一年级的孩子做题粗心怎么办 一年级娃娃做题粗心怎么办 有同学抄作业该怎么办