Cgaffinetransform Correlation function

Source: Internet
Author: User

this article reprinted to http://www.cnblogs.com/pengyingh/articles/2378732.htmlCoreGraphics.h

Cgaffinetransform rotation = cgaffinetransformmakerotation (m_pi_2);? [xxx settransform:rotation];? Oh just so simple two lines of code can be realized!
By the way to record some constants, later use!
#define M_e         2.71828182845904523536028747135266250    e? #define m_log2e     1.44269504088896340735992468100189214    log 2e? #define M_log10e    0.434294481903251827651128918916605082  log 10e?# Define M_ln2       0.693147180559945309417232121458176568  log e2?# Define M_ln10      2.30258509299404568401799145468436421   log E10? #define M_pi        3.14159265358979323846264338327950288    pi? #define m_pi_2      1.57079632679489661923132169163975144    pi/2? #define m_pi_4      0.785398163397448309615660845819875721   Pi/4? #define M_1_pi      0.318309886183790671537767526745028724  1/pi?# Define M_2_pi      0.636619772367581343075535053490057448  2/pi? #define M_2_ Sqrtpi  1.12837916709551257389615890312154517   2/sqrt (PI)? #define M_SQRT2      1.41421356237309504880168872420969808   SQRT (2)? #define M_SQRT1_2    0.707106781186547524400844362104849039  1/SQRT (2)


from:http://donbe.blog.163.com/blog/static/138048021201061054243442/?? Cgaffinetransformmaketranslation (width, 0.0); Is it a change of position?

Cgaffinetransformrotate (transform, M_PI);

Cgaffinetransformmakerotation (-M_PI), also rotating?

Transform = Cgaffinetransformscale (transform,-1.0, 1.0);

View.transform = cgaffinetransformidentity; The matrix transformation in the linear algebra, this is the identity transformation??? When you change a View.transform property or view.layer.transform You need to restore the default state, remember to reset them to use

View.transform = cgaffinetransformidentity,

or View.layer.transform = catransform3didentity,

Assuming you've been changing the properties of a view.transform all the time, and you haven't reset them before each change, you'll find that later changes and changes you want are not what you really want.
Quartz Conversion Implementation principle: Quartz the drawing into two parts,
User space, which is device-independent,
Device Space,
There is a transformation matrix in the middle of user space and device space: CTM
The essence of this chapter is to explain CTM

3 features offered by quartz
Move, rotate, zoom

As shown below, first load a picture
void Cgcontextdrawimage (
Cgcontextref C,
CGRect Rect,
Cgimageref image
);





Move function
CGCONTEXTTRANSLATECTM (Mycontext, 100, 50);



Rotation function
Include <math.h>
Static inline double radians (double degrees) {return degrees * m_pi/180;}
CGCONTEXTROTATECTM (Mycontext, radians (–45.));



Scaling
CGCONTEXTSCALECTM (Mycontext,. 5,. 75);



Flip, two conversion effects, first move the picture to the upper right corner, and then rotate 180 degrees
CGCONTEXTTRANSLATECTM (Mycontext, w,h);
CGCONTEXTROTATECTM (Mycontext, radians ( -180.));



Combine several actions
CGCONTEXTTRANSLATECTM (mycontext, W/4, 0);
CGCONTEXTSCALECTM (Mycontext,. 25,. 5);
CGCONTEXTROTATECTM (Mycontext, radians (22.));





CGCONTEXTROTATECTM (Mycontext, radians (22.));
CGCONTEXTSCALECTM (Mycontext,. 25,. 5);
CGCONTEXTTRANSLATECTM (mycontext, W/4, 0);




The above is achieved by directly modifying the current CTM to achieve 3 large effects, the following is achieved by creating affine Transforms, and then connecting CTM to achieve the same 3 kinds of effects
The advantage of this is that you can reuse this affine Transforms
Apply affine Transforms to CTM functions
void Cgcontextconcatctm (
Cgcontextref C,
Cgaffinetransform Transform
);


Creating affine Transforms
Move effect
Cgaffinetransform Cgaffinetransformmaketranslation (
CGFloat TX,
CGFloat ty
);

Cgaffinetransform Cgaffinetransformtranslate (
Cgaffinetransform T,
CGFloat TX,
CGFloat ty
);

