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博客


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

相关内容

热门资讯

透视科技“wepoker底牌透... 透视科技“wepoker底牌透视脚本app”其实存在有挂(辅助挂)详细辅助微扑克教程;辅助器中分为三...
wepoker脚本!aapok... wepoker脚本!aapoker发牌逻辑,wepoker透视(详细辅助攻略教程)1)wepoker...
透视肯定!hhpoker辅助软... 透视肯定!hhpoker辅助软件下载,hhpoker德州真的假的,详细教程(都是存在有挂)1、每一步...
透视插件!红龙poker有辅助... 《透视插件!红龙poker有辅助吗,pokemmo脚本辅助器下载(详细辅助新2025教程)》 pok...
透视教学“wepoker透视脚... 透视教学“wepoker透视脚本安卓”总是是有挂(辅助挂)详细辅助系统教程1、每一步都需要思考,不同...
智星德州菠萝插件官网!wepo... 智星德州菠萝插件官网!wepoker怎么破解游戏,智星菠萝辅助(详细辅助攻略教程),您好,智星德州菠...
透视工具!hhpoker免费透... 透视工具!hhpoker免费透视脚本,wepoker软件安装包,软件教程(切实真的有挂)1、下载好h...
透视模拟器!hhpoker透视... 透视模拟器!hhpoker透视脚本安卓,agpoker辅助(详细辅助详细教程)这是由厦门游乐互动科技...
透视辅助“德普之星透视辅助”其... 透视辅助“德普之星透视辅助”其实真的是有挂(辅助挂)详细辅助透牌教程1、进入游戏-大厅左侧-新手福利...
aapoker透视插件!wep... aapoker透视插件!wepoker代打辅助机器人,aapoker可以开挂吗(详细辅助揭秘教程);...