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

平面上给定一个三角形ABC以及任意点P,判断点P是否是三角形内的点(点P在三角形边上也认为是在三角形内),输入的点坐标都是单精度浮点数。

实现原理参考:http://www.yalewoo.com/in_triangle_test.html

其实现代码如下:

#include <cstdio>
#include <iostream>
#include<vector>
using namespace std;
const float e = 0.000001;

struct Point{
	float x;
	float y;
	Point(float a, float b) :x(a), y(b){ }
};

float cross(const Point &a, const Point &b, const Point &p)
{
	return (b.x - a.x)*(p.y - a.y) - (b.y - a.y)*(p.x - a.x);
}

bool inTriangle(const Point &p, const Point &a, const Point &b, const Point &c)
{
	if (cross(a, b, p) < 0 && cross(b, c, p) < 0 && cross(c, a, p) < 0)
		return true;
	if (cross(a, b, p) > 0 && cross(b, c, p) > 0 && cross(c, a, p) > 0)
		return true;
	if ((cross(a, b, p) - 0.0 > -e) && (cross(a, b, p) - 0.0 < e))
		return true;
	if ((cross(b, c, p) - 0.0 > -e) && (cross(b, c, p) - 0.0 < e))
		return true;
	if ((cross(c, a, p) - 0.0 > -e) && (cross(c, a, p) - 0.0 < e))
		return true;
	return false;
}

int main()
{
	float x;
	vector<float> vec;
	for (int i = 0; i !=8; ++i)
	{
		cin >> x;
		vec.push_back(x);
	}
	Point A(vec[0], vec[1]);
	Point B(vec[2], vec[3]);
	Point C(vec[4], vec[5]);
	Point D(vec[6], vec[7]);
	
	if (inTriangle(D, A, B, C))
		cout <<"Yes"<<endl;
	else
		cout << "No"<<endl;
	system("pause");
	return 0;
}
实验结果如下:



判断一个点是否三角形内部,可以使用向量叉乘的方法。以下是C#的基本代码示例: ```csharp using System; public class Point { public double X { get; set; } public double Y { get; set; } } class Program { static void Main(string[] args) { // 定义三角形三个顶和测试 Point vertexA = new Point { X = 0, Y = 0 }; Point vertexB = new Point { X = 1, Y = 0 }; Point vertexC = new Point { X = 0.5, Y = Math.Sqrt(3) / 2 }; Point testPoint = new Point { X = 0.4, Y = 0.6 }; // 计算三角形边长 double AB = VectorLength(vertexA, vertexB); double BC = VectorLength(vertexB, vertexC); double CA = VectorLength(vertexC, vertexA); // 计算向量AB、BC和AT(从顶A到测试) Point vectorAB = new Point { X = vertexB.X - vertexA.X, Y = vertexB.Y - vertexA.Y }; Point vectorBC = new Point { X = vertexC.X - vertexB.X, Y = vertexC.Y - vertexB.Y }; Point vectorAP = new Point { X = testPoint.X - vertexA.X, Y = testPoint.Y - vertexA.Y }; // 检查P是否在由向量AB和BC构成的平行四边形内,这是三角形的一个边界条件 if (CrossProduct(vectorAB, vectorBC) * CrossProduct(vectorAB, vectorAP) < 0 && CrossProduct(vectorBC, vectorAP) * CrossProduct(vectorBC, vectorAB) < 0) { // P在三角形ABC内 Console.WriteLine("Test point is inside the triangle."); } else { // P不在三角形内 Console.WriteLine("Test point is not inside the triangle."); } Console.ReadKey(); } // 向量长度计算函数 static double VectorLength(Point p1, Point p2) { return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2)); } // 向量叉乘计算函数 static double CrossProduct(Point v1, Point v2) { return v1.X * v2.Y - v1.Y * v2.X; } } ``` 这个代码首先计算了三角形的两边以及从顶A到测试的向量,然后通过检查这三组向量之间的叉乘结果来确定是否三角形内。如果任意两组叉乘结果异号,则说明三角形区域内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值