POJ1269_Intersecting Lines(几何/叉积判断直线位置关系)

解题报告

题目传送门

题意:

判断直线的位置关系(平行,重合,相交)

思路:

两直线可以用叉积来判断位置关系。

AB直线和CD直线

平行的话端点C和端点D会在直线AB的同一侧。

重合的话在直线AB上。

剩下就是相交。

求两直线交点可以用面积比和边长比来求。

看下面的图就知道了,推导就比较容易了

#include <iostream>
#include <cstring>
#include <cstdio>
#define eps 1e-6
#define zero(x) (((x)>0?(x):-(x))>eps)
using namespace std;
struct Point
{
    double x,y;
};
struct L
{
    Point l,r;
};
double xmulti(Point a,Point b,Point p)
{
    return (b.x-a.x)*(p.y-a.y)-(p.x-a.x)*(b.y-a.y);
}
int main()
{
    int n,i,j;
    scanf("%d",&n);
    cout<<"INTERSECTING LINES OUTPUT"<<endl;
    L l1,l2;
    Point p;
    for(i=0; i<n; i++)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&l1.l.x,&l1.l.y,&l1.r.x,&l1.r.y,&l2.l.x,&l2.l.y,&l2.r.x,&l2.r.y);
        double a,b,c,d;
        a=xmulti(l1.l,l1.r,l2.l);//c
        b=xmulti(l1.l,l1.r,l2.r);//d
        c=xmulti(l2.l,l2.r,l1.l);
        d=xmulti(l2.l,l2.r,l1.r);
        if(a==0&&b==0)
            cout<<"LINE"<<endl;
        else if(a*b>0&&c*d>0)
        {
            cout<<"NONE"<<endl;
        }
        else
        {
            p.x=(b*l2.l.x-a*l2.r.x)/(b-a);
            p.y=(b*l2.l.y-a*l2.r.y)/(b-a);
            printf("POINT %.2lf %.2lf\n",p.x,p.y);
        }
    }
    cout<<"END OF OUTPUT"<<endl;
}

Intersecting Lines

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10764   Accepted: 4803

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

Source

Mid-Atlantic 1996

POJ1269_Intersecting Lines(几何/叉积判断直线位置关系)

时间: 2024-12-31 03:49:28

POJ1269_Intersecting Lines(几何/叉积判断直线位置关系)的相关文章

POJ 1269 Intersecting Lines (判断直线位置关系)

题目链接:POJ 1269 Problem 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 becau

JS魔法堂:判断节点位置关系

一.前言 在polyfill querySelectorAll 和写弹出窗时都需要判断两个节点间的位置关系,通过jQuery我们可以轻松搞定,但原生JS呢?下面我将整理各种判断方法,以供日后查阅. 二.祖孙关系 html <div id="ancestor"> <div id="parent"> <div id="son">son</div> </div> </div> &l

判断两条直线的位置关系 POJ 1269 Intersecting Lines

两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, p2, p3, p4,直线L1,L2分别穿过前两个和后两个点.来判断直线L1和L2的关系 这三种关系一个一个来看: 1. 共线. 如果两条直线共线的话,那么另外一条直线上的点一定在这一条直线上.所以p3在p1p2上,所以用get_direction(p1, p2, p3)来判断p3相对于p1p2的关

简单几何(直线位置) POJ 1269 Intersecting Lines

题目传送门 题意:判断两条直线的位置关系,共线或平行或相交 分析:先判断平行还是共线,最后就是相交.平行用叉积判断向量,共线的话也用叉积判断点,相交求交点 /************************************************ * Author :Running_Time * Created Time :2015/10/24 星期六 09:08:55 * File Name :POJ_1269.cpp *********************************

POJ1269:Intersecting Lines(判断两条直线的关系)

题目:POJ1269 题意:给你两条直线的坐标,判断两条直线是否共线.平行.相交,若相交,求出交点. 思路:直线相交判断.如果相交求交点. 首先先判断是否共线,之后判断是否平行,如果都不是就直接求交点了. #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <math.h> #include <queue> #

Segments---poj3304(判断直线与线段的位置关系)

题目链接:http://poj.org/problem?id=3304 题意:给你n个线段,求是否有一条直线与所有的线段都相交,有Yes,没有No; 枚举所有的顶点作为直线的两点,然后判断这条直线是否和所有的线段相交即可;注意不能找两个相同的点作为直线上的两点: #include<iostream> #include<algorithm> #include<math.h> #include<string.h> #include<stdio.h>

LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边形的内部; 射线法判断即可适用于任何(凸或凹)多边形;时间复杂度为O(n); 判断一个点是在多边形内部,边上还是在外部,时间复杂度为O(n):射线法可以正确用于凹多边形: 射线法是使用最广泛的算法,这是由于相比较其他算法而言,它不但可以正确使用在凹多边形上,而且不需要考虑精度误差问题.该算法思想是从

Cupid&#39;s Arrow---hdu1756(判断点与多边形的位置关系 模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1756 题意:中文题,套模板即可: /* 射线法:判断一个点是在多边形内部,边上还是在外部,时间复杂度为O(n): 射线法可以正确用于凹多边形: 射线法是使用最广泛的算法,这是由于相比较其他算法而言,它不但可以正 确使用在凹多边形上,而且不需要考虑精度误差问题.该算法思想是从点出 发向右水平做一条射线,计算该射线与多边形的边的相交点个数,当点不在 多边形边上时,如果是奇数,那么点就一定在多边形内部,否

poj 2318 TOYS (点与线段位置关系判断)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10848   Accepted: 5206 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w