# Angle between two line segments

Here's a question? I have two line segments which have an angle between them. For the line segments, I know coordinates of their points. I need to be able to rotate one segment for one angle (for example, $\alpha$ needs to be $60^\circ$) - it doesn't matter in which direction is angle calculated, as long as it stays persistent. Because that I only know point of line segment, idea is to rotate other point around point of intersection for desired angle, and to get coordinates for that point after rotation. But I can not come to solution which works for every angle and where rotation direction (clockwise or counter-clockwise) is persistent. $(0, 0)$ is in top left corner, as shown on picture. You don't have to bother with theory (if you don't want), I need to apply this as algorithm. Thanks The usual rotation matrix approach works. To rotate by an angle $\theta$, you have $x'=x \cos \theta - y \sin \theta, y'=y \cos \theta + x \sin \theta$ If you apply it to all the points, the angles between lines will stay the same.

• that is not working. If you use that formula, it will rotate point around 0.0, and not point of intersection. I have also tried to ad x,y coordinates of intersection points as x′=xcosθ−ysinθ + x(intersection) and it didnt worked. Also, you have to take in account angle that is already between two lines. Point is, that if for example user enters 45, alfa becomes 45 by rotating one of the lines, and rotation of the line is done as rotation of one point of the line around intersection point(not one in which is intersection), because drawing of the line is based on point coordinates. – Nenad Milosavljevic Nov 23 '13 at 14:46
• edit: after reading your answer once more, i think that you maybe misunderstood the question - point is that user is able to enter alfa in degrees, not for it to stay same after rotation of one line. – Nenad Milosavljevic Nov 23 '13 at 14:49
• How about this? Leave the tip of the angle and one segment fixed. Use the tip of the angle as the origin of rotation $x_0,y_0)$. The other point on one segment is $(x_1,y_1)$. Now $(x_2,y_2)=(x_0+(x_1-x_0)\cos \alpha +(y_1-y_0)\sin \alpha,y_0+(y_1-y_0)\cos \alpha +(x_1+x_0)\sin \alpha)$ is a point on the other leg. – Ross Millikan Nov 23 '13 at 16:00
• Hello, first of all sorry for long waiting for an answer, I was on vacation. Unfortunately, this is not working. Instead of "rotating" point, what happens is that point is moved for some angle (not one that is specified), and line segment length is altered. Only example where algorithm is kind of working is when static point is aligned with one of the axis. Any suggestions? I will illustrate with screenshots tomorrow. – Nenad Milosavljevic Dec 8 '13 at 3:35

Ok, I have found solution for my problem (here's solution in objective c): first of all, I'm getting length of line segment which needs to be rotated. Then i'm calculating angle between static line segment and intersection point - or axis (second angle in code). Afterwards, I'm adding second angle to desired angle, and rotating point of line segment around intersection point for sum of two angles (that's because i need angle to be set between two line segments, not between one segment and axis). Angle will alway be calculated in clockwise direction from static point.

-(CGPoint)rotateWallaroundPoint:(CGPoint)rotateAroundP pointToRotate:(CGPoint)pToRotate staticPoint:(CGPoint)staticP forAngle:(CGFloat)angle opposite:(bool)opp {
CGFloat l;
l = sqrtf( (pToRotate.x - rotateAroundP.x) *(pToRotate.x - rotateAroundP.x) + (pToRotate.y - rotateAroundP.y) * (pToRotate.y - rotateAroundP.y));
CGPoint rotatedPoint;
CGFloat secondAngle;
secondAngle = atan2f(staticPoint.y - rotateAroundP.y,staticPoint.x - rotateAroundP.x);
angle = angle + secondAngle;
rotatedPoint.x = l * cosf(angle) + rotateAroundP.x;
rotatedPoint.y = l * sinf(angle) + rotateAroundP.y;
return rotatedPoint;