Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

My screen resolution is 640x480 and I have two Vector2 objects located at (10,10) and (600, 320).

How can I connect these two objects and extend the line to encompass the whole screens width, how can I get the Y for the intersection at the center of the width (320)?

share|improve this question
    
Can you try to explain a bit further what exactly you are looking for? –  Nate Apr 7 '11 at 14:51
    
It sounds like there is a line from (10,10) to (600,320). This needs to be extended to x=0 and x=640 and divided at x=320, which implies finding y for these 3 x values. –  e100 Oct 3 '12 at 13:20

3 Answers 3

you have to use the equation of a line. I prefer to use parametric representation P(t)= 1-t*(v1) + t*V2 ; then you solve the equation where P1 = P2 ...

here is the detailed solution http://paulbourke.net/geometry/lineline2d/ you can also google "line line intersection"

share|improve this answer

Do you mean midpoint?

You can always set the two vectors equal to each other with a scalar multiple for each and solve, but this situation is not clear. Are we to assume they are pointing at each other?


Updated for clarity

You are asking for the point of intersection between two lines, one that is vertical - splitting the screen - at x=320, and one that is at some other non parallel point. With two lines, the point of intersection is the point where they are equal to each other.

I used the example of a vertical line (x=320) and a second line (y=13x+2) and all you have to do is set them equal to each other and solve. Since one line is x=320, so no matter what you do the x value needs to be 320. When you plug that value into the second equation, you get y=13(320)+2

This will give you the y position for the system of equations. You can find the line by using the vectors slope, or rise/run (change in y/change in x), and solve for the offset (y=mx+b, because your lines origin gives you the x and y positions.

Ex.

Two points (300,30), (450, 330);
The slope is (330-30)/(450-300)=(300/150)=2;
The y intercept is y=mx+b -> (30)=(2)(300)+b -> b=-570;
The line is thus y=(2)x-570; 
This line intersects the x=320 line at:
y=(2)x-570 == x=320 -> y=(2)(320)-570 = 70
the point of intersection is: (320, 70)

http://www.analyzemath.com/Calculators_2/intersection_lines.html

share|improve this answer
    
I have a line that is at (320,0) and (320,480) vertically splitting the screen. Now if I have another line that intersects this line, how can I find the Y position of that intersect? –  anonymouse Apr 5 '11 at 19:46
1  
=) You have two lines, one is at x=320 (a vertical line at x = 320), the second can be anything not parallel. Plug 320 into the equation for the other line. Ex. Line #1: x=320 Line #2: y=13x+2 they intersect at y=13(320)+2 –  Bob_Gneu Apr 5 '11 at 22:30
    
Could you maybe explain this a little further? –  anonymouse Apr 6 '11 at 17:51

If I understand correctly, there is a line from (10,10) to (600,320). Let's call these (x1,y1) and (x2,y2). The line needs to be extended to x=0 and x=640, and divided at x=320. This implies finding y for these 3 values of x.

You need to know the equation which describes the line. For a straight line, this will be in the form of:

y = slope*x + intercept

i.e. for all points on the line, this formula will be true.

Slope is just:

(change in y) / (change in x)

So:

y = ((y2-y1) / (x2-x1) * x) + intercept

Rearrange:

intercept = y - ((y2-y1) / (x2-x1) * x)

Substitute in x and y from either one of your known points. (x1,y1) used here:

intercept = y1 - ((y2-y1) / (x2-x1) * x1)

Subsitute the expressions you now have for slope and intercept back into the line equation:

y = slope*x + intercept

And you can then find y for any value of x.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.