tags:

views:

506

answers:

4

I have an array of Point variables. When drawn using Graphics.Drawline, they create the expected image. My problem is that 0,0 is actually the center of the image (not the top left of my canvas as expected. My X and Y coordinates in the Points can contain negative numbers.

When I try to draw this to my Image, of course I get 1/4 of the total image as the remainder is drawn outside the bounds of my canvas. How do I center this drawing correctly onto my canvas?

I know the dimensions of the image I want to draw. I know where 0,0 is (width / 2, height / 2).

I suppose I can interpret each and every single Point, but that seems like the hard way to do this. Any help is greatly appreciated.

A: 

When creating the array, add an offset to each x value equal to half of the width and an offset to y equal to half of the height. That way when the points are drawn, they're in the expected position.

Michael Todd
+1  A: 

Welcome to the Windows' version of the Cartessian Plane. Your last statement is correct. You do have to offset each and every point. The only real help you can give yourself is to make the offset logic a separate method to clean up your main drawing code.

Paul Sasik
TranslateTransform() cannot help in this case? Thank you
GDIHelp
+2  A: 

TranslateTransform() can map coordinates for you if you setup a transformation during your drawing handlers.

Graphics.TranslateTransform @ MSDN

Or, map your coordinates by adding half the width and half the height of the desired viewing area to each coordinate.

Also, you may need to scale your coordinates. You may use Graphics.ScaleTransform to do this.

Graphics.ScaleTransform @ MSDN

If you don't wish to use this, then you should divide X coordinates by the percent amount you wish to stretch the width, and divide Y coordinates by the percent amount you wish to stretch the height. This gives us 1 for 100%, 1.2 for 120%, 0.8 for 80%, etc.

meklarian
+1 : I've used the Graphics class so many times, and often done some manual extents calculations etc, not even realising that these methods were available. Very very useful.
Ian
A: 

This might help?

http://www.bobpowell.net/mappingmodes.htm

Dylan