MATLAB Answers

0

How to draw orthogonal lines ?

Asked by SUSHMA MB on 14 Aug 2015
Latest activity Commented on by Image Analyst
on 15 Aug 2015
I have two points, one is start and the other is end point. I have connected these two points by a straight line. Now i want to draw orthogonal lines over the line. How can i draw it? I have attached a picture for reference. As shown in the picture, i want eight orthogonal planes placed in equidistant. Please help me with this problem.

  0 Comments

Sign in to comment.

3 Answers

Answer by 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);

  1 Comment

How to draw the orthogonal lines ?

Sign in to comment.


Answer by Mike Garrity
on 14 Aug 2015
Edited by 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

  2 Comments

Mike Garrity
on 14 Aug 2015
Things are a bit more interesting in 3D. I discussed that case on the MATLAB Graphics blog in these two posts ( post1 , post2 ).
Hi...Thank you for the answer. It works. But my points are positive, but why am i getting negative axis. Can i adjust the axis, like i want only positive axis. Like i have points: start_point = [5.95 37.55]; goal_point = [35.62 5.73]; and i want to define the length of the orthogonal lines, i.e., 5 units towards both the side from the point of intersection. Is it possible?

Sign in to comment.


Answer by 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

  3 Comments

Thank you so much...
And how to further divide each of theses orthogonal lines in an equal spacing. I mean i want to further divide the line into an equal spacing of 8, but along the orthogonal lines. And this time i only want the points, not necessary to visualize the points on the orthogonal lines. I hope i made myself clear. Thank you in advance.
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);

Sign in to comment.