62 views (last 30 days)

blaat
on 14 Aug 2015

The line between your two points can be described by

y = a (x - x1) + b,

where

a = (y2 - y1)/(x2 - x1)

b = y1,

if we call your two points (x1, y1) and (x2, y2). Lines perpendicular to the original line will have a slope of -1/a and can be expressed as:

y = -1/a (x - x0) + y0,

where (x0, y0) is the point on the original line where it intersects the orthogonal line.

Equidistant points on the line can be easily computed using linspace():

num_orth = 8;

x_orth = linspace(x1, x2, num_orth);

y_orth = linspace(y1, y2, num_orth);

Mike Garrity
on 14 Aug 2015

Edited: Mike Garrity
on 14 Aug 2015

Let's say we have the following line:

pt1 = 10*randn(1,2);

pt2 = 10*randn(1,2);

line([pt1(1), pt2(1)],[pt1(2),pt2(2)])

The points where four equally spaced orthogonal lines cross it are:

n = 4;

t = linspace(0,1,n+2); % evenly spaced parameters

t = t(2:(end-1)); % we don't need the start and end points

v = pt2 - pt1;

x = pt1(1) + t*v(1); % p(t) = p1 + t*(p2-p1)

y = pt1(2) + t*v(2);

h = line(x,y);

h.LineStyle = 'none';

h.Marker = 'o';

Next we need to normalize that vector:

delete(h)

v = v / norm(v);

And then we rotate it by 90 degrees. That's just swapping the X & Y components of v, and changing the sign of one:

for i=1:n

line([x(i)+v(2), x(i)-v(2)],[y(i)-v(1), y(i)+v(1)]);

end

The one catch at this point is that the axes might be using different scale factors for the X & Y. This will make the lines look like they're not orthogonal, even if they are mathematically. We can fix this by calling:

axis equal

Mike Garrity
on 14 Aug 2015

I'm afraid I don't understand the question "why am i getting negative axis".

The length of the orthogonal lines goes at the point where I divided v by norm(v). That was to get them to be unit length. Just multiply by the length you want:

start_point = [5.95 37.55]

goal_point = [35.62 5.73]

line([start_point(1), goal_point(1)],[start_point(2), goal_point(2)],'Marker','o')

n = 8;

t = linspace(0,1,n+2);

t = t(2:(end-1));

v = goal_point - start_point;

x = start_point(1) + t*v(1);

y = start_point(2) + t*v(2);

v = 5*v / norm(v);

for i=1:n

line([x(i)+v(2), x(i)-v(2)],[y(i)-v(1), y(i)+v(1)]);

end

axis equal

Image Analyst
on 15 Aug 2015

It's just simple 10th grade algebra. But if you want the MATLAB way, just use linspace():

xEquallySpaced = linspace(x(1), x(2), numPoints);

yEquallySpaced = linspace(y(1), y(2), numPoints);

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

Start Hunting!
## 0 Comments

Sign in to comment.