C判断一个点在三角形上
创始人
2025-01-07 21:05:42
0
 背景

鼠标操作时,经常要判断是否命中显示控件,特开发此算法快速判断。

原理

三角形三等分点定理是指在任意三角形ABC中,可以找到三个点D、E和F,使得线段AD、BE和CF均等分三角形ABC。 这意味着三个等分点分别位于三个边界上,并且每个等分点都将三角形划分为等面积部分。

代码
#include  #include   // 点 typedef struct {     float x;     float y; } *Point;  // 直线距离 float distance(Point a, Point b) {     return sqrt((b->x - a->x) * (b->x - a->x) + (b->y - a->y) * (b->y - a->y)); }  // 三角形面积 float area(Point a, Point b, Point c) {     float ab = distance(a, b);     float ac = distance(a, c);     float bc = distance(b, c);     return 0.5 * sqrt(4 * ab * ab * ac * ac - (ab * ab + ac * ac - bc * bc) * (ab * ab + ac * ac - bc * bc)); }  // 判断点是否在三角形上 _Bool isPtOn(Point a, Point b, Point c, Point pt) {     float triangleArea = area(a, b, c);       float splitTriangle1 = area(a, b, pt);     float splitTriangle2 = area(a, c, pt);     float splitTriangle3 = area(b, c, pt);     return (splitTriangle1 + splitTriangle2 + splitTriangle3 == triangleArea); }  int main(int argc, char **argv) {     Point a = malloc(sizeof(float) * 2);     a->x = -5;     a->y = 0;     Point b = malloc(sizeof(float) * 2);     b->x = 0;     b->y = 5;     Point c = malloc(sizeof(float) * 2);     c->x = 5;     c->y = 0;          // 在三角形上     Point d = malloc(sizeof(float) * 2);     d->x = 0;     d->y = 1;     _Bool ret = isPtOn(a, b, c, d); 	printf("%d\n", ret); 	 	// 不在三角形上     d->x = 0;     d->y = -1; 	ret = isPtOn(a, b, c, d); 	printf("%d\n", ret); }
输出
1 0
扩展

如何判断在矩形,任意多边形上?(提示:计算多边形面积是等于点切割多边形面积和)

C双指针元素去重-CSDN博客

Qt 判断一个点是否落在三角形内(算法)_qt判断一个点在三角形内-CSDN博客


创作不易,小小的支持一下吧!

相关内容

热门资讯

wepower有外挂!hhpo... wepower有外挂!hhpoker德州有挂(透视)存在挂教程(有挂猫腻)-哔哩哔哩;1、超多福利:...
wepokeai机器人!wej... wepokeai机器人!wejoker透视方法(透视)细节揭秘(确实有挂)-哔哩哔哩;亲真的是有正版...
wepokeai代打的胜率!p... wepokeai代打的胜率!pokermaster脚本(透视)黑科技教程(有挂秘笈)-哔哩哔哩是一款...
wepoke黑科技!德普之星透... wepoke黑科技!德普之星透视辅助软件是真的(透视)教你攻略(有挂方略)-哔哩哔哩是一款可以让一直...
wepoke是真的有挂!hh ... wepoke是真的有挂!hh poker辅助有用(透视)细节方法(有挂功能)-哔哩哔哩;最新版202...
wepokeai代打逻辑!po... wepokeai代打逻辑!pokemmo手机版脚本免费(透视)透明教程(真的有挂)-哔哩哔哩;是一款...
wepoke有app软件!we... wepoke有app软件!wepoker辅助工具(透视)透牌教程(有挂分析)-哔哩哔哩;wepoke...
wepok软件透明挂!哈糖大菠... 【福星临门,好运相随】;wepok软件透明挂!哈糖大菠萝软件下载(透视)2025新版教程(揭秘有挂)...
七分钟了解!新畅游互娱科技(辅... 七分钟了解!新畅游互娱科技(辅助挂)详细透视开挂辅助新2025版(切实真的有挂)-哔哩哔哩;1、首先...
wepokeai代打逻辑!德州... wepokeai代打逻辑!德州hhpoker脚本(透视)辅助教程(有挂秘笈)-哔哩哔哩;是一款可以让...