Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
perpendicular line -- follow up

Subject: perpendicular line -- follow up

From: Lisa

Date: 31 May, 2011 19:28:05

Message: 1 of 8

hey,

i have a follow up question on this thread posted over a years ago:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/260933


the proposed solution for finding the perpendicular vector works fine for lines that are neither horizontal nor vertical since for those, the slope remains undefinied. i have been thinking about using the dot product to identify the perpendicular in these cases but could not succeed so far to bring it to matlab. i would be very grateful for suggestions or advise because i just cannot work it out myself.

thanks in advance

lisa

Subject: perpendicular line -- follow up

From: ImageAnalyst

Date: 31 May, 2011 20:07:44

Message: 2 of 8

Lisa
Why not? Surely you know how to check if the slope == 0 or inf, or if
x1==x2 or y1==y2. You can then handle those as special cases if you
want, or need to.

Subject: perpendicular line -- follow up

From: Lisa

Date: 31 May, 2011 23:07:04

Message: 3 of 8

ImageAnalyst

yes, i know how to check if the slope is existent or not. however, i do not know how to proceed then. i know that the dot product might provide a solution but i do not know how to implement it properly in matlab.

any suggestions?


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <86baf6bf-7f5a-4cb5-9eaa-0b1c471e275e@y19g2000yqk.googlegroups.com>...
> Lisa
> Why not? Surely you know how to check if the slope == 0 or inf, or if
> x1==x2 or y1==y2. You can then handle those as special cases if you
> want, or need to.

Subject: perpendicular line -- follow up

From: Roger Stafford

Date: 1 Jun, 2011 00:24:02

Message: 4 of 8

"Lisa" wrote in message <is3fg5$25i$1@newscl01ah.mathworks.com>...
> i have a follow up question on this thread posted over a years ago:
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/260933
> the proposed solution for finding the perpendicular vector works fine for lines that are neither horizontal nor vertical since for those, the slope remains undefinied.
- - - - - - - - - -
  There should be no problem with horizontal or vertical lines here. This is how I would have answered thread 260933. If P1 = [x1,y1] and P2 = [x2,y2] are the two endpoints of the line segment, then

 Q0 = (P1+P2)/2 = [(x1+x2)/2,(y1+y2)/2]

would be its midpoint. A point Q1 along the line from Q0 orthogonal to the segment P1P2 and a distance from Q0 equal to the length of P1P2 would be

 Q1 = Q0 + [0,1;-1,0] * (P2-P1) = [(x1+x2)/2+(y2-y1),(y1+y2)/2-(x2-x1)]

The points Q0 and Q1 lie on, and thus determine, the desired line, and there has been no need to compute its slope.

Roger Stafford

Subject: perpendicular line -- follow up

From: Lisa

Date: 1 Jun, 2011 03:26:02

Message: 5 of 8

Roger,

i am sorry but when i take your solution to matlab, it does not plot two orthogonal lines but two separate ones. maybe i should clarify my problem a little bit more: i am plotting patch objects (rectangles) that span the distance from one point to another. these two points may lie anywhere in the 2D space. my actual problem is that the rectangle between them is never perfectly rectangular but always a little bit skewed so that not all of its angles have 90 degrees. this i would like to avoid by plotting the perpendicular lines.


"Roger Stafford" wrote in message <is40r2$ikd$1@newscl01ah.mathworks.com>...
> "Lisa" wrote in message <is3fg5$25i$1@newscl01ah.mathworks.com>...
> > i have a follow up question on this thread posted over a years ago:
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/260933
> > the proposed solution for finding the perpendicular vector works fine for lines that are neither horizontal nor vertical since for those, the slope remains undefinied.
> - - - - - - - - - -
> There should be no problem with horizontal or vertical lines here. This is how I would have answered thread 260933. If P1 = [x1,y1] and P2 = [x2,y2] are the two endpoints of the line segment, then
>
> Q0 = (P1+P2)/2 = [(x1+x2)/2,(y1+y2)/2]
>
> would be its midpoint. A point Q1 along the line from Q0 orthogonal to the segment P1P2 and a distance from Q0 equal to the length of P1P2 would be
>
> Q1 = Q0 + [0,1;-1,0] * (P2-P1) = [(x1+x2)/2+(y2-y1),(y1+y2)/2-(x2-x1)]
>
> The points Q0 and Q1 lie on, and thus determine, the desired line, and there has been no need to compute its slope.
>
> Roger Stafford

