在直角坐标和屏幕坐标之间转换 [英] Translating between cartesian and screen coordinates

查看:107
本文介绍了在直角坐标和屏幕坐标之间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的游戏,我需要在两个坐标系之间进行转换的函数.好吧,这主要是数学问题,但我需要的是C ++代码来完成此工作,并提供一些有关如何解决我的问题的解释.

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.

萤幕伴娘:

a)左上角为0,0

a) top left corner is 0,0

b)没有减值

c)右边+ = x(x值越大,右边的点越多)

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

d)底部+ = y

d) bottom +=y

笛卡尔2D坐标:

a)中间点是(0,0)

a) middle point is (0, 0)

b)确实存在负值

c)右+ = x

d)底部-= y(y越小,底部的点越多)

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

我需要一种简单的方法来从一个系统转换到另一个系统,反之亦然.为此,(我认为)我需要一些知识,例如将(0,0)[屏幕坐标的左上角]放置在笛卡尔坐标中的位置.

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.

但是,存在一个问题,即在将笛卡尔坐标中的某个点转换为屏幕坐标之后,屏幕坐标中的位置可能为负,这是无稽之谈.我不能将屏幕坐标的左上角放在(-inifity,+ infinity)笛卡尔坐标中...

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...

我该如何解决?我能想到的唯一解决方案是将屏幕(0,0)放置在笛卡尔(0,0)中,并且仅使用笛卡尔系统的四分之一,但是在这种情况下,使用笛卡尔系统是没有意义的...

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.

推荐答案

从笛卡尔坐标转换为屏幕坐标的基本算法是

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

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

但是正如您提到的,笛卡尔空间是无限的,而屏幕空间不是.通过更改屏幕空间和笛卡尔空间之间的分辨率,可以轻松解决此问题.上述算法使笛卡尔空间中的1单位=屏幕空间中的1单位/像素.如果允许其他比例,则可以缩小"或放大屏幕空间以覆盖所有必要的笛卡尔空间.

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.

这会将上述算法更改为

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

现在,您可以通过修改缩放系数,直到所有笛卡尔坐标都适合屏幕,来处理负(或过大)的screenX和screenY.

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.

您还可以允许平移坐标空间,这意味着允许笛卡尔空间的中心偏离屏幕的中心.这也可能有助于使zoom_factor保持尽可能紧密,但也可以拟合在笛卡尔空间原点周围分布不均匀的数据.

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.

这会将算法更改为

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

这篇关于在直角坐标和屏幕坐标之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