简体   繁体   English

在旋转、缩放和平移组件中分解 CGAffineTransform

[英]Decomposing CGAffineTransform in rotate, scale, and translate components

I am trying to break CGAffineTransform into rotate, scale, and translate components as follows:我试图将 CGAffineTransform 分解为旋转、缩放和平移组件,如下所示:

public extension CGAffineTransform {
  func rotationRadians() -> CGFloat {
      return atan2(b, a)
  }

   func translation() -> CGPoint {
      return CGPoint(x: tx, y: ty)
   }

   func scaleXY() -> CGPoint {
      let scalex = sqrt(a * a + c * c)
      let scaley = sqrt(d * d + b * b)
      return CGPoint(x: scalex, y: scaley)
    }
}

Now I try this code on random transform by breaking it and then constructing it back from the same components, but the answer never comes out to be same, no matter what order I use in concatenation.现在我通过打破它然后从相同的组件构造它来尝试随机变换这个代码,但答案永远不会相同,无论我在连接中使用什么顺序。 Not sure what I am doing wrong.不知道我做错了什么。

   let randomTransform = __CGAffineTransformMake(2.078, 3.459, 1.676, 0, 591, 397)
   let rotationRadians = randomTransform.rotationRadians()
   let scaleXY = randomTransform.scaleXY()
   let translation = randomTransform.translation()
                
   let translationTransform = CGAffineTransform(translationX: translation.x, y: translation.y)
   let scalingTransform = CGAffineTransform(scaleX: scaleXY.x, y: scaleXY.y)
   let rotationTransform = CGAffineTransform(rotationAngle: rotationRadians)
                
   var newTransform =  rotationTransform.concatenating(scalingTransform).concatenating(translationTransform)
                
   NSLog("New transform \(newTransform), original \(randomTransform)")
             

Here is the output from console:这是控制台的输出:

New transform CGAffineTransform(a: 1.374790977280978, b: 2.965084308709465, c: -2.2884513909600113, d: 1.7812793274062642, tx: 591.0, ty: 397.0), original CGAffineTransform(a: 2.078, b: 3.459, c: 1.676, d: 0.0, tx: 591.0, ty: 397.0)

What you're trying to do is basically impossible, although it might be approachable in some instances using some sophisticated mathematical techniques.您尝试做的事情基本上是不可能的,尽管在某些情况下使用一些复杂的数学技术可能是可以接近的。 See http://callumhay.blogspot.com/2010/10/decomposing-affine-transforms.html for an example.有关示例,请参见http://callumhay.blogspot.com/2010/10/decomposing-affine-transforms.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM