将坐标转换为旋转坐标系
我正在尝试将旧图形编辑器的旋转和裁剪矩形设置转换为新编辑器,该编辑器使用与旧编辑器不同的坐标系。下图说明了该问题:
所有矩形具有相同的长宽比(例如 3:2),并且所有坐标在边缘上标准化(即在 X 和 Y 方向上从 0 到 1)。
旧程序保存在与绿色矩形(原点位于 A)对齐的坐标系中给定的蓝色矩形 C 的角坐标,以及黄色矩形的旋转角度。
新程序需要蓝色矩形的角点在与黄色矩形对齐的坐标系中的坐标(原点位于 B)。如何实现从旧到新的转变?
这似乎是一个简单的数学问题,但数学课已经很多年了,我无法用笔和纸解决这个问题,也无法搜索这个网站(很多类似的问题,但我找不到完全匹配的问题) ...)
I am trying to convert the rotation and crop rectangle settings of an old graphics editor to new editor which uses different coordinate system than the old one. The following picture illustrates the problem:
All the rectangles have the same aspect ratio (e.g. 3:2), and all the coordinates are normalized across the edges (ie from 0 to 1 in both X and Y direction).
The old program saves the coordinates of corners of the blue rectangle C given in the coordinate system aligned with the green rectangle (with origin at A), and the angle of rotation of yellow rectangle.
The new program needs the coordinates of the corners of the blue rectangle in the coordinate system aligned with the yellow rectangle (with origin at B). How do I do the transformation from old to new?
This seems like a simple math problem, but it has been so many years since the math classes that I could not figure this out with pen-and-paper nor searching this site (many similar questions, but I could not find quite a matching one...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
令
c(0)、c(1)、c(2)、c(3)
为C
的四个角,并令b(0)< /code> 为 B 坐标系所在的角点 B。设 q 为 B 的 x 轴旋转角度。所有这些角度和点必须在同一坐标系中给出。
要查找 B 中的
c(i)
坐标,请将向量c(i) - b(0)
旋转角度q
(或-q
取决于事物的测量方式)。您可以为此使用旋转矩阵。令cq = cos(q)
、sq = sin(q)
和(dx, dy) = c(i) - b(0)
代码>. B 中c(i)
的坐标为令
c = (c(0) + c(2)) / 2
为 C 的中心。令S(s)
为矩阵按s
缩放并让R(q)
是按q
旋转的矩阵。 B 的角由下式给出矩形 A 的角
a(0)、a(1)、a(2)、a(3)
也是已知的。我们希望确定缩放参数s
的最大可能值,使得B的所有点b(i)
都在矩形A内。我认为最安全、最简单的方法这里是考虑相关的
b(i)
和a(i)
对,并为这些对计算最大值s(i, j)
这样如果s = s(i, j)
那么b(i)
位于a(j)
的角区域内。令
a(0)
和a(2)
位于 A 的对角,并令c(0)
和c(1)
是 C 的相邻角。设r(j) = a(j) - c
和d(i) = R(q) * (c(i) - c )
。每个对角线
i
都可以缩放在 B 移出
r(j)
定义的区域之前, 。计算i = 0, 1
和j = 0, 2
的s(i, j)
并令s
是这 4 个值中的最小值。根据
q
的测量方式,您可能需要将q' = atan2(kx * sin(q), ky * cos(q))
应用于q
来解决宽高比问题。Let
c(0), c(1), c(2), c(3)
be the four corners ofC
and letb(0)
be the corner B where B's coordinate system is located. Letq
be the angle of rotation of the x-axis of B. All these angles and points must be given in the same coordinate system.To find the coordinates of
c(i)
in B, rotate the vectorc(i) - b(0)
by the angleq
(or-q
depending on how things are measured). You can use a rotation matrix for this. Letcq = cos(q)
,sq = sin(q)
, and(dx, dy) = c(i) - b(0)
. The coordinates ofc(i)
in B are thenLet
c = (c(0) + c(2)) / 2
be the center of C. LetS(s)
be the matrix that scales bys
and letR(q)
be the matrix that rotates byq
. The corners of B are given byThe corners
a(0), a(1), a(2), a(3)
of the rectangle A are also known. We wish to determine the greatest possible value of the scaling parameters
such that all pointsb(i)
of B are within the rectangle A.I think the safest and simplest approach here is to consider relevant pairs of
b(i)
anda(i)
and for such pairs compute the greatest values(i, j)
such that ifs = s(i, j)
thenb(i)
is within the corner region ofa(j)
.Let
a(0)
anda(2)
be opposite corners of A and letc(0)
andc(1)
be adjacent corners of C. Letr(j) = a(j) - c
andd(i) = R(q) * (c(i) - c)
.Each diagonal
i
can be scaled bybefore B moves outside the region defined by
r(j)
. Computes(i, j)
fori = 0, 1
andj = 0, 2
and lets
be the minimum of those 4 values.Depending on how
q
is measured you may need to apply a transformationq' = atan2(kx * sin(q), ky * cos(q))
toq
to account for issues of aspect ratio.