# How to draw orthogonal lines ?

132 ビュー (過去 30 日間)
SUSHMA MB 2015 年 8 月 14 日
コメント済み: Image Analyst 2015 年 8 月 15 日
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.

サインインしてコメントする。

### 回答 (3 件)

blaat 2015 年 8 月 14 日
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 件のコメント表示非表示 なし
SUSHMA MB 2015 年 8 月 14 日
How to draw the orthogonal lines ?

サインインしてコメントする。

Mike Garrity 2015 年 8 月 14 日

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 件のコメント表示非表示 1 件の古いコメント
SUSHMA MB 2015 年 8 月 14 日
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?

サインインしてコメントする。

Mike Garrity 2015 年 8 月 14 日
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 件のコメント表示非表示 2 件の古いコメント
Image Analyst 2015 年 8 月 15 日
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);

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by