UVa 378 - Intersecting Lines

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 using namespace std;
 6 struct point{
 7     int x,y;
 8     point(int x = 0,int y = 0){
 9         this->x = x;
10         this->y = y;
11     }
12 };
13 struct line{
14     point st,en;
15     line(){}
16 };
17 class Intersect {
18     private:
19         line la,lb;
20     public:
21         void readData();
22         void process();
23         bool isCross();//
24         bool isSameLine();
25 };
26 void Intersect::readData(){
27     point p1,p2,p3,p4;
28     cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y>>p4.x>>p4.y;
29     la.st = p1;la.en = p2;
30     lb.st = p3;lb.en = p4;
31 }
32 void Intersect::process(){
33     if(isCross()){
34         //求出交点
35         double A1 = la.st.y - la.en.y,B1 = la.en.x - la.st.x;
36         double C1 = la.st.x * la.en.y - la.st.y * la.en.x;//A1,B1,C1是方程系数
37         double A2 = lb.st.y - lb.en.y,B2 = lb.en.x - lb.st.x;
38         double C2 = lb.st.x * lb.en.y - lb.st.y * lb.en.x;
39         double x = (B1*C2 - B2*C1)/(A1*B2 - A2*B1);
40         double y = (C1*A2 - C2*A1)/(A1*B2 - A2*B1);//方程的解
41         printf("POINT %.2lf %.2lf\n",x,y);
42     }
43     else{
44         if(isSameLine()==0){
45             cout<<"LINE"<<endl;
46         }
47         else
48             cout<<"NONE"<<endl;
49     }
50 }
51 bool Intersect::isCross(){//判断是否是相交的
52     if((la.en.x - la.st.x)*(lb.en.y - lb.st.y) ==
53        (la.en.y - la.st.y)*(lb.en.x - lb.st.x)){
54        return false;
55     }
56     else
57         return true;
58 }
59 bool Intersect::isSameLine(){//用向量叉积判断点是否在线上
60     return (lb.st.x-lb.en.x)*(la.st.y-lb.en.y)-(la.st.x-lb.en.x)*(lb.st.y-lb.en.y);
61 }
62 int main()
63 {
64     int cases;
65     Intersect intersect;
66     #ifndef ONLINE_JUDGE
67         freopen("D://acm.txt","r",stdin);
68     #endif // ONLINE_JUDGE
69     while(cin>>cases){
70         cout<<"INTERSECTING LINES OUTPUT"<<endl;
71         while(cases--){
72             intersect.readData();
73             intersect.process();
74         }
75         cout<<"END OF OUTPUT"<<endl;
76     }
77     return 0;
78 }
时间: 2024-10-11 00:53:39

UVa 378 - Intersecting Lines的相关文章

[POJ 1269]Intersecting Lines

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16311   Accepted: 7040 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

POJ1269 Intersecting Lines[线段相交 交点]

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15145   Accepted: 6640 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

POJ 1269 - Intersecting Lines 直线与直线相交

题意:    判断直线间位置关系: 相交,平行,重合 1 include <iostream> 2 #include <cstdio> 3 using namespace std; 4 struct Point 5 { 6 int x , y; 7 Point(int a = 0, int b = 0) :x(a), y(b) {} 8 }; 9 struct Line 10 { 11 Point s, e; 12 int a, b, c;//a>=0 13 Line() {

Poj_1269 Intersecting Lines -判两直线状态(水题、坑OJ)

题意:判直线相交.平行.重合 吐槽:坑OJ,G++WA,C++AC,模版题 /************************************************ Author :DarkTong Created Time :2016/8/3 23:02:25 File Name :Poj_1269.cpp *************************************************/ //#include <bits/stdc++.h> #include &

POJ 1269 Intersecting Lines(线段相交,水题)

Intersecting Lines 大意:给你两条直线的坐标,判断两条直线是否共线.平行.相交,若相交,求出交点. 思路:线段相交判断.求交点的水题,没什么好说的. struct Point{ double x, y; } ; struct Line{ Point a, b; } A, B; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.y-p.y)-(p1.y-p.y)*(p2.x-p.x); } bool

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

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

【POJ】Intersecting Lines(计算几何)

http://poj.org/problem?id=1269 我会说这种水题我手推公式+码代码用了1.5h? 还好新的一年里1A了---- #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> #include <

Intersecting Lines - POJ 1269(判断平面上两条直线的关系)

分析:有三种关系,共线,平行,还有相交,共线和平行都可以使用叉积来进行判断(其实和斜率一样),相交需要解方程....在纸上比划比划就出来了.... 代码如下: ====================================================================================================================================== #include<math.h> #include<algor

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

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