用于查看 2D 绘图的变换矩阵

发布于 2024-12-08 20:17:51 字数 376 浏览 0 评论 0原文

我正在重构一个显示绘图的图形程序。程序使用了自己的变换,代码相当复杂。我想用转换矩阵重构代码。

窗口和视图坐标为 X 向右,Y 向下。

窗口中的绘图使用左上角点作为基点(0,0)。该视图是一个屏幕矩形区域,其基点 (0,0) 也在左上角。

视图区域可以是屏幕上的任意矩形。它显示 80%x80% 区域内的绘图,周围留有一些边距。

绘图的尺寸为宽 x 高。程序应在两个方向上以相同的比例显示绘图。绘图的中心映射到视图区域的中心。绘图可旋转0~360度任意角度。

给定以下数据,一般的变换矩阵是什么:

  1. 绘图的宽度和高度
  2. 旋转角度
  3. 视图区域的宽度和高度
  4. 绘图周围的边距

I am refactoring a graphic program that shows a drawing. The program uses its own transformation and the code is quite complicated. I'd like to refact the code with a transformation matrix instead.

The window and view coordinates are X to the right and Y to downwards.

The drawing in window uses the top left point as base point (0,0). The view is a screen rectangle area with base point (0,0) at the top left point too.

The view area can by any rectangle on the screen. It shows the drawing inside 80%x80% area leaving some margins around.

The drawing has a size of width x height. The program should show the drawing with the same scale ratio at both directions. The center of the drawing is mapping to the center of the view area. the drawing can be rotated in any angle between 0 ~ 360 degrees.

What is the general transformation matrix for this, given the following data:

  1. width and height of the drawing
  2. Rotation angle
  3. width and height of the view area
  4. Margins around the drawings

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

喜你已久 2024-12-15 20:17:51

首先,您需要包含旋转图像的框的宽度/高度

x0 = image_width/2 * cos(angle) + image_height/2 * sin(angle)
x1 = image_width/2 * cos(angle) - image_height/2 * sin(angle)
y0 = image_width/2 * sin(angle) + image_height/2 * cos(angle)
y1 = image_width/2 * sin(angle) - image_height/2 * cos(angle)
rotated_width = max(abs(x0), abs(x1)) * 2
rotated_height = max(abs(y0), abs(y1)) * 2

,然后您需要计算两个维度中的哪一个将是缩放约束,

sf = min((view_width - 2*border) / rotated_width,
         (view_height - 2*border) / rotated_height)

然后您可以计算 3x2 变换矩阵

m11 = sf*cos(angle)
m12 = sf*sin(angle)
m21 = -sf*sin(angle)
m22 = sf*cos(angle)
m31 = view_width/2 - image_width/2 * m11 - image_height/2 * m21
m32 = view_height/2 - image_width/2 * m12 - image_height/2 * m22

First you need the width/height of the box containing the rotated image

x0 = image_width/2 * cos(angle) + image_height/2 * sin(angle)
x1 = image_width/2 * cos(angle) - image_height/2 * sin(angle)
y0 = image_width/2 * sin(angle) + image_height/2 * cos(angle)
y1 = image_width/2 * sin(angle) - image_height/2 * cos(angle)
rotated_width = max(abs(x0), abs(x1)) * 2
rotated_height = max(abs(y0), abs(y1)) * 2

then you need to compute which of the two dimensions will be the scaling constraint

sf = min((view_width - 2*border) / rotated_width,
         (view_height - 2*border) / rotated_height)

then you can compute the 3x2 transformation matrix

m11 = sf*cos(angle)
m12 = sf*sin(angle)
m21 = -sf*sin(angle)
m22 = sf*cos(angle)
m31 = view_width/2 - image_width/2 * m11 - image_height/2 * m21
m32 = view_height/2 - image_width/2 * m12 - image_height/2 * m22
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文