# How do you calculate if a line is facing a curve?

Given the 2 points describing a line segment, and the 3 points describing a quadratic bezier curve, how do you calculate if the line is facing towards or away from the curve?

The line has a fixed length, and is stored in 2D as x1/y1 and x2/y2. The curve is a quadratic bezier curve, and is stored as x1/y1 (control point), x2/y2 (bezier point), x3/y3 (control point).

• I don't have any concrete examples but you could try treating the line as a ray, and if the ray intersects the curve then its facing towards. – PhilCK Dec 23 '11 at 18:16

First of all we need to know how a beizer curve is generated. according to it's wiki page, a Quadratic beizer curve is a function of t stated as follow:
In this formula t is a variable between zero and one and all other parameters are vector values (it means B(t), P0, P1, and P2 have values for x and y in 2D world).

the next thing you should know is how to define a line based on two given points. again we can see in beizer wiki page that lines are some special kind of beizer curves with only two control points and no beizer point. and their formula is again defined using parameter t as follow:
again t is a scalar value while p0, p1, and L(t) are 2D vectors.

Now we want to know if our beizer curve collides with the ray started with an specific point in an specific direction. to do so we need to solve This equation:

• in this formulas t and t' are unknown variables and all the others are known
• P0, P1 and P2 are representing Beizer curve while P'0 and P'1 are endpoints of the line.
• we can't assume t=t' in most cases. and it's also possible to get two values for both t and t'. having two results only mean the line has two intersection points with the curve and it's nothing to worry about.

solving that equation is almost easy but the results of the equation will tell us about collision states:

if in a results we get t<0 or t>1 it means the ray does not collide with the curve itself but with it's trails which is a point we are not interested in.

if in a results we get t'<0: it means the line collides with the curve in the opposite direction.

if we get t>0: it means lines collides with the

• It’s not clear to me how you can get two values for t and t'. What’s the equation being solved in that case? – sam hocevar Dec 24 '11 at 12:43
• @SamHocevar I've edited my answer to make it simpler, now it's just two scalar equations with order of 2 with two scalar variables to be found. – Ali1S232 Dec 24 '11 at 17:37

A quadratic bezier curve is (obviously) a quadratic function. A line is a linear function. So to find any intersection points you have to solve a system of equations like this:

line-function = bezier-function

If you get any solutions (functions intersect) you calculate the distance between intersection point and start/end point of your line and you should know if the line is facing towards or away.