find whether a point lies within a rectangle in csharp

To check if a point lies within a rectangle, we need to check if the x and y coordinates of the point lie within the x and y coordinates of the rectangle.

Here is the C# code to check if a point p lies within a rectangle specified by its top-left corner rectangleTopLeft and its width and height rectangleWidth and rectangleHeight:

main.cs
public bool IsPointInsideRectangle(PointF p, PointF rectangleTopLeft, float rectangleWidth, float rectangleHeight)
{
    return p.X >= rectangleTopLeft.X && p.X <= rectangleTopLeft.X + rectangleWidth 
           && p.Y >= rectangleTopLeft.Y && p.Y <= rectangleTopLeft.Y + rectangleHeight;   
}
294 chars
6 lines

This function returns true if the point is inside the rectangle and false otherwise. The PointF class is used to represent a point in two-dimensional space with floating-point values for the X and Y coordinates.

gistlibby LogSnag