现在的位置: 首页 > 综合 > 正文

ACM-计算几何之Leyni, LOLI and Line——hrbust1104

2014年11月11日 ⁄ 综合 ⁄ 共 2108字 ⁄ 字号 评论关闭

Leyni, LOLI and Line


题目:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1104

Description

Professor Leyni likes to help LOLIs with their math.
This time, Leyni meets several LOLIs in the classroom and gets several problems about "Intersecting Lines".
The LOLIs want to know how and where two lines intersect.Leyni asks you to help him to answer.

Input
There are multiple test cases. The first line of input is an integer T indicating the number of test cases. Then T test cases follow.
For each test case:
Line 1. This line contains 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).
All numbers required by this problem will be in the range [-1000, 1000].

Output
For each test case:
Line 1.If there is no intersection, output "NONE". If they are the same line, output "LINE". Otherwise output the x and y coordinates of the point, correct to two decimal places as the sample 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
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20

Hint
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.

判断直线相交问题。

若相交则输出交点,若平行则输出NONE,若重合则输出LINE

这道题和poj一道题几乎一模一样。。。(http://blog.csdn.net/lttree/article/details/23877305

就是输入输出上有些差别,代码我也试过,这两个代码完全可以互换用,都能AC。。

但是,这个代码比较精简一些。


#include <stdio.h>
int main()
{
    int t;
    double x1,x2,x3,x4,y1,y2,y3,y4,x,y;
    scanf("%d",&t);
    while(t--)
    {
        // 输入线段两个端点
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4);
        // 判断是平行还是重合
        if( (x2-x1)*(y4-y3) == (x4-x3)*(y2-y1) )
        {
            if( (x3-x1)*(y4-y2) == (x4-x2)*(y3-y1) && (y4-y3)!=0 )
                printf("LINE\n");
            else    printf("NONE\n");
        }
        else    // 若相交则求交点,不难推出来
        {
            x=( ((y1*(x2-x1))-x1*(y2-y1))*(x4-x3)-(y3*(x4-x3)-x3*(y4-y3))*(x2-x1) ) / ( (y4-y3)*(x2-x1)-(y2-y1)*(x4-x3) );
            y=( (y1*(x2-x1)-x1*(y2-y1))*(y4-y3)-(y3*(x4-x3)-x3*(y4-y3))*(y2-y1) ) / ( (y4-y3)*(x2-x1)-(y2-y1)*(x4-x3) );
            printf("POINT %.2lf %.2lf\n",x,y);
        }
    }
    return 0;
}



抱歉!评论已关闭.