Converting between square and rectangular pixel co-ordinates
I'm new at using transforms and this type of math, and would appreciate some direction solving my coding problem. I'm writing in XCode for the iphone, and am working with CGraphics.
Problem: In Xcode, I want to draw curves, lines and so on it's screen of of square pixels. Then convert those points, as close as possible, into non-square pixel sysem.
For example if the original coordinate system is 500 x 500 pixels that are displayed on square screen of 10 by 10 inchs I draw a round circle with the circle formula. It looks round, and all is well.
Now, I draw the same circle on a second 10 x 10 inch screen that is 850 pixels by 500 pixels. Without changing the coordinates, the same circle formual displays something that looks like an egg.
How can I draw the circle on the second screen in a different coordinate system? And in addition, I need to access the set of p开发者_运维技巧oints x,y system individually.
s
This is a straightforward issue of converting your world coordinates to screen coordinates.
I recognize that the original question was about circles but I'm going to use square in the interest of simpler pseudo-code and math examples.
Consider a simple example where you're going to draw a square that you have modeled as one corner at [0, 0]
and the opposite corner at [1, 1]
in world coordinates. What you would like to see on the first example screen (with pixels that extend from 0 - 500 in each dimension) is one corner at [0, 0]
and the other at [500, 500]
in pixel space.
This is a simple matrix transformation known as a scale matrix. In short form, the scale matrix that you need will have the same scaling factor on each of the diagonal entries and zeroes everywhere else. Written as pseudo code (using *
as the matrix-vector multiplication operation), we can say that:
worldCorner0 = {0, 0};
scaleMatrix = {500, 0,
0, 500};
pixelCorner0 = scaleMatrix * worldCorner0;
worldCorner1 = {1, 1};
pixelCorner1 = scaleMatrix * worldCorner1;
The result will be pixelCorner0 = {0, 0}
and pixelCorner1 = {500, 500}
.
Now, in your other example, you have a non-square screen but you still want to draw your square without distortion. You still need a scale matrix but, if you'd like to see the whole square on the screen, you should continue to scale by the least number of pixels in the screens dimensions. If you used the larger number, the square would extend off the visible area (but would still be square).
In your case, the second screen has a maximum extent of 800 pixels in one dimension and 500 in the other. Conveniently, you already have a scale matrix that scales by 500 pixels. If you draw those pixelCorners described above, you'll preserve your geometry, regardless of your screen dimensions.
You need an affine transformation, and a simple one, just scaling. See Wikipedia under the section Examples in 2D graphics and you want the scaling piece.
精彩评论