Solved

Vector Question

Posted on 1998-07-06
2
200 Views
Last Modified: 2010-04-06
Not exactly a graphics question but a graphics person should be able to answer me.

I've got a Vector with origin Ro and Direction Rd, I also have a line between two points, P0 and P1, how do I find out if the line intersects between the two points, and where they intersect?

Thanks.
0
Comment
Question by:Jonc
2 Comments
 

Author Comment

by:Jonc
ID: 1112838
Edited text of question
0
 
LVL 10

Accepted Solution

by:
Jacco earned 50 total points
ID: 1112839
I presume your question concerns 2D problems.

Here is a sample algoritm:

P0(X,Y) P1(X,Y)

Ro(X,Y) Rd(X,Y)

First you make Po and Pd (which are the origin and direction of the lines through the points P0 and P1)

For the origin you take P1: Po = P1
For the direction you take P2 - P1: Pd = P2 - P1

PoX = P1X
PoY = P1Y

PdX = P2X - P1X
PdY = P2Y - P1Y

Normalizing the directions:

PdX = 1
PdY = (P2Y - P1Y)/(P2X - P1X)

RdY = RdY/RdX
RdX = 1

(Note: P2X-P1X can be zero making the divide impossible but you know then that the line
is vertical so if there is an intersection it is at RoX. An that is only possible if RoX is between P1X and P2X)

If RdY and PdY are equal then the line do not intersect. Otherwise continue.

The following equations describe both lines

RY = RoY + (RX - RoX)*RdY
PY = PoY + (PX - PoX)*PdY

a intersection occurs when:

RY=PY
RX=PX

Replace RX and PX by IX
Replace RY and PY by IY

That gives:

IY = RoY + (IX - RoX)*RdY
IY = PoY + (IX - PoX)*PdY

Subtract to two equations:

0 = RoY - PoY  + (IX - RoX)*RdY - (IX - PoX)*PdY

IX is the only unknow in this equation (notice that RdY and PdY need to be different):

0 = RoY - PoY + IX*RdY - RoX*RdY - IX*PdY + PoX*PdY

0 = RoY - PoY + IX*(RdY - PdY) - RoX*RdY + PoX*PdY

IX*(PdY - RdY) = RoY - PoY - RoX*RdY + PoX*PdY

IX = (RoY - PoY - RoX*RdY + PoX*PdY)/(PdY - RdY)

remembering that:

PoX = P1X
PoY = P1Y
PdY = (P2Y - P1Y)/(P2X - P1X)

You can rewrite the formula to:

IX = (RoY - P1Y - RoX*RdY + P1X*((P2Y - P1Y)/(P2X - P1X)))/((P2Y - P1Y)/(P2X - P1X) - RdY)

which were all known from the start.

Now we have the IX which is the X position of the intersection.

Check if IX is between P1X and P2X.

If not then there is no intersection.

If so then calculate the Y position. The line between the points P1 and P2 was described by:

IY = PoY + (IX - PoX)*PdY

Fill in formula for PdY:

IY = PoY + (IX - PoX)*PdY


IY = PoY + (IX - PoX)*(P2Y - P1Y)/(P2X - P1X)

There is you intersection.

I hope I didn't make any mistakes. You can use the ultimate formulas. But pay attention to vertical lines or directions!!

Regards Jacco

P.S. Phew these mathematics are handy but rusty

Good luck!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Making an animated GIF from several small video clips 10 149
Adobe Illustrator - stroke alignment 2 267
graphic software 6 89
File conversion question 5 83
Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
Add visuals to your Prezi to keep your audience engaged. Learn how to embed images, edit them, and more in this video micro tutorial. Select "Insert" from the top menu in your Prezi editor: Select "Image": A toolbar will pop in from the right …
Not only does Prezi allow you to create non-linear presentations, it also makes it easy to apply transition animations between your frames. Learn how to apply a fade-in transition to select items and entire frames in this tutorial Select "Edit Path"…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now