Find where two lines join?

Hey
My guy shoots at a tile, this tile is 30 x 30 and is a square. The angle of my shooting dude’s gun is known and the bullet will end at the tile. So far I have found which tile my gun’s bullet will end at but not the exact edge/point is know so I need to know…
How do I find where to “theroretical” lines join. I know the start and end cooodinates of each line but I dont know “mathematically” how to find out where they cross? There must be a way! If it is any help one of the lines is always 30 and either horizontal of vertical and I know the start points, end points and angle of the other line.
Thanks for your help
Dan :smiley:

I need to figure this out for my program too…

A quick google got me
http://id.mind.net/~zona/mmts/intersections/intersectionOfTwoLines1/intersectionOfTwoLines1.html

It’s some basic math, you need to find the slope and y intercept of each line then set the equations equal to each other.

Equation of a Line
y = mx + b
[LIST]
[]m is slope
[
]b is y intercept[/LIST]Finding Slope From Two Endpoints
m = (y2 - y1) / (x2 - x1)

Simply plug the coordinates of your end points into that equation and you have your slope.

Finding Y Intercept
Using the equation of a line, you can calculate the y intercept of the function.

b = y / mx
[LIST]
[]m is slope
[
]y is the y component of any coordinate on the line (you can use the endpoints)
[*]x is the x component of that same coordinate[/LIST]Finding X Component of Intersection
At the intersection of two lines, the Y and X components are equal so all you need to do is set the equations of the two lines equal to each other and solve.

x = (b1 - b2) / (m2 - m1)

Finding Y Component of Intersection
Now just sub the newly found X value into either of the equations and solve for Y.

Hope this helps :slight_smile:

<3 im not the OP but thanks canadian u just made my life easier

Thanks i tried this other one and it works but I think ill have to do a “cheapy” way because its like impossible to find the correct side to check against.
Thanks for your help again
Dan

Vectors. Give that a shot and see if it helps.

wow that link is nice man

You may also want to look at this:
http://www.kirupa.com/forum/showthread.php?t=195433

:s:

Wow im using the tonypa engine as a guide, but I dont like some features of it + I have made it simpler+better+added shooting which I will submit somewhere when i finish. I never seen that part of the tonypa site before, thanks!

Ok…I’m confused, how would you use the slope-intercept method of getting a line to slope?
How would the slope-int. be written out? Like this?

line._y = m*line._x+b;

and have the line repeat itself in an enterFrame listener? All I want to know is how to draw a line with the slope-intercept method. I know how to with other methods, but this one is…more interesting.

You have to plot it with incremented values of x:

var m:Number = 1;
var b:Number = 0;
this.lineStyle(1);
for (var x:Number = 0; x < Stage.width; x++) {
 this.lineTo(x, m * x + b);
}

Or I suppose if you wanted to you could just find two points along the line and draw between them:

var m:Number = 1;
var b:Number = 0;
var sx:Number = 0;
var ex:Number = Stage.width;
this.lineStyle(1);
this.moveTo(sx, m * sx + b);
this.lineTo(ex, m * ex + b);

You also have to remember that Flash’s y-axis is reversed, so down is positive. You can compensate for this if you want:

var m:Number = 1;
var b:Number = 0;
var sx:Number = 0;
var ex:Number = Stage.width;
this.lineStyle(1);
this.moveTo(sx, -m * sx + (b + Stage.height));
this.lineTo(ex, -m * ex + (b + Stage.height));

Hope this helps :sc:

Someone should have a tutorial on shooting with this.