Android Canvas rotate (): Pan rotation coordinate system to any point of Origin-------attached: Android Inverse trigonometric Function Summary

Source: Internet
Author: User

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.save ();
Canvas.translate (Midpoint.x, MIDPOINT.Y); Pan the coordinate center to the midpoint
Canvas.rotate ((float) (90-math.todegrees (angle)));

Paint Paint2 = new paint ();
Paint2.setantialias (TRUE);
Paint2.setstyle (Style.stroke);
Paint2.setcolor (Color.Blue);

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);

Canvas.restore ();

Summarize:

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:

http://blog.csdn.net/dinko321/article/details/7679019
Http://www.cnblogs.com/-cyb/archive/2011/10/30/2229162.html
Http://bbs.189works.com/thread-38386-1-1.html

Http://www.myexception.cn/h/540302.html

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

Related Article

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.