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


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

相关内容

热门资讯

第三方技巧!hhpoker软件... 您好,hhpoker可以控制吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
最终!德普之星透视软件免费入口... 最终!德普之星透视软件免费入口官网,aapoker透视脚本,切实存在有挂(今日头条)1、下载好德普之...
日前!模拟器打开hhpoker... 日前!模拟器打开hhpoker,德普辅助软件,总是确实有挂(有挂解惑)1、进入到模拟器打开hhpok...
2026版总结!werplan... 2026版总结!werplan免费挂下载,德州圈脚本,好像真的有挂(有挂技巧)一、werplan免费...
反观!wpk真吗,hhpoke... 反观!wpk真吗,hhpoker破解工具,都是真的有挂(证实有挂)运hhpoker破解工具辅助工具,...
在玩家背景下!hhpoker透... 在玩家背景下!hhpoker透视脚本下载,hhpoker辅助挂,一直确实有挂(有挂详情)1、让任何用...
黑科技辅助挂!fishpoke... 黑科技辅助挂!fishpoker透视底牌,hhpoker有没有作弊挂,一直真的是有挂(真的有挂)1、...
今天上午!aapoker怎么选... 今天上午!aapoker怎么选牌,wepoker私人辅助器,都是是真的有挂(有挂实锤)1、不需要AI...
据目击者称!wpk德州局透视,... 据目击者称!wpk德州局透视,wpk是真的还是假的,真是存在有挂(有挂规律)1、玩家可以在wpk是真...
第三方技巧!xpoker辅助工... 第三方技巧!xpoker辅助工具,wpk俱乐部怎么作弊,一直是有挂(有挂透明挂)1、上手简单,内置详...