How do I create an NSAffineTransform with a skew effect?

亡梦爱人 提交于 2019-12-12 17:48:11

问题


I'm quite new to drawing in Cocoa, and working on an experimental app involving a hexagonal grid. In order to simplify this, I want to skew the coordinate system so that the Y axis is rotated 30 degrees to the left. I came across this in Apple's Cocoa Drawing Guide, which indicates it is possible:

Combining a non-uniform scaling transform with a rotation transform can also give your content a skewed effect.

However, I cannot understand how this would work, or locate any examples.

How can I set up an NSAffineTransform so that the X axis remains horizontal and the Y axis is rotated counteclockwise by 30 degrees?


回答1:


Unfortunately, there is no easy way to do this as you would with transforms, rotations, etc.

You have to create a transform matrix:

0            tan(skewy)   0
tan(skewx)   0            0
0            0            1

Using setTransformStruct: with an NSAffineTransformStruct:

typedef struct _NSAffineTransformStruct {
  CGFloat m11, m12, m21, m22;
  CGFloat tX, tY;
} NSAffineTransformStruct;

For skewing, m11, m22, tx, and ty are zero; m12 is tan(skewx) and m21 is tan(skewy)

There is much more information on matrices in Apple's documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/Transforms.html#//apple_ref/doc/uid/TP40003290-CH204-BCIIICJI

Hope this helps!



来源:https://stackoverflow.com/questions/12752331/how-do-i-create-an-nsaffinetransform-with-a-skew-effect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!