|
-
Knowledgable n00b
[RESOLVED] Extending a vector to the edge of the window
Alright, this is one I've really been struggling with for a while.
I'm trying to draw a line from a point, in a direction to whichever edge of the window it hits first.
I've really only come up with the most brute force solutions to this problem (getting the vector, finding intersections with the all edges, choosing the shortest) but I can't help but feel that some much more elegant solution is JUST out of my vision.
A followup:
This one is a similar problem in that I know there's got to be a really nice solution hanging out on the web somewhere that I haven't found, yet.
If I have a series of points along multiple edges of the window (up to all four) how can I sequence them to be connected to each other, and any window corners along the way?
In other words, let's say I have a series of points (generated at runtime, so without any order) along 2 edges of the screen and I want to draw the triangle that they make up. How can I connect them in order (so as not to get any of that ugly intersecting polygon nonsense) AND include the corner?
This one I really have very little insight on. The best I've managed is picking a point at the center of the stage and comparing the angles formed when drawing lines to each point. It's ugly, and requires that one find the largest angle between two of those vectors, first (making two passes necessary). It even breaks when the points extend to three sides as the largest angle is not always between the two outside vectors.
This ones a little intense, and at the same time a little open ended, but both of these problems just seem common enough that I gotta believe someone out there has come up with better solutions than I have.
Thanks,
Sandy
Last edited by BlueGeek; 10-31-2011 at 08:59 AM.
"...there were only two good books in the hotel gift shop and I had written both of them."
-Douglas Adams
-
Can you post some diagrams? I'm not sure I understand the problems.
But, basically, it seems that sorting your points would get you most of the way there on the second one.
On the first, you should be able to compare the distances from the point to the edges very easily, and just choose the shortest one, then draw your line. Or did you mean that the line direction is also a given and you want to find the closest intersecting point on the lines given by the edges? Look up the formula for distance of a point to a line. That should help you in that case.
-
Knowledgable n00b
Alright, a little background on what I'm making here.
This is (at some point, knock on wood) going to be a simple lighting engine. I have a basic polygon composed of points that a light source extends vectors from to the edge of the screen to calculate shadows.
Diagram for problem 1:

Here, point A is the light source and point B is the polygon corner casting a shadow. C is the point I would like to figure out (along the edge of the screen, bottom in this case). It's not hard finding the intersection of two lines, it's just messy figuring out which of the (in this case four) lines it intersects with first (top bottom left or right).
Diagram for problem 2:

Here the points B through F have all been cast onto the edges of the screen by the points along the blue line. In order to make a proper shadow I must connect the shape made by these points, plus the corner point A (here shown by the red and blue lines). For complex shapes which have concave features within concave features, the projection onto the edge of the window may not be in order, and may have a lot of overlap causing 'negative-fill' (when a single shape draws twists over itself flash considers it to be outside of the shape). Because of this, I need a quick, fast, easy way to sequence all of the points, OR just pick the outmost ones.
"...there were only two good books in the hotel gift shop and I had written both of them."
-Douglas Adams
-
You can use the difference of B and A to narrow the intersection to one of the two edges in the direction of the vector. That is, on of 0 - B.x or width - B.x should have the same sign as B.x - A.x. Similarly for y. Then you just need to find one intersection, and see if it's at a valid window coordinate.
As for the second, I think you can sort your points by x, then y (or vice versa) to get them in order. If you add the appropriate corner, you should be able to get all the vertices you need.
On the other hand, I'm not a graphics expert, so I'm guessing there are established ways of calculating these things efficiently. Perhaps check game programming literature.
-
Knowledgable n00b
The first answer was EXACTLY the sort of vector-loving super efficiency I was looking for, thanks!
That second answer gave me an idea, it's almost what I need, thanks so much! Marking this as resolved.
"...there were only two good books in the hotel gift shop and I had written both of them."
-Douglas Adams
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|