• POJ 1269 Intersecting Lines


                                                                   Intersecting Lines
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 10942   Accepted: 4917

    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

    5
    0 0 4 4 0 4 4 0
    5 0 7 6 1 0 2 3
    5 0 7 6 3 -6 4 -3
    2 0 2 27 1 5 18 5
    0 3 4 0 1 2 2 5
    

    Sample Output

    INTERSECTING LINES OUTPUT
    POINT 2.00 2.00
    NONE
    LINE
    POINT 2.00 5.00
    POINT 1.07 2.20
    END OF OUTPUT

    算法分析:
    注意此题是基于 直线 运行的测试的!
    每组数据包括4个点,也就是两条直线,请输出这两条直线的关系。
    1)相交的话 输出交点(保留2位小数);
    2) 平行不相交 输出: NONE ;
    3) 平行重合 输出: LINE

    利用数学向量的叉积 判断:两条直线是否相交
    (1)若不相交:利用 叉积判断 3点是否共线, 若共线则 重合; 否则 平行不相交
    (2)若相交 :利用如下公式计算交点
    l             设有直线AB和CD,求交点P :
                  注意 AC  CD  AB  CD  均为向量
                (1)令t = ( AC × CD )/( AB× CD )
                  (2)P.x = A.x - (A.x-B.x)*t;

                       P.y = A.y - (A.y-B.y)*t;

    Accepted:
    #include <stdio.h>
    #include <string.h>
    
    struct N
    {
    	double x, y;
    }s[10], p ;
    
    
    int main()
    {
    	int n;
    	int i;
    	scanf("%d", &n);
    	printf("INTERSECTING LINES OUTPUT
    ");
        
    	for(i=0; i<n; i++)
    	{
    		scanf("%lf %lf %lf %lf", &s[0].x, &s[0].y, &s[1].x, &s[1].y );
            scanf("%lf %lf %lf %lf", &s[2].x, &s[2].y, &s[3].x, &s[3].y );
    
    		//向量法判断是否相交
    		//向量法求直线交点
    		double t, d, f;
    		double dd = ( (s[1].x-s[0].x)*(s[3].y-s[2].y) - (s[1].y-s[0].y)*(s[3].x-s[2].x) );
    		if( dd==0 ) // 区分平行与重合
    		{
    			double ff=( (s[2].x-s[0].x)*(s[3].y-s[0].y) - (s[2].y-s[0].y)*(s[3].x-s[0].x) );
    			if(ff==0)
    			    printf("LINE
    ");
    			else
    				printf("NONE
    ");
    		}
    		
    		else //相交的情况
    		{
                d = ( (s[2].x-s[0].x)*(s[3].y-s[2].y) - (s[2].y-s[0].y)*(s[3].x-s[2].x ) );
    
    			f = ( (s[1].x-s[0].x)*(s[3].y-s[2].y) - (s[1].y-s[0].y)*(s[3].x-s[2].x ) );
    
    			t = d/f;
    			p.x = s[0].x - (s[0].x-s[1].x)*t;
    			p.y = s[0].y - (s[0].y-s[1].y)*t;
    			printf("POINT %.2lf %.2lf
    ", p.x, p.y );
    		}
    
    	}
    	printf("END OF OUTPUT
    ");
    	return 0;
    }
    
    
    
    

  • 相关阅读:
    8 盒子模型
    7 display属性
    如何提高运维价值体系
    Python学习之OS模块初识
    7、MongoDB学习之游标
    Python 学习之文件对象的属性和方法简介
    Python集合set()操作详解
    Python中的字典介绍
    Python序列之元组
    Python序列之列表
  • 原文地址:https://www.cnblogs.com/yspworld/p/3913488.html
Copyright © 2020-2023  润新知