Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to project light and detect if a given point falls within it?

Basically, projecting light like a flashlight, and checking if a point - I only need to check for a point, but it wouldn't hurt to be able to check for more than one - is in the area illuminated by it or not.

Also, I assume most (all?) algorithms work in 2D/3D, but would it be possible to use one that works in an N-dimensional space? I'm only interested if it's usable for an arbitrary number of dimensions with a reasonable complexity.

like image 837
gendum Avatar asked Jul 25 '10 02:07

gendum


1 Answers

Assuming you've got a normalized vector n pointing in the direction of the light cone, a light at Pl, and test point at Pp, and half-cone angle theta, you can do the test like this (independent of the dimensionality of your space):

vector Vl = Pl + n
vector Vp = Pp - Pl

phi = acos(dot(Vl, Vp) / (||Vl|| * ||Vp||))

if phi <= theta
  p in light cone
else
  p not in light cone

Here dot(a,b) is the dot product of the two vectors (a1*b1 + ... + an*bn) and ||a|| is the magnitude of vector a, sqrt(a1*a1 + ... + an*an).

The basic idea is to figure out the angle between the centerline of the light beam and the vector from the light source to the point of interest. If that angle is bigger than the spotlight (half-)angle, the point of interest is outside of the beam--otherwise it's inside.

Note that real lights don't really cutoff so sharply, but this will get you started.

like image 173
Drew Hall Avatar answered Nov 15 '22 09:11

Drew Hall