Click here to Skip to main content
15,889,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
suppose i have Point coordinates and a rectangle(or polygon),how to know whether a point lies inside a rectangle or not?
Posted

1 solution

Yes. Example in WinForms:
CSS
private Rectangle boundRect;

private Region testRegion;

private Point testPoint;

private bool IsInRect;

private bool IsInRegion;

private void TestHitDetection()
{
    boundRect = this.ClientRectangle;

    testRegion = new Region(new Rectangle(50,50, 200,200));

    testPoint = new Point(100,100);

    IsInRect = boundRect.Contains(testPoint);

    IsInRegion = testRegion.IsVisible(testPoint);
}
imho the MS names for the hit-detection operators are very poorly named.
 
Share this answer
 
Comments
Maciej Los 29-Nov-13 2:30am    
+5!
Why the declaration of variables is outside the procedure?
BillWoodruff 29-Nov-13 2:34am    
No earthly reason, just habit/instinct :)
Maciej Los 29-Nov-13 2:44am    
Man is a creature of habit...
I would suggest to change the habits because of several reasons.
Scopes (C#)[^]
Variable and Method Scope in Microsoft .NET[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900