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:
Plot a line perpendicuar to another line from its midpoint

Subject: Plot a line perpendicuar to another line from its midpoint

From: Kenneth Galea

Date: 3 Apr, 2010 19:17:24

Message: 1 of 2

Hi everyone. I'm trying to plot a line perpendicular and in the middle of another line. I'm already half way through but I'm stuck when trying to plot from the middle:
check this link:
 http://dl.dropbox.com/u/5800838/untitled.jpg

I did the following code:

    x1 =GR_endpoints(plotnext,2) %start point of line
    y1 =GR_endpoints(plotnext,1)
    x2 =GR_endpoints(plotnext+1,2) %end point of line
    y2 =GR_endpoints(plotnext+1,1)
    
    P1 = [x1 y1]
    P2 = [x2 y2]
    d = 75
    
    t = sqrt((x2-x1)^2+(y2-y1)^2);
    x3 = x1-(y2-y1)/t*d
    y3 = y1+(x2-x1)/t*d
    line([x3 x1], [y3 y1] , 'Color','b', 'LineStyle','-', 'LineWidth', 0.5)

N.B. I did not use gradient since I saw one example in this forum but this can lead to a division by 0. Please help

Regards
Kenneth

Subject: Plot a line perpendicuar to another line from its midpoint

From: Roger Stafford

Date: 3 Apr, 2010 21:44:20

Message: 2 of 2

"Kenneth Galea" <k.galea@hotmail.com> wrote in message <hp8483$igb$1@fred.mathworks.com>...
> Hi everyone. I'm trying to plot a line perpendicular and in the middle of another line. I'm already half way through but I'm stuck when trying to plot from the middle:
> ........
> t = sqrt((x2-x1)^2+(y2-y1)^2);
> x3 = x1-(y2-y1)/t*d
> y3 = y1+(x2-x1)/t*d
> line([x3 x1], [y3 y1] , 'Color','b', 'LineStyle','-', 'LineWidth', 0.5)
> .......

  You are using the wrong base point. For the midpoint use this:

 t = sqrt((x2-x1)^2+(y2-y1)^2);
 x0 = (x1+x2)/2;
 y0 = (y1+y2)/2;
 x3 = x0-(y2-y1)/t*d;
 y3 = y0+(x2-x1)/t*d;
 line([x3 x0], [y3 y0] , .....

Roger Stafford

Tags for 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