Core Graphics conversion, coregraphics Graphics
Simple Coordinate Transformation:
Quarzd 2D provides three APIs for Coordinate Transformation:
// 1. Shift transformation CGContextTranslateCTM (CGContextRef c, CGFloat tx, CGFloat ty );
For the transformed coordinate system, the origin is located at (tx, ty) of the original coordinate system. It is also equivalent to adding tx and ty to the horizontal and vertical coordinates of all points in the original coordinate system.
// 2. rotating and transforming CGContextRotateCTM (CGContextRef c, CGFloat angle );
Assume that Point A1 is changed to A2 after coordinate transformation, and the coordinate origin is O. A1O = A2O (A2 is in the circle with O as the center and A1O length as the radius) and A1OA2 = angel. The rotation here is clockwise. If you want to rotate it counterclockwise, use-angel.
// 3. Scale and transform CGContextScaleCTM (CGContextRef c, CGFloat sx, CGFloat sy );
The abscissa coordinates of all points in the new coordinate system are equal to the original abscissa coordinates multiplied by the scaling factor sx and sy, respectively.
Note that all the transformation functions apply to context. However, unlike the preceding modification of the drawing settings, these Apis modify the state of the coordinate system and cannot restore the settings even if the CGContextDrawPath method is called.
Storage and recovery of coordinate system status
We can call this method to save and restore the system status.
CGContextSaveGState (CGContextRef c); // Save the current drawing status. CGContextRestoreGState (CGContextRef c); // restore the system drawing status.
A drawing context maintains the state of a drawing system stack. When the context is initialized, this stack is empty. Therefore, you must call the CGContextSaveGState method to import the current system status to the stack and then call CGContextRestoreGState to exit the stack when it needs to be restored. The State contains not only the status of the current coordinate system, but also the filled style, line style, shadow style, and other States. It should be noted that if the API for coordinate transformation is called before the State is saved, it will no longer be restored. If you try to call the CGContextRestoreGState method at this time, the stack method will be executed for an empty stack, resulting in an error.
* Matrix transformation
In addition to the preceding three transformation methods, Quartz 2D also provides a more general method to process coordinate transformation using matrices.
CGContextConcatCTM (CGContextRef c, CGAffineTransform transform); // Transform is a transformation matrix-(CGAffineTransform) CGContextGetCTM (CGContextRef c
The transformation matrix CGAffineTransform transform can be created as follows:
CGAffineTransform transform1 = CGAffineTransformMakeRotation(CGFloat angle);CGAffineTransform transform2 = CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);CGAffineTransform transform3 = CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty);
These three methods correspond to the rotating, scaling, and shifting APIs respectively, so they are not explained.
You can also create a more general transformation matrix:
CGAffineTransform transform = CGAffineTransformMake(CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty);
According to the calculation rules of the matrix and the transformation law of the shift transformation, the left side of the vertex a' after point A (x, y) undergoes the transform matrix should be (ax + cy + tx, bx + dy + ty ).
Based on the knowledge of High School Mathematics, several common transformation matrices are obtained:
CGAffineTransformMake (-1, 0, 0, 1, 0, 0); // about the X axis symmetric transformation CGAffineTransformMake (1, 0, 0,-1, 0, 0 ); // about the Y-axis symmetric transformation CGAffineTransformMake (0, 1, 1, 0, 0); // about the Y = X symmetric transformation CGAffineTransformMake (0,-1,-1, 0, 0, 0); // about the X axis symmetric transformation CGAffineTransformMake (1, 0, tan (α), 1, 0, 0 ); // horizontal tilt angle α ° CGAffineTransformMake (1,-tan (α), 0, 1, 0, 0); // vertical tilt angle α °
You can also modify existing matrices.
Transform (gradient t, CGFloat sx, CGFloat sy); Round (CGAffineTransform t, CGFloat angle); Round (CGAffineTransform t, CGFloat tx, CGFloat ty); // append the translation transformation effect, the preceding two methods are similar to except (CGAffineTransform t). // obtain the inverse CGAffineTransformConcat (CGAffineTransform t1, CGAffineTransform t2) of a transformation matrix.
The transformation matrix can act on not only the coordinate system of the context, but also CGPoint, CGSize, and CGRect. In addition, UIView has a transform attribute and supports setting the transformation matrix to perform coordinate transformation on the UI control itself.
CGPoint newPoint = CGPointApplyAffineTransform(CGPoint point, CGAffineTransform t);CGSize newSize = CGSizeApplyAffineTransform(CGSize size, CGAffineTransform t);CGRect newRect = CGRectApplyAffineTransform(CGRect rect, CGAffineTransform t);