Segment a line to sections and add to it perpendicular line
2 views (last 30 days)
Show older comments
clc;
Based on this superb question and answers https://www.mathworks.com/matlabcentral/answers/484422-how-to-find-the-coordinates-of-a-point-perpendicular-to-a-line-knowing-the-distance I add a new variable that will help me to segment the line into sections (numOfSegment).
When I set the numOfSegment to 2 it works well but when I add a bigger number (e.g., 4) it plots the perpendicular line out of the line. Why? and how can I plot all the perpendicular lines and store their coordinates?
Code:
clc;
clear all;
close all;
A = [0 100]; %[x,y]
B = [12,180]; %[x,y]
Clen = 25; % distance off the line AB that C will lie
% Call AB the vector that points in the direction
% from A to B.
AB = B - A;
% Normalize AB to have unit length
AB = AB/norm(AB);
% compute the perpendicular vector to the line
% because AB had unit norm, so will ABperp
ABperp = AB*[0 -1;1 0];
%%%%%% numOfSegment is set to 2;
numOfSegment = 2;
ABmid = (A + B)/numOfSegment;
% Compute new points C and D, each at a ditance
% Clen off the line. Note that since ABperp is
% a vector with unit eEuclidean norm, if I
% multiply it by Clen, then it has length Clen.
C = ABmid + Clen*ABperp;
D = ABmid - Clen*ABperp;
% plot them all
subplot(1,2,1)
plot([A(1);B(1)],[A(2);B(2)],'-bo',[C(1);D(1)],[C(2);D(2)],'-rs')
axis equal
%%%%%% numOfSegment is set to 4
numOfSegment = 4;
ABmid = (A + B)/numOfSegment;
% Compute new points C and D, each at a ditance
% Clen off the line. Note that since ABperp is
% a vector with unit eEuclidean norm, if I
% multiply it by Clen, then it has length Clen.
C = ABmid + Clen*ABperp;
D = ABmid - Clen*ABperp;
% plot them all
subplot(1,2,2)
plot([A(1);B(1)],[A(2);B(2)],'-bo',[C(1);D(1)],[C(2);D(2)],'-rs')
axis equal
0 Comments
Answers (0)
See Also
Categories
Find more on Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!