Subject: perpendicular line -- follow up

From: Rune Allnor

Date: 1 Jun, 2011 04:57:52

Message: 6 of 8

On Jun 1, 5:26 am, "Lisa " <_awalt...@web.de> wrote:
> Roger,
>
> i am sorry but when i take your solution to matlab, it does not plot two orthogonal lines but two separate ones. maybe i should clarify my problem a little bit more: i am plotting patch objects (rectangles) that span the distance from one point to another. these two points may lie anywhere in the 2D space. my actual problem is that the rectangle between them is never perfectly rectangular but always a little bit skewed so that not all of its angles have 90 degrees. this i would like to avoid by plotting the perpendicular lines.

That's a different task than the question you asked.
If you have two points, p1 = [x1 y1] and p2 = [x2,y2],
you need to do something like

P00 = [min(x1,x2),min(y1,y2)];
P10 = [max(x1,x2),min(y1,y2)];
P11 = [max(x1,x2),max(y1,y2)];
P01 = [min(x1,x2),max(y1,y2)];

patch([P00(1) P10(1) P11(1) P01(1)],[P00(2) P10(2) P11(2) P01(2)],
[1,0,0]);

Rune

Subject: perpendicular line -- follow up

From: Roger Stafford

Date: 1 Jun, 2011 05:30:21

Message: 7 of 8

"Lisa" wrote in message <is4bga$e2j$1@newscl01ah.mathworks.com>...
> Roger,
>
> i am sorry but when i take your solution to matlab, it does not plot two orthogonal lines but two separate ones. maybe i should clarify my problem a little bit more: i am plotting patch objects (rectangles) that span the distance from one point to another. these two points may lie anywhere in the 2D space. my actual problem is that the rectangle between them is never perfectly rectangular but always a little bit skewed so that not all of its angles have 90 degrees. this i would like to avoid by plotting the perpendicular lines.
- - - - - - - - - -
  To make the "[0,1;-1,0] * (P2-P1)" multiplication work right, all the points should have been represented by column vectors, not row vectors. But except for that flaw (which I assume you corrected,) on my computer those operations I described do just what they are supposed to do. I don't understand what you mean when you say, "it does not plot two orthogonal lines but two separate ones". What I promised to do was to define a line Q1Q0 orthogonal to the line P1P2 with the point Q0 located at its midpoint, and that is exactly what it does. A good test for orthogonality of the two segments is that dot(P2-P1,Q1-Q0) should be zero and that also was true clear out to the 16th decimal place. I ran several tests with random points and plotted them.

  You should be sure the scales on your two axes are the same, or else lines that are mathematically orthogonal can look very skewed. On my system that is accomplished by typing "axis equal".

Roger Stafford

Subject: perpendicular line -- follow up

From: Lisa

Date: 1 Jun, 2011 19:05:20

Message: 8 of 8

Rune,

this will give you a square but i want to plot a rectangle that can have any given position in 2D space and any "thickness" but remains rectangular (all angles equal 90 degrees). its not straightforward to solve this since i could not find a proper solution so far online.


Rune Allnor <allnor@tele.ntnu.no> wrote in message <4950aad6-c34c-43d6-b694-000a3954e575@f2g2000yqh.googlegroups.com>...
> On Jun 1, 5:26 am, "Lisa " <_awalt...@web.de> wrote:
> > Roger,
> >
> > i am sorry but when i take your solution to matlab, it does not plot two orthogonal lines but two separate ones. maybe i should clarify my problem a little bit more: i am plotting patch objects (rectangles) that span the distance from one point to another. these two points may lie anywhere in the 2D space. my actual problem is that the rectangle between them is never perfectly rectangular but always a little bit skewed so that not all of its angles have 90 degrees. this i would like to avoid by plotting the perpendicular lines.
>
> That's a different task than the question you asked.
> If you have two points, p1 = [x1 y1] and p2 = [x2,y2],
> you need to do something like
>
> P00 = [min(x1,x2),min(y1,y2)];
> P10 = [max(x1,x2),min(y1,y2)];
> P11 = [max(x1,x2),max(y1,y2)];
> P01 = [min(x1,x2),max(y1,y2)];
>
> patch([P00(1) P10(1) P11(1) P01(1)],[P00(2) P10(2) P11(2) P01(2)],
> [1,0,0]);
>
> Rune

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us