Solved

Vector Question

Posted on 1998-07-06
2
195 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
Comment Utility
Edited text of question
0
 
LVL 10

Accepted Solution

by:
Jacco earned 50 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
switching image colours 11 105
Microsoft Image Composer 6 115
Image optimization 7 139
Inverting colors with Photoshop 9 60
I've been using this technique since Adobe CS2, and it should work with any version of Illustrator that includes the appearance panel. In this tutorial we'll create a button using the appearance panel in Adobe Illustrator, and then save it as a r…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
In this tutorial viewers will learn how to create blended and gradiated shapes in Illustrator using the blend tool Draw two shapes, one of them in a different color: Select both and create a blend by going to Object > Blend > Make: Blends can also b…
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 …

772 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

14 Experts available now in Live!

Get 1:1 Help Now