給出點a1,a2,b1,b2的座標, 分別構成線段a1a2, 線段b1b2, 求兩線段的交點座標
線段a1a2,b1b2如下圖所示, 並建立輔助線(**於《挑戰程式設計競賽2>>)
step1: 先求出b1點到直線a1a2的距離d1, 和b2點到直線a1a2的距離d2
可根據向量法求出(a1b1為向量, 其它一樣)
step2: 求出 t
可根據相似三角形 d1 : d2= t : (1-t)
得step3: 求出交點點o的座標
根據比例縮放可求出向量b1o, t為小數, 向量b1o=t * 向量b1b2
又因為向量b1o=點o座標 - 點b1座標, 而b1座標已知, 可求出點o的座標
#include#include#includeusing namespace std;
typedef struct node
node;
inline node vector(node a, node b);
double dis2(node a, node b);
double cross(node a, node b, node p);
double dot(node a, node b, node p);
int dir(node a, node b, node p);
double disline(node a, node b, node p);
int main()
; cout << inter.x << ' ' << inter.y << endl;
} else cout << "沒有交點" << endl;
return 0;
}inline node vector(node a, node b) //向量ab;}
double dis2(node a, node b) //兩點間的距離的平方
double cross(node a, node b, node p) //向量的外積
double dot(node a, node b, node p) //向量的內積
int dir(node a, node b, node p) //點與線段方位判定
}double disline(node a, node b, node p) //點p到直線ab的距離
參考書籍: 挑戰程式設計競賽 計算幾何之求兩線段的交點
當兩條線段有交點的時候,交點座標可以用叉乘來求。思路就是連線線段的端點,構造向量,從而構造出相似三角形,然後求出交點在一條線段上的位置 用比例t來表示 然後再加到線段端點上就可以了。題目 cgl 2 c ac include include include using namespace std d...
計算幾何 二維幾何 模板
幾何意義 向量的點積 a b a b a b cos cos 若cos cos 為正,兩向量之間的夾角為銳角 為負,兩向量夾角為鈍角 為量,兩向量夾角為直角。b在a方向上的投影 向量的叉積 a b a b a b sin sin 數值上表示a和b構成的平行四邊形的面積。include using n...
二維計算幾何複習
二維計算幾何 宣告 由於本人較弱,並不能保證以下內容的100 正確 歡迎大佬來挑錯 基本定義 1 struct point 6 point double xx,double yy 9 10 typedef point vec 1112 vec operator vec v1,vec v2 15 ve...