将坐标转换为旋转坐标系

发布于 2024-11-30 18:09:08 字数 416 浏览 1 评论 0原文

我正在尝试将旧图形编辑器的旋转和裁剪矩形设置转换为新编辑器,该编辑器使用与旧编辑器不同的坐标系。下图说明了该问题:

rectangles illustration

所有矩形具有相同的长宽比(例如 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:

rectangles illustration

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2024-12-07 18:09:08

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) 的坐标为

q 和 (dx , dy)


c = (c(0) + c(2)) / 2 为 C 的中心。令 S(s) 为矩阵按 s 缩放并让R(q) 是按 q 旋转的矩阵。 B 的角由下式给出

b(i) = c + S(s) * R(q) * (c(i) - c)

矩形 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) - cd(i) = R(q) * (c(i) - c )

每个对角线 i 都可以缩放

s(i, j) = min (|r(j).x| / |d(i).x|, |r(j).y| / |d(i).y|)

在 B 移出 r(j) 定义的区域之前, 。计算 i = 0, 1j = 0, 2s(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 of C and let b(0) be the corner B where B's coordinate system is located. Let q 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 vector c(i) - b(0) by the angle q (or -q depending on how things are measured). You can use a rotation matrix for this. Let cq = cos(q), sq = sin(q), and (dx, dy) = c(i) - b(0). The coordinates of c(i) in B are then

Product of the rotation matrix for q and (dx, dy)


Let c = (c(0) + c(2)) / 2 be the center of C. Let S(s) be the matrix that scales by s and let R(q) be the matrix that rotates by q. The corners of B are given by

b(i) = c + S(s) * R(q) * (c(i) - c)

The 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 parameter s such that all points b(i) of B are within the rectangle A.

I think the safest and simplest approach here is to consider relevant pairs of b(i) and a(i) and for such pairs compute the greatest value s(i, j) such that if s = s(i, j) then b(i) is within the corner region of a(j).

Let a(0) and a(2) be opposite corners of A and let c(0) and c(1) be adjacent corners of C. Let r(j) = a(j) - c and d(i) = R(q) * (c(i) - c).

Each diagonal i can be scaled by

s(i, j) = min (|r(j).x| / |d(i).x|, |r(j).y| / |d(i).y|)

before B moves outside the region defined by r(j). Compute s(i, j) for i = 0, 1 and j = 0, 2 and let s be the minimum of those 4 values.


Depending on how q is measured you may need to apply a transformation q' = atan2(kx * sin(q), ky * cos(q)) to q to account for issues of aspect ratio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文