Points in triangles, circles, rectangles

Keywords: Algorithm Machine Learning linear algebra

1, Round

Judge the distance from the point to the center of the circle, and then compare the distance with the radius of the circle. Less than is on the inside of the circle, equal to the edge of the re circle, greater than the outside of the re circle.

bool CScenes::IsPointInCir(CPoint point)//Point is a point from the outside
{
	int dis,x,y;
	x = point.x - m_cirVertex.x;//m_cirVertex is the center coordinate of the circle
	y = point.y - m_cirVertex.y;
	dis = sqrt(pow(x,2) + pow(y,2));
	if(dis <= 40)
	{
		return true;
	}
	return false;
}

2, Triangle

Use the area method to judge. Given a point p(x0,y0) on the plane, judge whether the point is in the triangle ABC. The triangle vertex coordinates are a (XA, ya), B (XB, Yb) and C (XC, YC). The area method can be used to judge as follows: where S(A,B,C) represents the area of triangle ABC.

If ABS (s (a, B, c)) = ABS (S(P,B,C)) + ABS (S(A,P,C)) + ABS (S(A,B,P)), then p is inside or on the edge of the triangle ABC; If ABS (S(P,B,C)), ABS (S(A,P,C)) and ABS (S(A,B,P)) are all greater than 0, it means that P is inside the triangle ABC, otherwise P is on the side of the triangle ABC. Specifically, if S(P,B,C) is 0, it means that P is on the BC side, S(A,P,C) is 0, it means that P is on the AC side, and S(A,B,P) is 0, it means that P is on the AB side;
If ABS (s (a, B, c)) < ABS (s (P, B, c)) + ABS (s (a, P, c)) + ABS (s (a, B, P)), P is outside the triangle ABC;
The case of ABS (s (a, B, c)) > ABS (s (P, B, c)) + ABS (s (a, P, c)) + ABS (s (a, B, P)) does not exist in theory.
Here comes another question, how to find the area of a triangle in a plane? This can be achieved by cross multiplication, that is, S(A,B,C) is 1 / 2 of the vector module obtained by cross multiplying AC by vector AB, and then finding the absolute value of this value is the area of triangle ABC.

bool CScenes::IsPointInTir(CPoint point)//Point is a point from the outside
{
	int tirArea = CalTirArea(m_tirVertex[0],m_tirVertex[1],m_tirVertex[2]);//m_tirVertex is an array of triangle vertex coordinates
	int splitTirArea = CalTirArea(m_tirVertex[0],m_tirVertex[1],point) + CalTirArea(m_tirVertex[0],point,m_tirVertex[2]) + CalTirArea(point,m_tirVertex[1],m_tirVertex[2]);
	if(abs(tirArea - splitTirArea) < 10)//Less than 10 is to reduce the error and make the effect more obvious
	{
	    return true;
	}
	return false;
}
int CScenes::CalTirArea(CPoint a,CPoint b,CPoint c)//Calculate the area of the triangle
{
    int x1 = b.x - a.x;  
    int y1 = b.y - a.y;  
    int x2 = c.x - a.x;  
    int y2 = c.y - a.y;  
    return abs(x1*y2-x2*y1)/2.0;  
}

3, Rectangle

The vector method is used to solve the problem. The points inside the convex polygon are on the same side of the vector where the edge of the convex polygon is located (provided that the vector where the edge is located is calculated in the same direction, both clockwise and counterclockwise), and the re product is used to solve the problem.

Suppose that the four vertices of the quadrilateral are a (x1, Y1) B (2,2), (x3,3), (x4, Y4) in turn, and the point to be judged is P(x,y). If the point P is inside the quadrilateral, the vector AB * AP (Note: 1. This is to find the cross product; 2.AB and AP are vectors, which is equal to the values of (x2-x1) * (y-y1)-(y2-y1) * (x-x1)) and BCBP, CD CP and DA * DP have the same sign (if it is equal to zero, it means that P is on the edge, which can be regarded as internal or external according to your preference), that is, if the four values are both positive or negative, point P is inside ABCD, otherwise it is outside.

bool CScenes::IsPointInRect(CPoint point)//Point is a point from the outside
{
	CPoint A = m_rectVertex[3];//Lower left vertex
	CPoint B = m_rectVertex[0];//Top left vertex
	CPoint C = m_rectVertex[1];//Top right vertex
	CPoint D = m_rectVertex[2];//Lower right vertex

	int a = (B.x - A.x) * (point.y - A.y) - (B.y - A.y) * (point.x - A.x);
	int b = (C.x - B.x) * (point.y - B.y) - (C.y - B.y) * (point.x - B.x);
	int c = (D.x - C.x) * (point.y - C.y) - (D.y - C.y) * (point.x - C.x);
	int d = (A.x - D.x) * (point.y - D.y) - (A.y - D.y) * (point.x - D.x);
	
	if((a > 0 && b > 0 && c > 0 && d > 0) || (a < 0 && b < 0 && c < 0 && d < 0))
	{
		return true;
	}
	return false;
}

Posted by shivani.shm on Wed, 13 Oct 2021 22:46:42 -0700