平面中判断点是否在某一三角形内算法 .
2014-03-05 20:44
344 查看
引自:http://blog.csdn.net/wypblog/article/details/7259158
给定平面上一点p(x0,y0),判断该点是否在三角形ABC中,三角形顶点坐标分别为A(xa,xb),B(xb,yb),C(xc,yc)。可以使用面积法来判断,方法如下:其中S(A,B,C)表示三角形ABC的面积。
1、 若abs( S(A,B,C) ) = abs( S(P,B,C) ) + abs( S(A,P,C) ) + abs( S(A,B,P) ) ,则P在三角形ABC的内部或边上;如果还有abs( S(P,B,C) )、abs( S(A,P,C) ) 和abs( S(A,B,P) )全都大于0,则说明P在三角形ABC的内部,否则P在三角形ABC的边上,具体为:S(P,B,C)为0,则说明P在BC边上,S(A,P,C)为0,则说明P在AC边上,S(A,B,P)为0,则说明P在AB边上;
2、 若abs( S(A,B,C) ) < abs( S(P,B,C) ) + abs( S(A,P,C) ) + abs( S(A,B,P) ) ,则P在三角形ABC的外部;
3、 对abs( S(A,B,C) ) > abs( S(P,B,C) ) + abs( S(A,P,C) ) + abs( S(A,B,P) ) 情况在理论上是不存在的
此处又引出另一个问题,如何求平面中三角形的面积?这个可以使用叉乘法来实现,即S(A,B,C)为向量AB叉乘AC所得向量模的1/2,再对该值求绝对值就是三角形ABC的面积。
[cpp]
#include <iostream>
#include <math.h>
using namespace std;
#define ABS_FLOAT_0 0.0001
struct point_float
{
float x;
float y;
};
/**
* @brief 计算三角形面积
*/
float GetTriangleSquar(const point_float pt0, const point_float pt1, const point_float pt2)
{
point_float AB, BC;
AB.x = pt1.x - pt0.x;
AB.y = pt1.y - pt0.y;
BC.x = pt2.x - pt1.x;
BC.y = pt2.y - pt1.y;
return fabs((AB.x * BC.y - AB.y * BC.x)) / 2.0f;
}
/**
* @brief 判断给定一点是否在三角形内或边上
*/
bool IsInTriangle(const point_float A, const point_float B, const point_float C, const point_float D)
{
float SABC, SADB, SBDC, SADC;
SABC = GetTriangleSquar(A, B, C);
SADB = GetTriangleSquar(A, D, B);
SBDC = GetTriangleSquar(B, D, C);
SADC = GetTriangleSquar(A, D, C);
float SumSuqar = SADB + SBDC + SADC;
if ((-ABS_FLOAT_0 < (SABC - SumSuqar)) && ((SABC - SumSuqar) < ABS_FLOAT_0))
{
return true;
}
else
{
return false;
}
}
void main(void)
{
point_float A, B, C, P;
A.x = A.y = 1.0;
B.x = 4.0;
B.y = 1.0;
C.x = 2.0;
C.y = 5.0;
P.x = 3.0;
P.y = 2.0;
if (IsInTriangle(A, B, C, P))
{
cout<<"P is in ABC!"<<endl;
}
else
{
cout<<"P is not in ABC!"<<endl;
}
}
给定平面上一点p(x0,y0),判断该点是否在三角形ABC中,三角形顶点坐标分别为A(xa,xb),B(xb,yb),C(xc,yc)。可以使用面积法来判断,方法如下:其中S(A,B,C)表示三角形ABC的面积。
1、 若abs( S(A,B,C) ) = abs( S(P,B,C) ) + abs( S(A,P,C) ) + abs( S(A,B,P) ) ,则P在三角形ABC的内部或边上;如果还有abs( S(P,B,C) )、abs( S(A,P,C) ) 和abs( S(A,B,P) )全都大于0,则说明P在三角形ABC的内部,否则P在三角形ABC的边上,具体为:S(P,B,C)为0,则说明P在BC边上,S(A,P,C)为0,则说明P在AC边上,S(A,B,P)为0,则说明P在AB边上;
2、 若abs( S(A,B,C) ) < abs( S(P,B,C) ) + abs( S(A,P,C) ) + abs( S(A,B,P) ) ,则P在三角形ABC的外部;
3、 对abs( S(A,B,C) ) > abs( S(P,B,C) ) + abs( S(A,P,C) ) + abs( S(A,B,P) ) 情况在理论上是不存在的
此处又引出另一个问题,如何求平面中三角形的面积?这个可以使用叉乘法来实现,即S(A,B,C)为向量AB叉乘AC所得向量模的1/2,再对该值求绝对值就是三角形ABC的面积。
[cpp]
#include <iostream>
#include <math.h>
using namespace std;
#define ABS_FLOAT_0 0.0001
struct point_float
{
float x;
float y;
};
/**
* @brief 计算三角形面积
*/
float GetTriangleSquar(const point_float pt0, const point_float pt1, const point_float pt2)
{
point_float AB, BC;
AB.x = pt1.x - pt0.x;
AB.y = pt1.y - pt0.y;
BC.x = pt2.x - pt1.x;
BC.y = pt2.y - pt1.y;
return fabs((AB.x * BC.y - AB.y * BC.x)) / 2.0f;
}
/**
* @brief 判断给定一点是否在三角形内或边上
*/
bool IsInTriangle(const point_float A, const point_float B, const point_float C, const point_float D)
{
float SABC, SADB, SBDC, SADC;
SABC = GetTriangleSquar(A, B, C);
SADB = GetTriangleSquar(A, D, B);
SBDC = GetTriangleSquar(B, D, C);
SADC = GetTriangleSquar(A, D, C);
float SumSuqar = SADB + SBDC + SADC;
if ((-ABS_FLOAT_0 < (SABC - SumSuqar)) && ((SABC - SumSuqar) < ABS_FLOAT_0))
{
return true;
}
else
{
return false;
}
}
void main(void)
{
point_float A, B, C, P;
A.x = A.y = 1.0;
B.x = 4.0;
B.y = 1.0;
C.x = 2.0;
C.y = 5.0;
P.x = 3.0;
P.y = 2.0;
if (IsInTriangle(A, B, C, P))
{
cout<<"P is in ABC!"<<endl;
}
else
{
cout<<"P is not in ABC!"<<endl;
}
}
相关文章推荐
- [置顶] 平面中判断点是否在某一三角形内算法 .
- 平面中判断点是否在某一三角形内算法 .
- 2D中判断点是否在某一三角形内算法
- 二维平面上判断点在三角形内的最优算法
- 求判断点是否在三角形内的最佳算法
- 分享自己推导的判断点是否落在三角形中的算法
- 计算几何工具算法-判断点是否在三角形内
- 二维平面上判断点是否在三角形内
- 二维平面上判断点在三角形内的最优算法
- 今天写了一个很简单的判断平面坐标系两线段是否相交的算法
- 二维平面上判断点是否在三角形内
- 【算法】计算机图形学的一些经典小题:判断点在多边形内,随机生成三角形内的点,判断两个矩形是否相交等
- 判断是否是直角三角形的算法研究
- 算法发现~判断点是否在三角形内
- 判断平面上一点是否在三角形内 Inside a triangle or not
- Qt 判断一个点是否落在三角形内(算法)
- 判断2D平面内某点是否在某个旋转的矩形(OBB)内的算法
- 判断三维空间中的一个点是否在三角形内,边上的一种算法
- 判断一个点是否在一个三角形内(平面)
- 【代码向】字符串KMS算法,判断某一字符串是否包含另一字符串