How to check if a point is inside in a rectangle?

I searched and found some arithmetic formula to check if a point is inside in a rectangle or polygon:
http://math.stackexchange.com/questions/190111/how-to-check-if-a-point-is-inside-a-rectangle
http://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/

If the vertexes of rectangle are: FVector2D V1, V2, V3, V4; and the point to check is FVector2D P;
does UE4 have offered API to check it sample?

You need to check if your point is inside the box determined by the four vectors V1, V2, V3 et V4, here is an example from my code:



bool Utilities::IsPointInsideBox(FVector point, UBoxComponent * boxComponent)
{
	float xmin = boxComponent->GetComponentLocation().X - boxComponent->GetScaledBoxExtent().X;
	float xmax = boxComponent->GetComponentLocation().X + boxComponent->GetScaledBoxExtent().X;
	float ymin = boxComponent->GetComponentLocation().Y - boxComponent->GetScaledBoxExtent().Y;
	float ymax = boxComponent->GetComponentLocation().Y + boxComponent->GetScaledBoxExtent().Y;
	float zmin = boxComponent->GetComponentLocation().Z - boxComponent->GetScaledBoxExtent().Z;
	float zmax = boxComponent->GetComponentLocation().Z + boxComponent->GetScaledBoxExtent().Z;

	if ((point.X <= xmax && point.X >= xmin) && (point.Y <= ymax && point.Y >= ymin) && (point.Z <= zmax && point.Z >= zmin))
	{
		return true;
	}
	return false;
}


Hope it helps you.

1 Like

I found it:


FBox2D::IsInside(const FVector2D& TestPoint)
FBox::IsInside(const FVector& TestPoint)
FIntRect::Contains( FIntPoint P )

2 Likes

Many thx! Have nick day!