Distance along the perpendicular line

I have two line segments seg1 and seg2. The starting and ending points of each line segment is known. For example seg1=(x1,y1),(x2,y2) and seg2=(x3,y3),(x4,y4). I want to draw a perpendicular line through the mid point of seg1 (let's assume the mid point is (x0,y0)). Then I want to check whether this perpendicular line touches(crosses) seg2. If it crosses, then I want to calculate the distance along the perpendicular line to this crossing point(not the perpendicular distance from (x0,y0) to the crossing point/seg2)
Furthermore, I want to find the sign (+ or -) of the distance. i.e: If the seg2 is on the right side, the sign should be positive(+) and if it is on left, the sign should be negative (-).
Please help.

5 Kommentare

Jan
Jan am 27 Sep. 2011
How do you define "right side" in 2D? By looking from (x1,y1) to (x2,y2) or the other way around?
Please post, what you have done already and where which problems occurred. Specific questions are replied more likely compared to a general description of a problem. If I post a working program now, it is not clear if it matchs your needs exactly and therefore this would be waste of time.
Ivan van der Kroon
Ivan van der Kroon am 27 Sep. 2011
maybe you can start with a perpendicular line having a slope that is equal to the negative reciprocal of the original line. So, slope1=(y2-y1)/(x2-x1) and the line y=y0+(x-x0)*-1/slope1 will go through (x0,y0) perpendicularly. I'm sure you can find a way to check whether it crosses line2. Be careful with y2-y1=0 or x2-x1=0 though. Good luck.
Devinya Herath
Devinya Herath am 27 Sep. 2011
Dear Simon,
Yes. by looking from(x1,y1) to (x2,y2). I feel like we have to go for vector calculations but I need help. I will state the problem again for you to understand it clearly.
There is a set of line segments, seg_1, seg_2,....., seg_n. The starting and ending points of each segment is known. I select seg_1 as the initial segment. Let the end points of seg_1 be P1(x1,y1) and P2(x2,y2). now I want to calculate the mid point M (Xm, Ym) of seg_1 and find a line perpendicular to Seg_1 and that runs through M.
Now, the other segments can be in either side of seg_1 when we look from P1 to P2.
Then, I select another segment seg_i from rest of the segments and want to check whether the perpendicular line crosses that segment. I the crossing point is denoted by C(Xc, Yc), I want find following.
(1) The distance MC
(2) sign of the distance (if C is in one side of seg_1 the sign should be positive and if it is in the other side, the sign should) be negative.
Jan
Jan am 27 Sep. 2011
@Devinya: The problem is clear. What have you done so far?
Devinya Herath
Devinya Herath am 27 Sep. 2011
I have calculated M, the perpendicular line and C

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Gefragt:

am 27 Sep. 2011

Community Treasure Hunt

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

Start Hunting!

Translated by