Hacker News new | ask | show | jobs
by dllu 4483 days ago
Yes, with calculus you can find all the tangent lines from a point to any kind of curve. The exact equations vary based on what kind of curve it is (sine, parabola, cubic splines, Bezier curves, non-uniform rational B-splines, etc) and are kinda complicated.
3 comments

Let P be the point (a,b) and let Q be the point (x(t),y(t)) of a curve for some parameter t. The tangent vector to that curve is (x'(t),y'(t)) and the ray PQ is just (a-x(t),b-y(t)). You need to make sure these vectors are the same (up to a constant multiple) in order to have a tangent ray. That is you are led to two equations

cx'(t) = a-x(t) cy'(x) = b-y(t)

You need to solve these two equations for values of c and t. The difficulty of this task depends mostly on the difficulty of the curve itself.

If the curve has equation y = f(x) (or in other words a simple representation x(t) = t and y(t) = f(x(t)) = f(t)), then you have

c = a-x cf'(x) = b-f(x)

Therefore (a-x)f'(x) = b-f(x). If f(x) is a polynomial of degree n, the problem is reduced to root-finding of a polynomial of degree n so you'll have at most n roots to deal with. So in general it won't be easy. This reduces to the code in the post if you use line segments.

At that point you're no longer dealing with rays and the math gets way more expensive for a goal of 1/60th of a second per render loop.
Any of this is absurdly trivial on modern graphics hardware, you can literally do all of the geometric math to compute your shadow volumes on the GPU and it will be a miniscule portion of your frame time.
Hm, then it becomes a question of whether that can be ran fast enough and remain realtime. :)