Intersecting Lines (計算幾何基礎+判斷兩直線的位置關係)
題面:
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
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 OUTPUTPOINT 2.00 2.00NONELINEPOINT 2.00 5.00POINT 1.07 2.20END OF OUTPUT思路:本題求的就是兩條直線之間的位置關係,如果平行輸出“NONE”,相交輸出“POINT”和交點座標,重合就輸出“LINE”。判斷兩條直線是否平行則判斷兩條直線的單位方向向量是否相等或相反(即斜率是否相等),如果滿足則是平行或重合,否則就是相交,相交就呼叫求交點的函式求出交點即可;而判斷是否重合只需判斷一條直線上的某一點是否在另一條直線上即可。程式碼實現如下:
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct Point {
double x, y;
Point (double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector;
int n;
Point A, B, C, D;
Vector operator + (Vector A, Vector B) {
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Vector A, Vector B) {
return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p) {
return Vector(A.x * p, A.y * p);
}
bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-10;
int dcmp(double x) {
if(fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator == (const Point& a, const Point& b) {
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B) {
return A.x * B.x + A.y * B.y;
}
double Length(Vector A) {
return sqrt(Dot(A, A));
}
double Cross(Vector A, Vector B) {
return A.x * B.y - A.y * B.x;
}
//求單位方向向量
Vector Unit_direction_vector(Vector w) {
return Vector(w.x / Length(w), w.y / Length(w));
}
//判斷兩直線是否不相交
bool isIntersection(Vector A, Vector B) {
return Unit_direction_vector(A) == Unit_direction_vector(B) || Unit_direction_vector(Vector(- A.x, - A.y)) == Unit_direction_vector(B);
}
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
Vector u = P - Q;
double t = Cross (w, u) / Cross(v, w);
return P + v * t;
}
//判斷兩直線是否重合只要判斷是否有公共點即可
bool OnLine(Point p, Point a1, Point a2) {
return dcmp(Cross(a1 - p, a2 - p)) == 0;
}
int main() {
while(~scanf("%d", &n)) {
printf("INTERSECTING LINES OUTPUT\n");
while(n--) {
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &D.x, &D.y);
if(isIntersection(A - B, C - D)) {
if(OnLine(A, C, D)) {
printf("LINE\n");
} else {
printf("NONE\n");
}
} else {
Point P = GetLineIntersection(A, A - B, C, C - D);
printf("POINT %.2f %.2f\n", P.x, P.y);
}
}
printf("END OF OUTPUT\n");
}
}
相關推薦
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 o
1016 - 計算幾何之點與直線的關係 - TOYS(POJ 2318)
傳送門 題意 給你一個這樣的圖 然後隨機給你 m 個點,問落在每一個區域內的點有多少個 分析 入門題入門題 依舊是利用叉積,叉積太強了! 二分尋找並判斷 gsj太厲害了,簡直每次我演算法
計算幾何基礎 POJ 1269 Intersecting Lines 【直線相交判斷,求交點】
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using na
1015 - 計算幾何之直線與直線的關係 - Intersecting Lines(POJ1269)
傳送門 題意 給你兩條直線 若其相交則輸出 交點的座標 若其平行則輸出 NONE 若其重合則輸出 LINE 分析 比較基礎的計算幾何,就是考向量的使用 判斷兩條直線是否平行,就是看其叉積是否為
poj 1269 Intersecting Lines (求兩直線交點)
題目連結:poj 1269 題意:給出n個詢問,每次給兩條邊,有三種不同的結果可以輸出,1,平行不共線,2,平行且共線,3,相交併求出交點 題解:模板題,注意一點的是,判斷兩直線平行時用叉積去判斷,不要簡單的直接用斜率公式去判斷,可能會出現誤差。 程式碼如下: #include&
【CodeForces - 227A】Where do I Turn? (計算幾何,叉積判斷直線拐向)
題幹: Trouble came from the overseas lands: a three-headed dragon Gorynych arrived. The dragon settled at point C and began to terrorize t
GIS演算法基礎(三)計算幾何基礎(下)
判斷線段在多邊形內的演算法: 演算法思路: 如果線段與多邊形內交,則線段一定在多邊形外;如果線段和多邊形的每一條邊都不內交,如果有交點,則線段和多邊形的交點一定是線段的端點或者多邊形的頂點,然後只需要判斷交點是否線上段上就可以了 演算法步驟:
GIS演算法基礎(二)計算幾何基礎(中)
地理資料在計算機中表示大致分為兩種,向量資料和柵格資料。 要計算地理資料的空間關係,一般是向量資料之間的比較。例如:點,線,面之間的比較。 如何判斷線段之間是否相交,線段與面的包含關係。點與面的包含關係等等這些空間關係,都用到計算幾何的演算法。 空間關係的判定演算法的內
GIS演算法基礎(一) 計算幾何基礎(上)
最近在學習GIS演算法,在學習過程中,想把一些經典的演算法或者思想記錄下來,分享給大家 計算幾何基礎本來是計算機圖形學的內容,但是GIS在影象處理中是離不開計算機處理的,所以GIS演算法基礎第一個應該是計算幾何基礎。 如何把空間實體的點線面以及他們之間的關係(例如,相交,包
(計算幾何)判斷一個點是否線上段上
累加器傳送門: 這個問題需要用到向量的叉積性質,下面先從百度截一些語句來介紹一下 用向量的叉積來判斷一個點是否線上段上 回到我們要乾的事情,如果叉積為零,可以證明一個點線上段所在的
計算幾何基礎入門(1)
平面最接近點對&&
POJ3347 Kadj Squares(計算幾何&區間覆蓋)
ica nsis -s ber pro ins ascend char rst 題目鏈接: http://poj.org/problem?id=3347 題目描述: Kadj Squares Description In this problem, you are
HDU 6055 17多校 Regular polygon(計算幾何)
ati sort 結果 stream int tom tle eight idt Problem Description On a two-dimensional plane, give you n integer points. Your task is to figur
hdu 6127 : Hard challenge (2017 多校第七場 1008)(計算幾何)
for %d logs opera log val r+ ++ show 題目鏈接 題意:二維平面上有n個點(沒有重疊,都不在原點,任意兩點連線不過原點),每個點有一個權值,用一條過原點的直線把他們劃分成兩部分,使兩部分的權值和的乘積最大。輸出最大的乘積。 極角排序後,將原
Codeforces 849B Tell Your World (計算幾何)
continue int ++i 滿足 light size sizeof tar tell 題目鏈接 Tell Your World 題意 給出N個點(i, xi),問是否存在兩條平行的直線,使得每一個點恰好在兩條直線的其中一條上。 每條直線必須穿過至少一個點。 考
計蒜客 直線的交點(計算幾何 + 逆序對)
clas ret ons oid date pda 所有 efi define 題目鏈接 直線的交點 兩條直線的交點如果落在兩個平板之內的話 假設這兩條直線和兩條平板的交點橫坐標分別為 $x1, x2, X1, X2$ 那麽有$(x2 - x1)(X2 - X1)
POJ 2318 TOYS (計算幾何)叉積的運用
line sync ted all names pri char ems sizeof Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have
POJ 2398 - Toy Storage - [計算幾何基礎題][同POJ2318]
fin 邊界 vector nta 鏈接 ++ 輸出 mine puts 題目鏈接:http://poj.org/problem?id=2398 Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad
HDU 4173 Party Location(計算幾何,枚舉)
mes def air 思路 lib gpo 畫畫 ons dsm HDU 4173 題意:已知n(n<=200)位參賽選手的住所坐標。現要邀請盡可能多的選手來參加一個party,而每一個選手對於離住所超過2.5Km
51 Nod 1298 圓與三角形(計算幾何)
tput body bits truct 大於 簡單 以及 else c++ 題目鏈接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1298 題目: 1298 圓與三角形