|
楼主 |
发表于 2016-4-7 21:31:29
|
显示全部楼层
几何计算——三角形的垂心(二)
- #include <stdio.h>
- #include <math.h>
- #include <float.h>
-
- typedef struct tagPOINT
- {
- double x;
- double y;
- }POINT;
-
- typedef struct tagLINE
- {
- POINT point_beg;
- POINT point_end;
- }LINE;
-
- typedef struct tagTRI_ANGLE
- {
- POINT point[3];
- }TRI_ANGLE;
-
- int GetVerticalPoint(const POINT* point_demo, const LINE* line_demo, POINT* dst)
- {
- double line_demo_dx, line_demo_dy, cal_temp = 0.0f;
- line_demo_dx = line_demo->point_end.x - line_demo->point_beg.x;
- line_demo_dy = line_demo->point_end.y - line_demo->point_beg.y;
- cal_temp += line_demo->point_beg.y * line_demo->point_end.x;
- cal_temp -= line_demo->point_beg.x * line_demo->point_end.y;
- cal_temp += line_demo->point_end.y * point_demo->x;
- cal_temp -= line_demo->point_end.x * point_demo->y;
- cal_temp += point_demo->y * line_demo->point_beg.x;
- cal_temp -= point_demo->x * line_demo->point_beg.y;
- cal_temp /= (line_demo_dx * line_demo_dx + line_demo_dy * line_demo_dy);
- dst->x = point_demo->x - line_demo_dy * cal_temp;
- dst->y = point_demo->y + line_demo_dx * cal_temp;
- return 0;
- }
-
- int GetVerticalLine(const POINT* point_demo, const LINE* line_demo, LINE* dst)
- {
- dst->point_beg = *point_demo;
- if (0 != GetVerticalPoint(point_demo, line_demo, &(dst->point_end)))
- {
- return -1;
- }
- return 0;
- }
-
- int GetLineCross(const LINE* line0, const LINE* line1, POINT* point_cross)
- {
- double x1_x2, x3_x4, y1_y2, y3_y4, xy12, xy34, gain_all;
- x1_x2 = line0->point_beg.x - line0->point_end.x;
- y1_y2 = line0->point_beg.y - line0->point_end.y;
- x3_x4 = line1->point_beg.x - line1->point_end.x;
- y3_y4 = line1->point_beg.y - line1->point_end.y;
- gain_all = x1_x2 * y3_y4 - y1_y2 * x3_x4;
- if (abs(gain_all) < DBL_EPSILON)
- {
- return -1;
- }
- xy12 = line0->point_beg.x * line0->point_end.y - line0->point_end.x * line0->point_beg.y;
- xy34 = line1->point_beg.x * line1->point_end.y - line1->point_end.x * line1->point_beg.y;
- gain_all = 1.0 / gain_all;
- point_cross->x = (xy12 * x3_x4 - xy34 * x1_x2) * gain_all;
- point_cross->y = (xy12 * y3_y4 - xy34 * y1_y2) * gain_all;
- return 0;
- }
-
- int GetTri_angleVertical(const TRI_ANGLE* tri_angle_demo, POINT* point_demo)
- {
- LINE line_temp0, line_temp1;
- LINE line_vertical0, line_vertical1;
- line_temp0.point_beg = tri_angle_demo->point[1];
- line_temp0.point_end = tri_angle_demo->point[2];
- if (0 != GetVerticalLine((const POINT*)(tri_angle_demo->point), (const LINE*)&line_temp0, &line_vertical0))
- {
- return -1;
- }
- line_temp0.point_beg = tri_angle_demo->point[2];
- line_temp0.point_end = tri_angle_demo->point[0];
- if (0 != GetVerticalLine((const POINT*)(tri_angle_demo->point) + 1, (const LINE*)&line_temp1, &line_vertical1))
- {
- return -1;
- }
- if (0 != GetLineCross((const LINE*)&line_vertical0, (const LINE*)&line_vertical1, point_demo))
- {
- return -1;
- }
- return 0;
- }
复制代码 |
|