Solved

Vector Question

Posted on 1998-07-06
2
207 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Indesign file needs voice and vary speed. 3 274
add colored shadow in AI CS5 7 223
Convert image 4 103
File conversion question 5 120
Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
In this Micro Tutorial viewers will learn the basic shortcuts and functions of Illustrator. The viewer will learn about the paintbrush tool, anchor points, font sizing, and more.
It’s easy to embed any of your public Prezi presentations on your website or social network to share with others. Learn how simple it is in this tutorial.

830 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