# Two line segments are intersecting… how do I rotate either line about the intersection point?

I have two line segments which are currently intersecting, how do I rotate either intersecting line about the intersection point?

How do I calculate rotation of these lines?

x' = x * cos(theta) - y * sin(theta)
y' = x * sin(theta) + y * cos(theta)


But I believe this is rotation about the origin. So I need to first calculate the point of intersection. I know how to calculate the point of intersection, so let's assume I have my point of intersection.

How do I use my pointOfIntersection variable to rotate a line about this? I can plug in values for x and y, but what is theta? Is theta the angle of intersection?

• You could put a little more effort in this question to begin with (that is, if you're expecting others to make an effort in answering it). For example, provide the corresponding equations, add you thoughts on the problem, show what you've done so far attempting to solve it yourself, etc. – barak manos Mar 16 '15 at 17:06
• I'm not entirely sure but I assume it has to do with adjusting the slope of each line with some delta value? – Kala J Mar 16 '15 at 17:11
• See edit @barakmanos. – Kala J Mar 16 '15 at 17:17
• I interpret this as a request for a general way to rotate around an arbitrary point. I have answered that question, trusting that you will plug in the correct coordinates of the point you want to rotate around. – David K Mar 16 '15 at 17:52
• If the user is "clicking and dragging" with the mouse in order to rotate an image on the screen, then yes, you probably do need to track the mouse coordinates in order to get the angle of rotation. You can use the atan2 function to help you determine the direction from the center of your rotation to the mouse at any instant: the angle is $atan2(y-y_0,x-x_0)$ where $(x,y)$ is the coordinates of the mouse relative to your coordinate system. If the user clicks at one angle and then drags to a different angle, $\theta$ is the difference between those angles. – David K Mar 16 '15 at 19:48

The formula for rotation around a point $(x_0,y_0)$ is

$$x' = (x - x_0) \cos(\theta) - (y - y_0) \sin(\theta) + x_0,$$ $$y' = (x - x_0) \sin(\theta) + (y - y_0) \cos(\theta) + y_0.$$

Conceptually, you translate everything so that the point $(x_0,y_0)$ moves to the origin, then you rotate around the origin, and finally you translate everything so that the origin goes to $(x_0,y_0)$. The effect on $(x_0,y_0)$ is that it is moved to the origin, then back to where it started.

You can use this formula for your problem if you set $(x_0,y_0)$ to the point where the lines intersect and set $\theta$ to the amount of rotation you want.

• I have one more question, point(x,y) in this equation, is that point1 or point2 of the line segment or does it matter at all? I am assuming point1? – Kala J Mar 16 '15 at 19:21
• $(x,y)$ can be any point you want. The rotation takes that point to $(x',y')$. So you could set $(x,y)$ to the coordinates of point1, and compute $(x',y')$, to find the rotated copy of point1; then you can set $(x,y)$ to the coordinates of point2, use the formula to compute new values of $(x',y')$, and those will be the coordinates of the rotated copy of point2. If you want to find what the rotation does to other points, you can just keep plugging in $(x,y)$ coordinates for each point and getting new $(x',y')$ coordinates showing what the rotation does to that point. – David K Mar 16 '15 at 19:40
• Hmm, does my code make sense?: pastebin.com/L0YwvRHn – Kala J Mar 18 '15 at 16:27
• That looks like it's on the right track. If you want to have a click-and-drag interface I think you'll want to keep track of where the mouse was at the "click" event, and you may have to subtract the angle of that point from the angle you're computing for the new mouse position. But that's just the details of what you do to figure out the angle to feed to the rotation; it looks like you have computed the direction to the mouse correctly, and the rotation itself looks like it will do what you want as long as you give it the angle you want. – David K Mar 18 '15 at 19:19
• Ah that might be why the line was not moving. I was only feeding it one point, the current mouse position. I need to update the mouse position and then update the angle... I have to think up how to do that. Hmmm... – Kala J Mar 19 '15 at 12:13