In the natural state, the coordinate system is the origin point in the upper-left corner of the screen, the right x positive axis, and the y-axis downward. Now you want to pan the origin of the coordinate system to any point O (x, y) and rotate a angle, how do I do it?
Explain my problem background, the known screen has two points P1 and P2, forming a straight line L. I'm going to point mid (X, y) at two to the origin of the coordinates, the perpendicular bisector of the segment L is an axis, L is another axis, and a coordinate system. Cut out a square with a side length of D. As shown below:
Double d = math.sqrt ((p2.x-p1.x) * (p2.x-p1.x) + (P2.Y-P1.Y) * (P2.Y-P1.Y)); Distance between P1 and P2 two points
float k = Math.Abs ((p2.y-p1.y)/(p2.x-p1.x)); Slope
Double angle = Math.atan (k); Note that the range of this angle is 0----------PI/2, not 0 to 90°
Point midpoint = new Point ((p1.x+p2.x)/2, (P1.Y+P2.Y)/2); Find Midpoint
/********** drawing a new coordinate system ***********/
Canvas.translate (Midpoint.x, MIDPOINT.Y); Pan the coordinate center to the midpoint
Canvas.rotate ((float) (90-math.todegrees (angle)));
Paint Paint2 = new paint ();
int dd = D; Suppose the square side length to be made is a distance between two points D
In the rotated coordinate system, the lower-right vertex and upper-left vertex coordinates of the square with the edge length DD are (0, D/2), (-D,-D/2), respectively
Canvas.drawrect (New Rect (0, D/2,-D,-D/2), Paint2);
1, first pan to the new origin, and then rotate.
2,rotate () This function should note that the parameter is regular clockwise, otherwise counterclockwise. This parameter must be a degree, such as rotation 90 °, fill 90, can not fill pi/2. This piece, tangled up for the most of the day I. You can use Math.todegrees () and Math.toradians () to convert degrees and radians to each other.
3, Inverse trigonometric function Math.atan () is not the number of degrees, so to convert. Grandpa's, wasted me an afternoon.
For canvas rotation related, reference:
I am also a foggy about whether the canvas rotates only after it has rotated or rotated the coordinates. After a personal test, the coordinate system is rotated anyway.
Transferred from: http://blog.csdn.net/yanzi1225627/article/details/8266135
Android Canvas rotate (): Pan rotation coordinate system to any point of Origin-------attached: Android Inverse trigonometric Function Summary