Damn! 15 that's so young!
So, the first thing you need to know is what is a line. Check that out for the basis : [1]
Here we're going to use only to thing :
a) a line f is representable mathematically by y=a*x+b where a is the slope [2] of the line and b is the y-intercept of the line [3].
b) a line can be defined by two points O(o1, o2) and P(p1, p2).
In fact our line f is a linear equation [4].
From the point O and P we can find our a and b with these formulas :
a = (o2 - p2) / (o1 - p1)
b = o2 - a * o1 which is also b = p2 - a * p1
Now you know how to create any line. :wink:
The next step is to take two lines, let's say f and g which are y=a*x + b and y=c*x + d, respectively, and find their intersection [5].
To do that we have to solve the linear system given by f and g. It may sound very complicated but it's quite easy in fact.
This idea is very basic : we want to find the couple (x,y) such that y = a*x + b = c*x + d. (Remember we know a, b, c and d.) Hence a*x - c*x = d - b.
a*x - c*x can be written as x * (a - c) thus if we divide both side of a*x - c*x = d - b by (a - c) we have x = (d - b) / (a - c).
Now we know the value of x but we still don't know what is y. To know it we go back to y = a*x + b and «simply» compute y.
Now you know where the two line f and g intersect. But in our square we don't have line but segments [6]. Thus we have to check if our intersection (x,y) is «in» our segment or «out». This check is very easy :
Let's say our two segments are delimited by two points M(m1, m2) N(n1, n2) and I(i1, i2), J(j1, j2), respectively. Then you only check if x is between m1 and n1 and between i1 and j1. Or you check y. Checking x AND y is useless because if x is «in» then y is also «in» and vice versa.
If you have some trouble with anything try first to make some sketching then if you still don't understand ask me. :wink:
Now to check if your line go through your box you have to check each side.
Have FUN !
(NB : most part of wiki articles are out of our scope. Don't be afraid of what you're going to read there. Everything you'll need is explained here.)
[1]
http://en.wikipedia.org/wiki/Line_(geometry)
[2]
http://en.wikipedia.org/wiki/Slope[3]
http://en.wikipedia.org/wiki/Y-intercept[4]
http://en.wikipedia.org/wiki/Linear_equation[5]
http://en.wikipedia.org/wiki/Line_intersection[6]
http://en.wikipedia.org/wiki/Line_segmentPS : just wondering : are you German ?