计算几何——交点、面积的计算

交点、面积的计算

一·、基本操作

  0、准备

1 const int maxn = 10005;
2 typedef pair<double, double> _pair;
3 _pair point[maxn];

  1、两点间距离的计算

    Dis= sqrt[ (x2- x1)2+ (y2- y1)2 ];

1 double Get_Dis (_pair point1, _pair point2)
2 {
3     //计算两点间距离
4     return sqrt(((point1.first- point2.first)* (point1.first- point2.first) )
5                 + ((point1.second- point2.second)* (point1.second- point2.second) ) );
6 }

   2、计算叉乘(向量积/外积)

     PAxQB= { [ (A.x- P.x)* (B.y- Q.y) ]- [ (B.x- Q.x)* (A.y- P.y) ] };

 1 double Get_axb (_pair a_point1, _pair a_point2, _pair b_point1, _pair b_point2)
 2 {
 3     //计算两条向量的叉积
 4     //向量a= a_point1 --> a_point2= a_point2- a_point1;
 5     //向量b= b_point1 --> b_point2= b_point2- b_point1;
 6     //叉积axb= (a.x* b.y)- (b.x* a.y);
 7     //a.x= a_point2.x- a_point1.x; a.y= a_point2.y- a_point1.y;
 8     return (((a_point2.first- a_point1.first)* (b_point2.second- b_point1.second) )
 9             - ((b_point2.first- b_point1.first)* (a_point2.second- a_point1.second) ) );
10 }

Ps:求面积或判断相对位置。

  3、计算点乘(内积)

    PA·QB= { [ (A.x- P.x)* (B.x- Q.x) ]- [  (A.y- P.y)* (B.y- Q.y) ] };

 1 double Get_a_xb (_pair a_point1, _pair a_point2, _pair b_point1, _pair b_point2)
 2 {
 3     //计算两条向量的点乘
 4     //向量a= a_point1 --> a_point2= a_point2- a_point1;
 5     //向量b= b_point1 --> b_point2= b_point2- b_point1;
 6     //点乘a·b= (x1* x2)- (y1* y2)= (a.x* b.x)- (a.y* b.y);
 7     //a.x= a_point2.x- a_point1.x; a.y= a_point2.y- a_point1.y;
 8     return (((a_point2.first- a_point1.first)* (b_point2.first- b_point1.first) )
 9             - ((a_point2.second- a_point1.second)* (b_point2.second- b_point1.second) ) );
10 }

Ps:判断直线是否正交(垂直)。

   4、余弦定理

    cos A= (b²+ c²- a² )/ 2bc;

    用法:配合各种反三角函数(如acos(), atan();)求各种角度,配合点旋转求交点。

     Ps:给出一个向量a求与x 轴所成角的余弦值。

 1 double Get_Cos (_pair point1, _pair point2)
 2 {
 3     //计算向量a(point1-->point2) 的余弦值;
 4     point2.first-= point1.first;                //把point1看作坐标原点(0, 0);
 5     point2.second-= point1.second;              //则point2的坐标为(P2.x- P1.x, P2.y- P1.y);
 6     point1.first= 0;
 7     point1.second= 0;
 8     _pair point3;                               //在X轴上找一点P3,做直角三角形;
 9     point3.first= point2.first;                 //P3.x= P2.x;
10     point3.second= 0;                           //P3.y= P1.y= 0;
11     double Dis_P1_P2= Get_Dis(point1, point2);  //计算直角三角形的斜边长,即P1P2之间的距离;
12     return point3.first/ Dis_P1_P2;             //邻边/ 斜边;
13 }

 end;

原文地址:https://www.cnblogs.com/Amaris-diana/p/10744087.html