需要有人验证我的数学是否正确
晚上的大部分时间我都拿着铅笔坐着,试图回忆如何实现可以导航 2D 区域的可扩展视口。自从我第一次听说它以来已经有一段时间了,但我想我已经弄清楚了,我只需要验证一下。
我们有一个带有“经典”笛卡尔坐标系的 2D 世界,x 轴指向右侧,y 轴指向顶部。
在世界区域中,我们有一个由 2 个点 Pmin 和 Pmax 定义的矩形视口,其中: Pmin(xmin, ymin), Pmax(xmax, ymax)。这些点定义了视口的大小、位置和比例。
在世界区域中,我们有一个点 P,其中 Pmin P(x,y)< Pmax。 (P 在视口矩形中)
为了显示整个该死的东西,我们有一个画布(例如),它具有“更改的”坐标系,x 轴指向右侧,y 轴指向下方。画布的尺寸为MaxX 和MaxY。画布的大小是固定的。
现在,为了在画布中显示点 P'(x', y'),我需要计算它的位置,如下所示:
x' = (x - xmin) * Sx,其中
Sx = MaxX / (xmax - xmin)
y' = MaxY - (y - ymin) * Sy
,其中 Sy = MaxY / (ymax - ymin)
*请注意,由于画布的坐标系,y'坐标是反转的。
换句话说:上面的数学应该在考虑比例和视口位置的同时显示一个点。我说得对吗?如果不是,请证明我错了。
I've been sitting with a pencil for the better part of the evening trying to recall how to implement a scalable viewport that can navigate a 2D area. It's been a while since I've first heard of it, but I think I've figured it out, I just need to verify.
We have a 2D world with a "classic" cartesian coordinate system, x-axis points to the right, y-axis points to the top.
In the world area we have a rectangular viewport defined by 2 points Pmin and Pmax, where :
Pmin(xmin, ymin), Pmax(xmax, ymax). Those points define viewport's size, location and scale
In the world area we have a point P, where Pmin < P(x, y) < Pmax. (P is in the viewport rect)
To display the whole damn thing, we've got a canvas (for example) that has an "altered" coordinate system, x-axis points right, y-axis points down. The canvas's size is MaxX and MaxY. The canvas's size is fixed.
Now, in order to display point P'(x', y') in the canvas I need to calculate it's position like this :
x' = (x - xmin) * Sx
, where Sx = MaxX / (xmax - xmin)
y' = MaxY - (y - ymin) * Sy
, where Sy = MaxY / (ymax - ymin)
*please note that y' coord is inverted due to canvas's coordinate system
In other words : the above math should take care of displaying a point while taking scale and vieport's position into account. Am I correct ? If not, please prove me wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是正确的。视口中的所有点都将显示在画布上 - 并且仅显示这些点 - 并且所有内容都将正面朝上显示,并保留距离。
Yes, that is correct. All points in the viewport will appear on the canvas -- and only those points -- and everything will appear right-side-up with distances preserved.
您可能会发现为管理比例和范围的视口创建一个类很有用。它可以具有诸如
和 相反的方法:
如果您在具有视口坐标的视点中选择一个点并希望转换为世界,这非常有用。
You may find it useful to create a class for the viewport which manages the scale and ranges. It can have methods such as
and the inverse:
This is useful if you pick a point in the viewpoint with viewport coordinates and wish to transform to world.