# angle between two lines

18 views (last 30 days)

Show older comments

I have the follwoing geometry:

All the blues and whites are knowns. The reds are unknown and need to be determined (specifically speaking θ).

Any help would be appreicted.

Thanks.

##### 0 Comments

### Answers (1)

LO
on 2 Jun 2021

Edited: LO
on 2 Jun 2021

FIRST you get the angle between white and blue (it does not matter which angle, the procedure is the same, you just have to adjust the rotation later on)

METHOD1:

create vectors using points belonging to either white or blue lines

vector1 = [x2,y2,0] - [x1,y1,0];

vector2 = [x3,y3,0] - [x1,y1,0];

Theta = atan2d(norm(cross(vector1, vector2)), dot(vector1, vector2));

METHOD2 (less robust):

poly1=polyfit(x_values_blue,y_values_blue,1); % get coefficient of line passing through the points you have in one line

x=-20:20; % or another range, doesn't matter

y1=poly1(1)*x+poly1(2); % calculate line based on the coefficients found

% repeat the same for the white line

poly2=polyfit(x_values_white,y_values_white,1);

y2=poly2(1)*x+poly2(2);

% here you have the two slopes, calculate the difference to get their angle

beta=atand(poly1(1));

theta=atand(poly2(1));

SECOND you need to create a rotated vector using the angle you just found (to create the red lines)

% define the x- and y-data for the original line we would like to rotate

x = L1_x_values;

y = L1_y_values;

% create a matrix of these points, which will be useful in future calculations

v = [x;y];

% choose a point which will be the center of rotation (in this case the

% origin)

x_center = 0;

y_center = 0;

% create a rotation matrix

center = repmat([x_center; y_center], 1, length(x));

% define a counter-clockwise rotation matrix

theta = Theta ; % the angle you found previously in degrees, using the first method

theta = theta ; % angle measured using the second method, just comment the one you won't use

R = [cos(theta) -sin(theta); sin(theta) cos(theta)];

% rotate

s = v - center; % shift points in the plane so that the center of rotation is at the origin

so = R*s; % apply the rotation about the origin

vo = so + center; % shift again so the origin goes back to the desired center of rotation

% pick out the vectors of rotated x- and y-data

x_rotated = vo(1,:);

y_rotated = vo(2,:);

##### 1 Comment

LO
on 3 Jun 2021

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!