codeaviator
codeaviator

Reputation: 2565

Find the center of a set of graphics

I have a form with a panel where a set of complex graphics are painted (represented by blue circles in the image):

enter image description here

The graphics are painted using DrawLine and DrawEllipse methods of the Graphics class.

I need to find the global center of the set of graphics (yellow point on the image) so that I can apply some transformations afterwards, like centering the set of graphics on the panel.

The most direct workaround I can think of is finding the boundaries of the graphics, so they are contained in a rectangular frame (red box on the image) and then divide the frame's width and height by two.

But how can I achieve this? Is there a method that would allow me to find the top, bottom, left and right boundaries of the entire set of graphics?

Upvotes: 0

Views: 1182

Answers (1)

George Legeza
George Legeza

Reputation: 36

I'm not aware of any method in Graphics to do what you're looking for, but rolling your own should be relatively easy assuming you know the dimensions of each component image. The various DrawEllipse methods each take a bounding rectangle in some form, so you've already got all of the information that you need.

The most straightforward approach would be to simply keep track of the farthest left, right, top, and bottom bound of each image as you draw it and then form the complete bounding rectangle from those dimensions. You'll also need to account for the width of the Pen that you're using to draw your images.

As an alternative, you might want to take a look at the GraphicsPath class. GraphicsPath.GetBounds would more or less give you what you're looking for. If GraphicsPath doesn't work for you, then I think you're probably stuck writing your own code.

Upvotes: 1

Related Questions