Finding Outer Vertices of Shapes

I having trouble figuring out a good way to accomplish my goal. I'm trying to be able to cast 2D shadows from objects. I figured that I can just find the "outer" vertices relative to the light source and go from there, but I'm not having much luck. If you're still confused about what I want to do, here's a crude image. The blue blob is the light source. The circled vertices are the vertices I wish to get.

I've tried one implementation, but it's not working at all. Can anyone take a look and possibly offer suggestions? Once I get this working on a square, I'm going to add guards against single points and lines being passed as shapes, but for now I'm assuming the Shape is a square.

// use the dot product on that vector and a (0, -1, 0) vector to get the cosine of the angle between the two vectors
Vector temp = (0, -1, 0)
float theta = -acos(temp.dot(w))
float cos_theta = cos(theta)
float sin_theta = sin(theta)

In your example, the system would be rotated so that it looks like this:
(O is the object's origin, L is the light source's origin, and p1 and p2 are the two vertices you are solving for)

Kind of easy to find p1 and p2 now, isn't it? It's just the leftmost and rightmost vertices. Since only the x values for the vertices are needed to solve for p1 and p2, only the x value is calculated in the pseudocode's loop.

imikedaman Wrote:Kind of easy to find p1 and p2 now, isn't it? It's just the leftmost and rightmost vertices. Since only the x values for the vertices are needed to solve for p1 and p2, only the x value is calculated in the pseudocode's loop.

Hm. Seems very nice. I gave it a try, but I get some awkward shadows that overlap themselves (wrong points I guess) and it doesn't seem to get the right points at certain angles. Here's my code for implementing this:

// use the dot product on that vector and a (0, -1, 0) vector to get the cosine of the angle between the two vectors
Vector temp = (0, -1, 0)
float theta = acos(temp.dot(w)) // changed this to a positive acos since I think that's the correct sign for it
float cos_theta = cos(theta)
float sin_theta = sin(theta)

You'll probably have to tweak the code to make it work, but that's the general idea. In this one, I rotate the system to the graphic in the previous post I made, then find the smallest dot product between the (vector from the light source to the origin) and the (vector from the light source to the vertex).

// fixed this code here
if (temp_x <= light_pos_x) then // changed this from <= 0 to <= light_pos_x
if (dot_product < p1_min) then
p1_min = dot_product
p1 = vertices[m]
end if
else
if (dot_product < p2_min) then
p2_min = dot_product
p2 = vertices[m]
end if
end if
next

That should work great. Good luck!

EDIT 2: It doesn't seem to work with certain nonconvex objects (although it does with with a lot of them). Is that important at all? I ask because your demo has only convex objects.

The first two are rectangles which work fine with my original code (not your method). The third is a triangle which doesn't work with mine and the last is a pentagon that doesn't work with mine. With your method, none of them are computed correctly.