[Math] Algorithm to rotate a line

geometryrotations

I have a program I'm writing that requires a line drawn in SVG to be rotatable, and I'm having trouble with the math behind that. If I were to have a line at the beginning that has a start point of (40, 40) and an endpoint of (40, 240), and I want to be able to rotate that line in increments of one degree without changing the length of the line, what math would I need to look into? I'm familiar with basic slope and y-intercept math, but I'm not sure how to predict the replots of the start and end points along a rotation.

Best Answer

The canonical approach is using a rotation matrix to rotate the two endpoints of the line segment. The Wolfram article I linked to describes rotation about the origin. If you wish to rotate about another point, you need to:

  1. Translate the points so that the rotation point is coincident with the origin
  2. Rotate the geometry
  3. Translate the rotation point back to it's original position.

This is handled typically using a 4x4 (or 4x3) matrix in computer graphics.

Related Question