Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating between cartesian and screen coordinates

For my game I need functions to translate between two coordinate systems. Well it's mainly math question but what I need is the C++ code to do it and a bit of explanation how to solve my issue.

Screen coordiantes:

a) top left corner is 0,0

b) no minus values

c) right += x (the more is x value, the more on the right is point)

d) bottom +=y

Cartesian 2D coordinates:

a) middle point is (0, 0)

b) minus values do exist

c) right += x

d) bottom -= y (the less is y, the more at the bottom is point)

I need an easy way to translate from one system to another and vice versa. To do that, (I think) I need some knowledge like where is the (0, 0) [top left corner in screen coordinates] placed in the cartesian coordinates.

However there is a problem that for some point in cartesian coordinates after translating it to screen ones, the position in screen coordinates may be minus, which is a nonsense. I cant put top left corner of screen coordinates in (-inifity, +infinity) cartesian coords...

How can I solve this? The only solution I can think of is to place screen (0, 0) in cartesian (0, 0) and only use IV quarter of cartesian system, but in that case using cartesian system is pointless...

I'm sure there are ways for translating screen coordinates into cartesian coordinates and vice versa, but I'm doing something wrong in my thinking with that minus values.

like image 577
user1873947 Avatar asked Feb 14 '13 17:02

user1873947


People also ask

How do you convert coordinates into Cartesian coordinates?

To convert a point from cylindrical coordinates to Cartesian coordinates, use equations x=rcosθ,y=rsinθ, and z=z. To convert a point from Cartesian coordinates to cylindrical coordinates, use equations r2=x2+y2,tanθ=yx, and z=z.

What is the difference between XY and Z coordinates?

Usually, the x-coordinate is measured along the eastwest axis, the y-coordinate is measured along the northsouth axis, and the z-coordinate measures height or elevation.

What is the difference between Cartesian and rectangular coordinates?

Cartesian coordinates, also called rectangular coordinates, provide a method of rendering graphs and indicating the positions of points on a two-dimensional (2D) surface or in three-dimensional (3D) space.

Which method is used in screen coordinate system?

Explanation: The coordinate system of the screen is a Cartesian coordinate system. The origin (0,0) is at the top left of the screen. Point is denoted by (x,y), where x is x co-ordinate and y is y co-ordinate.


1 Answers

The basic algorithm to translate from cartesian coordinates to screen coordinates are

screenX = cartX + screen_width/2
screenY = screen_height/2 - cartY

But as you mentioned, cartesian space is infinite, and your screen space is not. This can be solved easily by changing the resolution between screen space and cartesian space. The above algorithm makes 1 unit in cartesian space = 1 unit/pixel in screen space. If you allow for other ratios, you can "zoom" out or in your screen space to cover all of the cartesian space necessary.

This would change the above algorithm to

screenX = zoom_factor*cartX + screen_width/2
screenY = screen_height/2 - zoom_factor*cartY

Now you handle negative (or overly large) screenX and screenY by modifying your zoom factor until all your cartesian coordinates will fit on the screen.

You could also allow for panning of the coordinate space too, meaning, allowing the center of cartesian space to be off-center of the screen. This could also help in allowing your zoom_factor to stay as tight as possible but also fit data which isn't evenly distributed around the origin of cartesian space.

This would change the algorithm to

screenX = zoom_factor*cartX + screen_width/2 + offsetX
screenY = screen_height/2 - zoom_factor*cartY + offsetY
like image 198
MerickOWA Avatar answered Oct 17 '22 01:10

MerickOWA