Rotation effect
Cgaffinetransform Cgaffinetransformmakerotation (
CGFloat angle
);

Cgaffinetransform Cgaffinetransformrotate (
Cgaffinetransform T,
CGFloat angle
);

Zoom effect
Cgaffinetransform Cgaffinetransformmakescale (
CGFloat SX,
CGFloat Sy
);

Cgaffinetransform Cgaffinetransformscale (
Cgaffinetransform T,
CGFloat SX,
CGFloat Sy
);

Invert effect
Cgaffinetransform Cgaffinetransforminvert (
Cgaffinetransform T
);

Effect only on Local
CGRect Cgrectapplyaffinetransform (
CGRect Rect,
Cgaffinetransform T
);

Determine if two Affinetrans are equal
BOOL Cgaffinetransformequaltotransform (
Cgaffinetransform T1,
Cgaffinetransform T2
);



Get affine Transform
Cgaffinetransform Cgcontextgetuserspacetodevicespacetransform (
Cgcontextref C
);

The following function only plays a viewing effect, such as looking at the point of the user space, and converting to the device space to coordinate the number of
Cgpoint Cgcontextconvertpointtodevicespace (
Cgcontextref C,
Cgpoint Point
);

Cgpoint Cgcontextconvertpointtouserspace (
Cgcontextref C,
Cgpoint Point
);

Cgsize Cgcontextconvertsizetodevicespace (
Cgcontextref C,
Cgsize size
);

Cgsize Cgcontextconvertsizetouserspace (
Cgcontextref C,
Cgsize size
);

CGRect Cgcontextconvertrecttodevicespace (
Cgcontextref C,
CGRect rect
);

CGRect Cgcontextconvertrecttouserspace (
Cgcontextref C,
CGRect rect
);


CTM's Real Mathematical behavior
This transformation matrix is actually a 3x3 proof
Such as


The following examples illustrate the mathematical implementation of several conversion operations
X y is the coordinates of the original point
The following is a calculation formula for converting from user coordinates to device coordinates




The following is an identity matrix, which is what coordinates are entered, what coordinates come out, no conversions

The result of the final calculation is x=x,y=y,


You can use the function to determine if the matrix is an identity matrix
BOOL Cgaffinetransformisidentity (
Cgaffinetransform T
);




Reference: http://developer.apple.com/library/ios/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_ Affine/dq_affine.html








-(void) Willanimatefirsthalfofrotationtointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (nstimeinterval) duration
{


if (tointerfaceorientation = = uiinterfaceorientationportrait)
{
B=yes;

SELF.VIEW=MAINVV;
Self.view.transform = cgaffinetransformidentity;
Self.view.transform = cgaffinetransformmakerotation (Degreestoradian (0));
Self.view.bounds = CGRectMake (0.0, 0.0, 768.0, 1004.0);

}
else if (tointerfaceorientation = = Uiinterfaceorientationlandscapeleft)
{
B=no;

Self.view = SELF.VV;
Self.view.transform = cgaffinetransformidentity;
Self.view.transform = Cgaffinetransformmakerotation (Degreestoradian (-90));
Self.view.bounds = CGRectMake (0.0, 0.0, 1024.0, 748.0);



}
else if (tointerfaceorientation = = Uiinterfaceorientationportraitupsidedown)
{

B=yes;
SELF.VIEW=MAINVV;
Self.view.transform = cgaffinetransformidentity;
Self.view.transform = Cgaffinetransformmakerotation (Degreestoradian (180));
Self.view.bounds = CGRectMake (0.0, 0.0, 768.0, 1004.0);

}
else if (tointerfaceorientation = = uiinterfaceorientationlandscaperight)
{

B=no;
Self.view = SELF.VV;
Self.view.transform = cgaffinetransformidentity;
Self.view.transform = Cgaffinetransformmakerotation (Degreestoradian (90));
Self.view.bounds = CGRectMake (0.0, 0.0, 1024.0, 748.0);

}


}


3

Quartz Conversion Implementation principle: Quartz the drawing into two parts,
User space, which is device-independent,
Device Space,
There is a transformation matrix in the middle of user space and device space: CTM
The essence of this chapter is to explain CTM

3 features offered by quartz
Move, rotate, zoom

