Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
How can we find the Height and width of a polygon which is formed with some points.

Eg:on the window each mouse click gives a point after some clicks if i click on a button the mouse click locations will join and form a shape.
Till this it is fine. iam able to calculate the center point, but how we can find the height and width of the formed shape.

any one can give me a example for better understanding

Thanks,
Posted

Everytime you set a point to the screen via a mouse click you'll have to take note of the following.


  1. Xmin = int.MaxValue;
    Xmax = int.MinValue;

    Ymin = int.MaxValue;
    Ymax = int.MinValue;
  2. If the currently set X coordinate is smaller than than Xmin, set that to X.
    If the currently set X coordinate is greater than than Xmax, set that to X.
    If the currently set Y coordinate is smaller than than Ymin, set that to Y.
    If the currently set Y coordinate is greater than than Ymax, set that to Y.
  3. When you finally press the button to draw your polygon, the bounding rectangle is defined by these coordinates:

    Upper left corner:  ( Xmin, Ymax )
    Upper right corner: ( Xmax, Ymax )
    Lower left corner:  ( Xmin, Ymin )
    Lower right corner: ( Xmax, Ymin )
    Height: Ymax - Ymin
    Width:  Xmax - Xmin



Best Regards,

—MRB
 
Share this answer
 
v2
Comments
KiranBabu M 30-Aug-11 7:17am    
good enough to understand..
thanks
Manfred Rudolf Bihy 30-Aug-11 7:34am    
Good enough, what's that supposed to mean?
Add all your points to a System.Drawing.Drawing2D.GraphicsPath, then you are able to call its BoundingBox property. It is of type RectangleF and therefore exposes all co-ordinates a developer can ever want to know.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 30-Aug-11 6:37am    
Elegant solution! 5+
KiranBabu M 26-Sep-11 5:45am    
Thanks for the solution

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