POJ 1269 Intersecting Lines(计算几何) (两线段位置)
2018-02-11 14:29
495 查看
Intersecting Lines
DescriptionWe 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.
InputThe 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).OutputThere 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 Input5
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 OutputINTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT
Source
【题意】
判断两直线位置, 共线(重合) 平行 相交, 相交求交点
【思路】
4个点, 两条直线,直线p1p2. ,p3p4 根据向量叉乘判断, 若共线,则有 p1p3 X p2p3 =0 && p1p4 X p2p4 =0
p1p2p3三点共线, p1p2p4三点共线;
若平行则有 向量 p1p2 X p3p4 =0 两直线共线
若相交, 假设交点 p0(x0,y0); 有
p0p1 X p0p2=0
p0p3 X p0p4=0
则有:
(y1-y2) x0 + (x2-x1) y0 + x1y2-x2y1 =0
(y3-y4) x0 + (x4-x3) y0 + x3y4-x4y3 =0
有 a1x + b1y +c1 =0
a2x+ b2y + c2 =0 联立方程解的
[代码实现]#include <iostream>
//#include <bits/stdc++.h>
#include <stdio.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define SHUT ios_base::sync_with_stdio(false);cout.precision(20);cout.setf(ios::fixed);cout.tie(nullptr);cin.tie(nullptr);
const int INF=0x3f3f3f3f;
const double esp=1e-8;
typedef long long ll;
typedef long double ld;
const int MAXN=1e6+5;
using namespace std;
struct node{
double x,y;
};
typedef struct Segments{
node S1,S2;
}Seg;
bool judge_parallel(node p1,node p2,node p3,node p4)
{
return (p4.y-p3.y)*(p2.x-p1.x)-(p2.y-p1.y)*(p4.x-p3.x);
}
bool judge_Collinear(node p1,node p2,node p3)
{
return (p3.y-p2.y)*(p3.x-p1.x)-(p3.x-p2.x)*(p3.y-p1.y);
}
void solve(node p1,node p2,node p3,node p4)
{
ld a1=p1.y-p2.y;
ld a2=p3.y-p4.y;
ld b1=p2.x-p1.x;
ld b2=p4.x-p3.x;
ld c1=p1.x*p2.y-p2.x*p1.y;
ld c2=p3.x*p4.y-p4.x*p3.y;
double x0= (c1*b2-c2*b1)/(a2*b1-a1*b2);
double y0= (a2*c1-a1*c2)/(a1*b2-a2*b1);
// cout<<"POINT "<<x0<<" "<<y0<<endl;
// printf("POINT %.2lf %.2lf\n",x0,y0);
printf("POINT %.2f %.2f\n",x0,y0);
}
int main()
{
int t;
cin>>t;
// cout.precision(3);
puts("INTERSECTING LINES OUTPUT");
node p1,p2,p3,p4;
while(t--)
{
cin>>p1.x>>p1.y;
cin>>p2.x>>p2.y;
cin>>p3.x>>p3.y;
cin>>p4.x>>p4.y;
if(judge_Collinear(p1,p2,p3)==0&&judge_Collinear(p1,p2,p4)==0) // 共线
cout<<"LINE"<<endl;
else
{
if(judge_parallel(p1,p2,p3,p4)==0)//平行
{
cout<<"NONE"<<endl;
}
else
{
solve(p1,p2,p3,p4);// 求交点
}
}
}
puts("END OF OUTPUT");
return 0;
}
123
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17299 | Accepted: 7449 |
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.
InputThe 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).OutputThere 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 Input5
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 OutputINTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT
Source
【题意】
判断两直线位置, 共线(重合) 平行 相交, 相交求交点
【思路】
4个点, 两条直线,直线p1p2. ,p3p4 根据向量叉乘判断, 若共线,则有 p1p3 X p2p3 =0 && p1p4 X p2p4 =0
p1p2p3三点共线, p1p2p4三点共线;
若平行则有 向量 p1p2 X p3p4 =0 两直线共线
若相交, 假设交点 p0(x0,y0); 有
p0p1 X p0p2=0
p0p3 X p0p4=0
则有:
(y1-y2) x0 + (x2-x1) y0 + x1y2-x2y1 =0
(y3-y4) x0 + (x4-x3) y0 + x3y4-x4y3 =0
有 a1x + b1y +c1 =0
a2x+ b2y + c2 =0 联立方程解的
[代码实现]#include <iostream>
//#include <bits/stdc++.h>
#include <stdio.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define SHUT ios_base::sync_with_stdio(false);cout.precision(20);cout.setf(ios::fixed);cout.tie(nullptr);cin.tie(nullptr);
const int INF=0x3f3f3f3f;
const double esp=1e-8;
typedef long long ll;
typedef long double ld;
const int MAXN=1e6+5;
using namespace std;
struct node{
double x,y;
};
typedef struct Segments{
node S1,S2;
}Seg;
bool judge_parallel(node p1,node p2,node p3,node p4)
{
return (p4.y-p3.y)*(p2.x-p1.x)-(p2.y-p1.y)*(p4.x-p3.x);
}
bool judge_Collinear(node p1,node p2,node p3)
{
return (p3.y-p2.y)*(p3.x-p1.x)-(p3.x-p2.x)*(p3.y-p1.y);
}
void solve(node p1,node p2,node p3,node p4)
{
ld a1=p1.y-p2.y;
ld a2=p3.y-p4.y;
ld b1=p2.x-p1.x;
ld b2=p4.x-p3.x;
ld c1=p1.x*p2.y-p2.x*p1.y;
ld c2=p3.x*p4.y-p4.x*p3.y;
double x0= (c1*b2-c2*b1)/(a2*b1-a1*b2);
double y0= (a2*c1-a1*c2)/(a1*b2-a2*b1);
// cout<<"POINT "<<x0<<" "<<y0<<endl;
// printf("POINT %.2lf %.2lf\n",x0,y0);
printf("POINT %.2f %.2f\n",x0,y0);
}
int main()
{
int t;
cin>>t;
// cout.precision(3);
puts("INTERSECTING LINES OUTPUT");
node p1,p2,p3,p4;
while(t--)
{
cin>>p1.x>>p1.y;
cin>>p2.x>>p2.y;
cin>>p3.x>>p3.y;
cin>>p4.x>>p4.y;
if(judge_Collinear(p1,p2,p3)==0&&judge_Collinear(p1,p2,p4)==0) // 共线
cout<<"LINE"<<endl;
else
{
if(judge_parallel(p1,p2,p3,p4)==0)//平行
{
cout<<"NONE"<<endl;
}
else
{
solve(p1,p2,p3,p4);// 求交点
}
}
}
puts("END OF OUTPUT");
return 0;
}
123
相关文章推荐
- poj 1269 Intersecting Lines(计算几何:线段相交)
- 计算几何 POJ 2826 An Easy Problem?! (线段位置判断并且求交点)
- POJ-2318 TOYS 计算几何 判断点在线段的位置
- POJ 1410 Intersection 计算几何 判断线段与矩形位置
- poj2318——TOYS(计算几何+点与线段的位置)
- poj 1269 Intersecting Lines(计算几何)
- poj 3565(计算几何,线段相交)
- poj 1584 计算几何 (点到线段距离+判断点是否在多边形内+判断多边形是否为凸包)
- POJ 3304 Segments (计算几何、判断直线与线段是否相交)
- poj 1269 Intersecting Lines(计算几何)
- POJ 1066 Treasure Hunt [线段相交]【计算几何】
- POJ 1269 计算几何 求直线交点
- [计算几何]POJ 1375 点对圆的切线+线段重叠
- [poj] 3347 Kadj Square || 计算几何的“线段覆盖”
- POJ 1410(计算几何初步——判断线段与矩形相交,条件恶心的一B啊,又没说清楚)
- POJ 3304(计算几何初步——判断线段和直线相交,加上枚举)
- 简单几何(直线位置) POJ 1269 Intersecting Lines
- poj 2318 TOYS(计算几何 点与线段的关系)
- POJ 1269 Intersecting Lines(计算几何)
- POJ1066---Treasure Hunt (计算几何基础:线段相交)