三角型,已知三个角度和两个定点坐标,求第三个定点坐标
一个三角形,已知两个顶点坐标p1
和p2
,和三个顶角angle1
, angle2
, angle3
,求第三个顶点p3的坐标。方法如下:
1 2 3
| x = (p1X * cot(angle2) + p2X * cot(angle1) + p2Y - p1Y) / cot(angle1) + cot(angle2));
y = (p1Y * cot(angle2) + p2Y * cot(angle1) + p1X - p2X) / (cot(angle1) + cot(angle2));
|
其中 cot(alpha) = 1/tan(alpha);
, 注意,p1
,p2
,p3
逆时针位置关系。
叉乘-Cross Production
叉乘是用于判断点与向量的位置关系。如已知向量起点为 p1,终点为 p2,待判断的点为 pt。那么点与向量的位置关系由以下公式决定:
1
| float tmp = (pt1->y - pt2->y)*pt->x + (pt2->x - pt1->x)*pt->y + pt1->x*pt2->y - pt2->x*pt1->y;
|
其中各个点由struct 定义:
1 2 3 4 5
| struct Points{ int x; int y; float value; };
|
简单的数学完整实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <stdlib.h> #include <stdio.h>
struct Points{ int x; int y; float value; };
int pointLinePosition(struct Points*, struct Points*, struct Points*);
int main(int argc, char** argv){ struct Points p0, p1; p0.x = 0, p0.y = 0; p1.x = 2, p1.y = 6;
struct Points px; px.x = 5, px.y = 100;
int res = pointLinePosition(&p0, &p1, &px);
if (res == 1){ printf("left on \n"); } else if (res == 0) printf("right \n");
return 0; }
int pointLinePosition(struct Points* pt1, struct Points* pt2, struct Points* pt){
float tmp = (pt1->y - pt2->y)*pt->x + (pt2->x - pt1->x)*pt->y + pt1->x*pt2->y - pt2->x*pt1->y;
if (tmp > 0 ){ printf("tmp: %.5f \n", tmp); return 1; } else if(tmp < 0){ printf("tmp: %.5f \n", tmp); return 0; } else{ printf("tmp: %.5f \n", tmp); return 1; } }
|