I have two line segments with points:

Line 1 is a line through $(x_1,y_1)$ and $(x_2,y_2)$ (smaller line)

Line 2 is a line through $(x_3,y_3)$ and $(x_4,y_4)$ (bigger line)

How can I make the Line 1 (smaller) to rotate and make it parallel to Line 2 (bigger) using either:

  1. $(x_1,y_1)$ as fixed point of rotation or
  2. $(x_2,y_2)$ as fixed point of rotation or
  3. center point as fixed point of rotation

Crossposted from StackOverflow.

  • 1
    $\begingroup$ I believe that here is not the best place to ask for this. Maybe here programmers.stackexchange.com $\endgroup$ – Sigur Feb 10 '13 at 14:16
  • $\begingroup$ Code details and implementations are off-topic here, so if you want that, you should try stackoverflow or programmers.se. If you want the math of the rotations, then I think the question should be on-topic once you remove the reference to the programming language. $\endgroup$ – Paresh Feb 10 '13 at 14:53
  • $\begingroup$ Removed programming reference $\endgroup$ – Ankit Sharma Feb 10 '13 at 17:15
  • $\begingroup$ Smaller and bigger are not appropriate terms. $\endgroup$ – Julien Feb 10 '13 at 17:17
  • $\begingroup$ Length of line 2 is greater then length of line 1 $\endgroup$ – Ankit Sharma Feb 11 '13 at 14:04

You can get the angle of each line using $\text{Atan2}(x_2-x_1,y_2-y_1)$ and similar. Note that the result is radians, but you probably don't need to worry about that. Taking the difference of the angles will show how much you have to rotate, call it $\theta$. To rotate $\theta$ around $(x_1,y_1)$ you have $$x'=(x-x_1) \cos \theta+(y-y_1) \sin \theta+x_1\\ y'=(y-y_1) \cos \theta - (x-x_1) \sin \theta + y_1$$

  • $\begingroup$ Wat is $x$ and $y$ in above equations? $\endgroup$ – Ankit Sharma Feb 11 '13 at 4:58
  • $\begingroup$ $x$ and $y$ are any point you want to transform. You might not have a line segment but some more complicated shape. These equations work for any point, given that $(x_1,y_1)$ is the center of rotation. $\endgroup$ – Ross Millikan Feb 11 '13 at 5:14
  • $\begingroup$ link . Kindly check the image. Dashed Line represents the line after rotation. $\endgroup$ – Ankit Sharma Feb 11 '13 at 13:59
  • $\begingroup$ @AnkitSharma: to my eye it looks like it worked if you were trying to rotate around the center of the short line. You can check that the slopes of the two lines agree: if they aren't vertical, see if $\frac {y_4-y-3}{x_4-x_3}=\frac {y_2-y-1}{x_2-x_1}$ to within numeric precision. $\endgroup$ – Ross Millikan Feb 11 '13 at 14:08
  • $\begingroup$ i m checking the slopes of lines only but getting -0.7777778 and 0.999999762 :( $\endgroup$ – Ankit Sharma Feb 11 '13 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.