Js math atan2 cos sin
1. Give the relative coordinate center o, p1 to o distance l, p1 to X axis angle (0 ~ 2PI)
left:x0+Math.cos(angle)top: y0+Math.sin(angle)
2. The relative coordinate center o and p1 coordinates are given, and the angle between the X axis of p1 is obtained.
// Angle greater than-Math. PI, less than Math. PI // (-linear, linear) var angle = astan2 (y1-y0, x1-x0 );
Case:
Vczly + bXxbDatq + 4 + tfF16q2r6GjPC9zdHJvbmc + PC9wPg0KPHByZSBjbGFzcz0 = "brush: java;">/* Knowledge point: the angle from which the axis is switched is equal to the angle from which the cube is rotated. Obtain angle x, y */var canvas, contxt, canvas = document. getElementById ('mycanvas '); contxt = canvas. getContext (2d); var drawWall = function () {contxt. save (); contxt. strokeStyle = #000; contxt. lineWidth = 5; contxt. beginPath (); contxt. moveTo (100,0); contxt. lineTo () contxt. stroke (); contxt. closePath (); contxt. restore () ;}; var drawLine = function (x, y) {contxt. save (); contxt. strokeStyle = #000; contxt. lineWidth = 2; contxt. beginPath (); contxt. moveTo (); contxt. lineTo (300 + x, y) contxt. stroke (); contxt. closePath (); contxt. restore () ;}; var drawRect = function (x, y, angle) {contxt. save (); contxt. fillStyle = # f00; contxt. translate (300 + x, y); contxt. rotate (angle + Math. PI/2); contxt. rect (-100,-50,200,100); contxt. fill (); contxt. restore () ;}; var angle, increase, width, height, x, y, radius; angle = Math. PI/3; increase = Math. PI/3/200; width = 200; height = 100; radius = 300; var onframe = function () {contxt. clearRect (0,0, 800,600); drawWall (); x = radius * Math. cos (angle); y = radius * Math. sin (angle); drawLine (x, y); drawRect (x, y, angle); if (angle> (Math. PI/3*2) {increase =-increase;} else if (angle