How to rotate a vertex around a certain point?

Imagine that you have two points in a 2d space, and you need to rotate one of these points by X degrees, and the other point acts as a center.

float distX = Math.abs( centerX -point2X ); float distY = Math.abs( centerY -point2Y ); float dist = FloatMath.sqrt( distX*distX + distY*distY ); 

So far, I just had to find the distance between two points ... any ideas where I should come from this?

enter image description here

+47
java math geometry
Aug 28 2018-12-12T00:
source share
6 answers

The easiest way is to make three transformations:

  • Translation that translates point 1 to the beginning
  • Rotation around the origin at the desired angle
  • A translation that returns point 1 to its original position

When you are done, you will get the following conversion:

 newX = centerX + (point2x-centerX)*Math.cos(x) - (point2y-centerY)*Math.sin(x); newY = centerY + (point2x-centerX)*Math.sin(x) + (point2y-centerY)*Math.cos(x); 

Note that this makes the assumption that the angle x negative for clockwise rotation (the so-called standard or right orientation for the coordinate system). If this is not the case, then you will need to change sign in terms with sin(x) .

+52
Aug 28 2018-12-12T00:
source share

You need a 2-stroke rotation matrix http://en.wikipedia.org/wiki/Rotation_matrix

Your new point will be

  newX = centerX + ( cosX * (point2X-centerX) + sinX * (point2Y -centerY)) newY = centerY + ( -sinX * (point2X-centerX) + cosX * (point2Y -centerY)) 

because you rotate clockwise and not counterclockwise

+16
Aug 28 '12 at 14:23
source share

Assuming you are using the Java Graphics2D API try this code -

  Point2D result = new Point2D.Double(); AffineTransform rotation = new AffineTransform(); double angleInRadians = (angle * Math.PI / 180); rotation.rotate(angleInRadians, pivot.getX(), pivot.getY()); rotation.transform(point, result); return result; 

where the pivot point is the point around which you rotate.

+8
Aug 28 2018-12-12T00:
source share
  • Translate "1" by 0.0

  • Rotate

    x = sin (angle) * r; y = cos (angle) * r;

  • Translate back

+1
Aug 28 2018-12-12T00:
source share

Here's a way to rotate any point around any other point in 2D. Note that in 3D this can be used as rotation around the z axis, the z-coordinate of the received point, since it does not change. Rotation around the X axis and Y axis in 3D can also be easily implemented.

The code is in JavaScript. The numbered lines at the beginning are a set of tests for the function. They also serve as an example of use.

 //A = new Array(0,0) //S = new Array(-1,0) //fi = 90 //alert("rotujBod: " + rotatePoint(A, S, fi)) function rotatePoint(A, S, fi) { /** IN points A - rotated point, S - centre, fi - angle of rotation (rad) * points in format [Ax, Ay, Az], angle fi (float) * OUT point B */ r = Math.sqrt((A[0] - S[0])*(A[0] - S[0]) + (A[1] - S[1])*(A[1] - S[1])) originOfRotation = new Array(S[0] + r, S[1]) if (A[1] < S[1]) { A2 = new Array(A[0], -1*A[1]) originalAngle = -1*sizeOfAngle(originOfRotation, S, A2) } else { originalAngle = sizeOfAngle(originOfRotation, S, A) } x = S[0] + r*Math.cos(fi + originalAngle) y = S[1] + r*Math.sin(fi + originalAngle) B = new Array(x, y) return(B) } function sizeOfAngle(A, S, B) { ux = A[0] - S[0] uy = A[1] - S[1] vx = B[0] - S[0] vy = B[1] - S[1] if((Math.sqrt(ux*ux + uy*uy)*Math.sqrt(vx*vx + vy*vy)) == 0) {return 0} return Math.acos((ux*vx + uy*vy)/(Math.sqrt(ux*ux + uy*uy)*Math.sqrt(vx*vx + vy*vy))) } 
0
Nov 07 '15 at 18:12
source share

Here's a version that takes care of the direction of rotation. The right (clockwise) is negative, and the left (counterclockwise) is positive. You can send a point or 2nd vector and set its primitives in this method (last line) to avoid memory allocation for performance. You may need to replace vector2 and mathutils with the libraries you use, or with the built-in java class, and you can use math.toradians () instead of mathutils.

 /** * rotates the point around a center and returns the new point * @param cx x coordinate of the center * @param cy y coordinate of the center * @param angle in degrees (sign determines the direction + is counter-clockwise - is clockwise) * @param px x coordinate of point to rotate * @param py y coordinate of point to rotate * */ public static Vector2 rotate_point(float cx,float cy,float angle,float px,float py){ float absangl=Math.abs(angle); float s = MathUtils.sin(absangl * MathUtils.degreesToRadians); float c = MathUtils.cos(absangl * MathUtils.degreesToRadians); // translate point back to origin: px -= cx; py -= cy; // rotate point float xnew; float ynew; if (angle > 0) { xnew = px * c - py * s; ynew = px * s + py * c; } else { xnew = px * c + py * s; ynew = -px * s + py * c; } // translate point back: px = xnew + cx; py = ynew + cy; return new Vector2(px, py); } 

Please note that this method has better performance than the one you tried to use in your message. Because you are using sqrt, which is very expensive and thus converts from degrees to radians, managed using a lookup table if you are wondering. And so it has a very high performance.

0
May 31 '17 at 8:48
source share



All Articles