判断一个点是否在三角形区域内

时间:2021-08-30 10:24:36
	float TriangleArea (float v0x, float v0y, float v1x, float v1y, float v2x, float v2y)
	{
		return Mathf.Abs ((v0x * v1y + v1x * v2y + v2x * v0y
			- v1x * v0y - v2x * v1y - v0x * v2y) / 2f);
	}
	public bool IsINTriangle (Vector3 point, Vector3 v0, Vector3 v1, Vector3 v2)
	{
		float x = point.x;
		float y = point.z;

		float v0x = v0.x;
		float v0y = v0.z;

		float v1x = v1.x;
		float v1y = v1.z;

		float v2x = v2.x;
		float v2y = v2.z;

		float t = TriangleArea (v0x, v0y, v1x, v1y, v2x, v2y);
		float a = TriangleArea (v0x, v0y, v1x, v1y, x, y) + TriangleArea (v0x, v0y, x, y, v2x, v2y) + TriangleArea (x, y, v1x, v1y, v2x, v2y);

		if (Mathf.Abs (t - a) <= 0.01f) {
			return true;
		} else {
			return false;
		}
	}