从 CGAffineTransform 中查找比例值

发布于 2024-12-10 00:26:10 字数 456 浏览 1 评论 0原文

好吧,所以我意识到我可以像这样从图层的 CATransform3D 中找到比例值:

 float scale = [[layer valueForKeyPath: @"transform.scale"] floatValue];

但是我一生都无法弄清楚如何从 CGAffineTransform 中找到比例值。举例来说,我有一个名为“cameraTransform”的 CGAffineTransform:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
CGAffineTransform *cameraTransform =  imagePicker.cameraViewTransform;

现在如何从cameraTransform 获取比例值?

Ok, so I realize I can find the scale value from a layer's CATransform3D like this:

 float scale = [[layer valueForKeyPath: @"transform.scale"] floatValue];

But I can't for the life of me figure out how I would find the scale value from a CGAffineTransform. Say for instance I have this CGAffineTransform called "cameraTransform":

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
CGAffineTransform *cameraTransform =  imagePicker.cameraViewTransform;

Now how do I get the scale value from cameraTransform?

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

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

发布评论

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

评论(3

时间海 2024-12-17 00:26:10

我尝试为所有类型的 CGAffineTransforms(甚至是旋转的 CGAffineTransforms)给出一个通用答案。

假设您的 CGAffineTransform 包含(可选)

  • 旋转
  • 平移
  • 缩放

并且

  • 没有倾斜

,那么有一个通用公式可以为您提供比例因子:

CGAffineTransform transform = ...;
CGFloat scaleFactor = sqrt(fabs(transform.a * transform.d - transform.b * transform.c));

“镜像”或翻转坐标方向将被忽略,这意味着 (x --> -x; y - -> y) 将导致scaleFactor == 1 而不是-1。

I try to give a general answer for all kinds of CGAffineTransforms, even rotated ones.

Assuming your CGAffineTransform contains (optionally)

  • rotation
  • translation
  • scaling

and

  • NO skew

then the there’s a general formula that gives you the scale factor:

CGAffineTransform transform = ...;
CGFloat scaleFactor = sqrt(fabs(transform.a * transform.d - transform.b * transform.c));

"Mirroring" or flipping coordinate directions will be ignored, that means (x --> -x; y --> y) will result in scaleFactor == 1 instead of -1.

娇女薄笑 2024-12-17 00:26:10

http://en.wikipedia.org/wiki/Determinant

“可以给出几何解释具有实数项的方阵的行列式的值:行列式的绝对值给出了在相关线性变换下乘以面积或体积的比例因子,而其符号表示变换是否保留方向,因此是 2 × 。 2矩阵行列式 -2,当应用于具有有限面积的平面区域时,会将该区域转换为面积两倍的区域,同时反转其方向。”

本文接着给出了 3x3 矩阵和 2x2 矩阵的行列式公式。 CGAffineTransforms 是 3x3 矩阵,但它们的右列始终为 0 0 1。结果是行列式将等于矩阵的 2x2 左上角正方形的行列式。因此,您可以使用结构体中的值并自行计算比例。

http://en.wikipedia.org/wiki/Determinant

"A geometric interpretation can be given to the value of the determinant of a square matrix with real entries: the absolute value of the determinant gives the scale factor by which area or volume is multiplied under the associated linear transformation, while its sign indicates whether the transformation preserves orientation. Thus a 2 × 2 matrix with determinant −2, when applied to a region of the plane with finite area, will transform that region into one with twice the area, while reversing its orientation."

The article goes on to give formulas for the determinant of a 3x3 matrix and a 2x2 matrix. CGAffineTransforms are 3x3 matrices, but their right column is always 0 0 1. The result is the determinant will be equal to the determinant of the 2x2 upper left square of the matrix. So you can use the values from the struct and compute the scale yourself.

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