gpt4 book ai didi

.net - 重置 GDI+ 转换矩阵中的比例

转载 作者:行者123 更新时间:2023-12-03 19:04:10 25 4
gpt4 key购买 nike

我正在编写一个函数来在 Windows 窗体应用程序的客户区中绘制 UI 句柄(旋转、调整大小等)。选择对象时调用该函数。

调用者向我发送了一个经过适当旋转、缩放和平移以适应更大的事物方案的图形对象(所选对象有自己的旋转/平移/缩放,并且 UI 句柄有相对于所选对象的平移和旋转).现在,我希望我的 UI handle 大小相同,而不管父级(选定对象)的比例如何。

如何消除/重置变换矩阵中的比例因子?如何在保留宝贵的平移和旋转值的同时将比例重置为 1?

最佳答案

Anti-Grain Geometry使用一种基本方法来确定转换的缩放比例(在 agg_trans_affine.cpp 中找到实现)。它通过以下方式实现:

  1. 计算变换的旋转
  2. 复制变换并应用相反的旋转
  3. 变换两个已知点并根据结果计算比例

翻译成 C#,这看起来像:

Matrix transform = (Matrix)graphics.Transform.Clone();

PointF[] rotationPoints = new PointF[] { new PointF(0, 0), new PointF(1, 0) };
transform.TransformPoints(rotationPoints);

double rotationRadians = Math.Atan2(rotationPoints[1].Y - rotationPoints[0].Y, rotationPoints[1].X - rotationPoints[0].X);
transform.Rotate((float)(-rotationRadians * (180.0 / Math.PI)));

PointF[] scalePoints = new PointF[] { new PointF(0, 0), new PointF(1, 1) };
transform.TransformPoints(scalePoints);

float xScale = scalePoints[1].X - scalePoints[0].X;
float yScale = scalePoints[1].Y - scalePoints[0].Y;

AGG 代码还包含一个警告,在退化的情况下这将无法正常工作,但它可能对您的情况有用。

关于.net - 重置 GDI+ 转换矩阵中的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/847019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com