By default, a GDI+ function like DrawLine uses a coordinate system where the origin is in the top left corner and the units are pixels.
So DrawLine(0, 0, 640, 480) would draw a diagonal line from the top left to the bottom right of a 640x480 screen. How do I set up a Graphics object so that the origin is in the center and both axes have a range of [-1, 1]?
You don't, that's how graphical coordinates work. That's how they work everywhere in computers and you just get used to it. You can always perform a simple transform:
Code:
Point ToLogicalCartesian(Point graphical, int maxHeight)
{
return new Point(graphical.X, maxHeight - graphical.Y);
}
Of course, not sure why you would need to. As far as the -1,1 domain, bitmap coordinates are integers, so you can't really. Why in the world would you need to? You can always normalize your data as you see fit.
to get the conversion, but I thought GDI+ had a better way of doing it, though a matrix or something.
I'm writing a software renderer, so I have to convert 3d points to 2d points on the screen, and in that scenario bitmap coordinates are not intuitive at all and [-1, 1] are.
and from now on the Graphics' origin is in the center. I was hoping there was a way to also scale the coordinates using something similar to get [-1, 1] without having to call a separate function.
There is the System.Drawing.Drawin2D.Matrix class, but I have never used it for much more than simple image transforms (rotation and color). I'm not sure if you could use it to suit your needs here, but it's something to look into.
BTW, the Drawing2D namesapce holds the more advanced GDI+ classes and functions.
EDIT: Hehe, I see you found that. Yeah, I can't be of much more help here, I have never done what you are attempting to do. Are you opposed to looking into the DirectX API? It may be better suited for what you are doing, but it of course comes with its own learning curve.
This isn't a serious project. The goal is to create a very simple renderer in C# using only GDI+ for drawing lines or filling in triangles, so no Direct3d this time.
At least using the Matrix transformation simplifies things a bit. Instead of:
This is how in Visual Basic.NET to get the Cartesian coordinates with Translate and Scale:
Dim graphicsObj As Graphics
' move the origin to the lower left (position it inside Picture Box = Picture Height + border from the top).
graphicsObj.TranslateTransform(0.0F, PictureHeight +20)
'In MSDN the following operation is called - reflect acros X-axis (http://msdn.microsoft.com/en-us/library/8667dchf.aspx)
graphicsObj.ScaleTransform(1, -1)
' Scales an element by the specified ScaleX and ScaleY amounts
' multiplying the transformation matrix by a diagonal matrix whose elements are (1,-1).
Bookmarks