2D空间中判断一点是否在三角形内
要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y。
实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内。
效果:
代码(Unity3D):
using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class TriangleCollider : MonoBehaviour
{
public const float ERROR = 0.05f;
public Transform trianglePoint1;
public Transform trianglePoint2;
public Transform trianglePoint3;
public Transform comparePoint; public bool IsContract(Vector3 comparePoint)
{
var dir1 = trianglePoint2.position - trianglePoint1.position;
var dir2 = trianglePoint3.position - trianglePoint2.position;
var dir3 = trianglePoint1.position - trianglePoint3.position; var cross1 = Mathf.Sign(Vector3.Cross(dir1, comparePoint - trianglePoint1.position).y);
var cross2 = Mathf.Sign(Vector3.Cross(dir2, comparePoint - trianglePoint2.position).y);
var cross3 = Mathf.Sign(Vector3.Cross(dir3, comparePoint - trianglePoint3.position).y); var dir = Mathf.Approximately(cross1, cross2) ? cross3 : Mathf.Approximately(cross1, cross3) ? cross2 : cross1; cross1 = Mathf.Sign(Vector3.Cross(dir1, comparePoint - trianglePoint1.position).y + -dir * ERROR);
cross2 = Mathf.Sign(Vector3.Cross(dir2, comparePoint - trianglePoint2.position).y + -dir * ERROR);
cross3 = Mathf.Sign(Vector3.Cross(dir3, comparePoint - trianglePoint3.position).y + -dir * ERROR); return Mathf.Approximately(cross1, cross2) && Mathf.Approximately(cross2, cross3);
} void OnDrawGizmos()
{
if (comparePoint == null) return; var oldColor = Gizmos.color; if (IsContract(comparePoint.position))
Gizmos.color = Color.red; Gizmos.DrawLine(trianglePoint1.position, trianglePoint2.position);
Gizmos.DrawLine(trianglePoint2.position, trianglePoint3.position);
Gizmos.DrawLine(trianglePoint3.position, trianglePoint1.position); Gizmos.color = oldColor;
}
}
为了加误差,所以多做了3次差乘。不过使用起来还算可以
2D空间中判断一点是否在三角形内的更多相关文章
- 2D空间中求一点是否在多边形内
参考自这篇博文:http://www.cnblogs.com/dabiaoge/p/4491540.html 一开始没仔细看做法,浪费了不少时间.下面是最终实现的效果: 大致流程: 1.随便选取多边形 ...
- [译]2D空间中使用四叉树Quadtree进行碰撞检测优化
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Unity2017.2.0f3 原文出处 : Quick Tip: Use Quadtrees to Detect Lik ...
- hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)
围困 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 360(138 users) Total Accepted: 157(12 ...
- 【TOJ 3005】Triangle(判断点是否在三角形内+卡精度)
描述 Given the coordinates of the vertices of a triangle,And a point. You just need to judge whether t ...
- 2D空间中求两圆的交点
出处:https://stackoverflow.com/questions/19916880/sphere-sphere-intersection-c-3d-coordinates-of-colli ...
- 2D空间中求线段与圆的交点
出处: https://answers.unity.com/questions/366802/get-intersection-of-a-line-and-a-circle.html 测试脚本(返回值 ...
- 【Leetcode】判断平面中1个点是否落在三角形内
参考资料: 题目: https://blog.csdn.net/dongtinghong/article/details/78657403 符号重载: https://blog.csdn.net/cd ...
- 2D和3D空间中计算两点之间的距离
自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary ...
- opengl剪裁空间和视口空间中不遵从右手定则,而是遵从左手定则
opengl剪裁空间和视口空间中不遵从右手定则,而是遵从左手定则. 比如说要在视口空间判断一个三角形是否是正面朝向用户,就需要用左手定则而非右手定则.
随机推荐
- css设置背景图片
background:pink;图片在背景图上面 background-image:url(food.jpg);一张图片铺满一行background-repeat:repeat-x;同一张图片多张铺满 ...
- git-credential-winstore.exe": No such file or directory
$ git push -u origin master\"D:/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe ...
- Ubuntu 16.04 几个国内更新源
http://blog.csdn.net/paincupid/article/details/52895676 如何更改源 1/ 在修改source.list前,最好先备份一份 执行备份命令 sudo ...
- visio任意角度精确旋转图形
visio 2013 步骤: 选中图形 点击"视图->显示->任务窗格->大小和位置" 然后会出现一个小窗口,里面可以输入图形的旋转角度
- visual studio 2015 + Cordova 开发环境搭建
简单的写一些,备忘,太折腾了,特别是通过代理上网的我们国内的开发者 1.当然是安装Visual Studio 2015,别忘了选择Tools For Apache Cordova. 对于通过Proxy ...
- Golang 逐行读写之scanner.Scan
Go语言实现逐行读的方法多种,本文只介绍Scaner的方法,也是go推荐的方法. 官方文档 例子: file, err := os.Open("filename") if err ...
- 戴尔3542安装ubuntu时出现:failed to lead ldlinux.c32
解决办法: 1. 开机未进入系统是连续敲击F2,进入BIOS2.在 BIOS 的Boot菜单下,将Secure Boot 改为 Disabled3. 将Boot List Option 改为 Lega ...
- activiti 里面各个方法理解
/** Return the intent that started this activity. */public Intent getIntent() { return mIntent;} pub ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- eclipse中Maven创建WEB项目
刚刚学到Maven的时候总是容易忽视到一些创建Maven项目是的步骤, 这里记录笔者熟悉一种,直接创建Maven Project 下面开始吧--- 选择web-app,没得说,然后那些groupID ...