As shown below, first load a picture
void Cgcontextdrawimage (
Cgcontextref C,
CGRect Rect,
Cgimageref image
);





Move function
CGCONTEXTTRANSLATECTM (Mycontext, 100, 50);




Rotation function
Include <math.h>
Static inline double radians (double degrees) {return degrees * m_pi/180;}
CGCONTEXTROTATECTM (Mycontext, radians (–45.));



Scaling
CGCONTEXTSCALECTM (Mycontext,. 5,. 75);



Flip, two conversion effects, first move the picture to the upper right corner, and then rotate 180 degrees
CGCONTEXTTRANSLATECTM (Mycontext, w,h);
CGCONTEXTROTATECTM (Mycontext, radians ( -180.));



Combine several actions
CGCONTEXTTRANSLATECTM (mycontext, W/4, 0);
CGCONTEXTSCALECTM (Mycontext,. 25,. 5);
CGCONTEXTROTATECTM (Mycontext, radians (22.));





CGCONTEXTROTATECTM (Mycontext, radians (22.));
CGCONTEXTSCALECTM (Mycontext,. 25,. 5);
CGCONTEXTTRANSLATECTM (mycontext, W/4, 0);




The above is achieved by directly modifying the current CTM to achieve 3 large effects, the following is achieved by creating affine Transforms, and then connecting CTM to achieve the same 3 kinds of effects
The advantage of this is that you can reuse this affine Transforms
Apply affine Transforms to CTM functions
void Cgcontextconcatctm (
Cgcontextref C,
Cgaffinetransform Transform
);


Creating affine Transforms
Move effect
Cgaffinetransform Cgaffinetransformmaketranslation (
CGFloat TX,
CGFloat ty
);

Cgaffinetransform Cgaffinetransformtranslate (
Cgaffinetransform T,
CGFloat TX,
CGFloat ty
);

Rotation effect
Cgaffinetransform Cgaffinetransformmakerotation (
CGFloat angle
);

Cgaffinetransform Cgaffinetransformrotate (
Cgaffinetransform T,
CGFloat angle
);

Zoom effect
Cgaffinetransform Cgaffinetransformmakescale (
CGFloat SX,
CGFloat Sy
);

Cgaffinetransform Cgaffinetransformscale (
Cgaffinetransform T,
CGFloat SX,
CGFloat Sy
);

Invert effect
Cgaffinetransform Cgaffinetransforminvert (
Cgaffinetransform T
);

Effect only on Local
CGRect Cgrectapplyaffinetransform (
CGRect Rect,
Cgaffinetransform T
);

Determine if two Affinetrans are equal
BOOL Cgaffinetransformequaltotransform (
Cgaffinetransform T1,
Cgaffinetransform T2
);



Get affine Transform
Cgaffinetransform Cgcontextgetuserspacetodevicespacetransform (
Cgcontextref C
);

The following function only plays a viewing effect, such as looking at the point of the user space, and converting to the device space to coordinate the number of
Cgpoint Cgcontextconvertpointtodevicespace (
Cgcontextref C,
Cgpoint Point
);

Cgpoint Cgcontextconvertpointtouserspace (
Cgcontextref C,
Cgpoint Point
);

Cgsize Cgcontextconvertsizetodevicespace (
Cgcontextref C,
Cgsize size
);

Cgsize Cgcontextconvertsizetouserspace (
Cgcontextref C,
Cgsize size
);

CGRect Cgcontextconvertrecttodevicespace (
Cgcontextref C,
CGRect rect
);

CGRect Cgcontextconvertrecttouserspace (
Cgcontextref C,
CGRect rect
);


CTM's Real Mathematical behavior
This transformation matrix is actually a 3x3 proof
Such as


The following examples illustrate the mathematical implementation of several conversion operations
X y is the coordinates of the original point
The following is a calculation formula for converting from user coordinates to device coordinates




The following is an identity matrix, which is what coordinates are entered, what coordinates come out, no conversions

The result of the final calculation is x=x,y=y,


You can use the function to determine if the matrix is an identity matrix
BOOL Cgaffinetransformisidentity (
Cgaffinetransform T
);


Move matrix




Scaling matrix



Rotation matrix



Rotate plus Move matrix

Cgaffinetransform Correlation function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.