Method to detect intersection between a rectangle and a polygon? - algorithm

What is the best method to detect whether the red rectangle overlaps the black polygon? Please refer to this image:

There are four cases.
Rect is outside of Poly
Rect intersects Poly
Rect is inside of Poly
Poly is inside of Rect
First: check an arbitrary point in your Rect against the Poly (see Point in Polygon). If it's inside you are done, because it's either case 3 or 2.
If it's outside case 3 is ruled out.
Second: check an arbitrary point of your Poly against the Rect to validate/rule out case 4.
Third: check the lines of your Rect against the Poly for intersection to validate/rule out case 2.
This should also work for Polygon vs. Polygon (convex and concave) but this way it's more readable.

If your polygon is not convex, you can use tessellation to subdivide it into convex subparts. Since you are looking for methods to detect a possible collision, I think you could have a look at the GJK algorithm too. Even if you do not need something that powerful (it provides information on the minimum distance between two convex shapes and the associated witness points), it could prove to be useful if you decide to handle more different convex shapes.
Christer Ericson made a nice Powerpoint presentation if you want to know more about this algorithm. You could also take a look at his book, Real-Time Collision Detection, which is both complete and accessible for anyone discovering collision detection algorithms.

If you know for a fact that the red rectangle is always axis-aligned and that the black region consists of several axis-aligned rectangles (I'm not sure if this is just a coincidence or if it's inherent to the problem), then you can use the rectangle-on-rectangle intersection algorithm to very efficiently compute whether the two shapes overlap and, if so, where they overlap.

If you use axis-aligned rectangles and polygons consist of rectangles only, templatetypedef's answer is what you need.
If you use arbitrary polygons, it's a much more complex problem.
First, you need to subdivide polygons into convex parts, then perform collision detection using, for example, the SAT algorithm

Simply to find whether there is an intersection, I think you may be able to combine two algorithms.
1) The ray casting algorithm. Using the vertices of each polygon, determine if one of the vertices is in the other. Assuming you aren't worried about the actual intersection region, but just the existence of it. http://en.wikipedia.org/wiki/Point_in_polygon
2) Line intersection. If step 1 produces nothing, check line intersection.
I'm not certain this is 100% correct or optimal.
If you actually need to determine the region of the intersection, that is more complex, see previous SO answer:
A simple algorithm for polygon intersection

Related

Making a list of outermost circles on a plane

I have a big rectangle filled with circles. The circles might overlap each other, but they all have the same diameter. I need to find the "borderline" circles. If there are gaps between these borderline circles - and the gap is bigger than a circle diameter - the one inside should also be included.
Here are some examples:
What I need to do in is to make these outer circles immovable, so that when the inner circles move - they never exit the rectangle. How can it be made, are there any known algorithms for such a thing? I'm doing it in TypeScript, but I guess, any imperative language solution can be applied
This is perhaps too conservative but certainly won't let any disks out.
Compute a Delaunay triangulation on the centers of the circles. A high-quality library is best because the degenerate cases and floating-point tests are tricky to get right.
Using depth-first search on the planar dual, find all of the faces that are reachable from the infinite face crossing only edges longer than (or equal to? depends on whether these are closed or open disks) two times the diameter.
All of the points incident to these faces correspond to the exterior disks.
I'm not quite sure that I have the answer to your question, but I put together two quick examples using the Tinfour Delaunay Triangulation library (which is written in Java). I had to digitize your points by hand, so I didn't quite hit the center in all cases.
The first picture shows that the boundary of a Delaunay Triangulation is a convex polygon. This is easy to build, simply add the vertices (circle centers) to Tinfour's IncrementalTin class and then ask it for the bounding polygon. Pretty much any Delaunay library will support this. So you wouldn't necessarily need Tinfour.
The problem is that this may include areas that are not valid for your interior circles. I played a little with ways to introduce concavities to the bounding polygon. As you can see below, the vertices in the lower-right corner had to be lopped off entirely (if I understand what you are looking for). I then iterated over the perimeter edges and introduced concavities where the vertex opposite each exterior edge was a "guard" vertex.
The code I wrote to do this is pretty messy. But if this is what you are looking for, I'll try to get it cleaned up and post it here.

Collision detection for every shape (2D)

I am currenty making a game which the user can make shapes(it can be every shape the user want exept for curves) and get them to interact with each other.I need a accurate collision detection algorithm that can run fast(somthing like 150 poligons at max), i managed to imploment SAT
but it seems to fall apart at more complex shapes.Can someone direct me to relevent algorithms for my situation?
SAT is intended for convex polygons.
In general case (your shapes can be concave) you can hold edge list for polygons and check for edge intersection with sweep line algorithm. (Note also that one polygon may fully contain other)
In some situations it is worth to create convex hull for every shape and check for hull intersection with SAT. If it occurs, then use more advanced algorithm to check whether real intersection does exist.

Splitting a polygon by another polygon

I need an algorithm for splitting a (convex) polygon (let's call it base polygon). The polygon should be splitted into several smaller polygons by another polygon's edges (let's call this one the splitting polygon).
I know there exist algorithms for clipping a polygon (f.e. the Sutherland-Hodgman algorithm), but these algorithms discard the vertices that are lying outside of the splitting polygon instead of creating new polygons with them. I don't want to clip a polygon, i want to split it into several small parts.
I know the answer seems quite obvious because I would just have to extend the existing algorithms.
The problem is that I can't figure out a nice and performant way of doing this.
Are there existing algorithms that describe how to best split a polygon in a performant way?
There has to be a simple solution for this problem that I can't figure out at the moment.
You can see this problem as that of finding the intersection of the subject polygon and the complement of the window polygon. So you can use the standard Surtherland-Hodgman algorithm for that purpose, taking two precautions:
swap the roles of the subject and the window (actually your window is not convex as you consider its complement, only the subject is convex),
embed the window polygon in a large bounding box that covers both polygons, and consider the box as a polygon with a hole.
Example: the subject polygon is the rectangle and the window is the pentagon. Form the green polygon (larger rectangle with a hole) and clip it inside the rectangular (convex) window. The result of the clipping is in blue.
With some extra care, it should be possible to do without the large bounding box.

Good algorithm for determination of non convex 2D figures collision

could you please provide me with some information (or suggest an article) on good collision detection algorithm for 2D non convex figures?
Thanks!
Try this:
http://www.cs.man.ac.uk/~toby/alan/software/
Note that it isn't free for commercial use.
For more details you can continue to this similar question:
A simple algorithm for polygon intersection
To determine if two simple polygons intersect :
If two simple polygons have a non-void intersection then one of the following will happen:
A) One of them has a corner inside the interior of the other one.
B) One of them has a whole edge inside the interior of the other one (the corners of that edge may not necessarily be in the interior). This means the middle of that edge will be inside the interior.
C) The polygons are identical.
D) There are two edges that intersect at an angle. The intersection point not being a corner to any of the polygons.
What you need to do is check if the polygons are identical (have the same corners), or one of the corners or one of the middle of the edges lies inside the interior of the other polygon or if there are two edges that intersect somewhere else than in a corner.
Determining if a point lies on the interior of a polygon.
I always found the wikipedia pages to be quite useful for my needs:
Sutherland Hodgman
Liang Barsky
Weiler Atherton
As well as this paper on the Weiler Atherton algorithm.

Efficient Packing Algorithm for Irregular Polygons

I'm looking for a packing algorithm which will reduce an irregular polygon into rectangles and right triangles. The algorithm should attempt to use as few such shapes as possible and should be relatively easy to implement (given the difficulty of the challenge). It should also prefer rectangles over triangles where possible.
If possible, the answer to this question should explain the general heuristics used in the suggested algorithm.
This should run in deterministic time for irregular polygons with less than 100 vertices.
The goal is to produce a "sensible" breakdown of the irregular polygon for a layman.
The first heuristic applied to the solution will determine if the polygon is regular or irregular. In the case of a regular polygon, we will use the approach outlined in my similar post about regular polys: Efficient Packing Algorithm for Regular Polygons
alt text http://img401.imageshack.us/img401/6551/samplebj.jpg
I don't know if this would give the optimal answer, but it would at least give an answer:
Compute a Delaunay triangulation for the given polygon. There are standard algorithms to do this which will run very quickly for 100 vertices or fewer (see, for example, this library here.) Using a Delaunay triangulation should ensure that you don't have too many long, thin triangles.
Divide any non-right triangles into two right triangles by dropping an altitude from the largest angle to the opposite side.
Search for triangles that you can combine into rectangles: any two congruent right triangles (not mirror images) which share a hypotenuse. I suspect there won't be too many of these in the general case unless your irregular polygon had a lot of right angles to begin with.
I realize that's a lot of detail to fill in, but I think starting with a Delaunay triangulation is probably the way to go. Delaunay triangulations in the plane can be computed efficiently and they generally look quite "natural".
EDITED TO ADD: since we're in ad-hoc heuristicville, in addition to the greedy algorithms being discussed in other answers you should also consider some kind of divide and conquer strategy. If the shape is non-convex like your example, divide it into convex shapes by repeatedly cutting from a reflex vertex to another vertex in a way that comes as close to bisecting the reflex angle as possible. Once you've divided the shape into convex pieces, I'd consider next dividing the convex pieces into pieces with nice "bases", pieces with at least one side having two acute or right angles at its ends. If any piece doesn't have such a "base" you should be able to divide it in two along a diameter of the piece, and get two new pieces which each have a "base" (I think). This should reduce the problem to dealing with convex polygons which are kinda-sorta trapezoidal, and from there a greedy algorithm should do well. I think this algorithm will subdivide the original shape in a fairly natural way until you get to the kinda-sorta trapezoidal pieces.
I wish I had time to play with this, because it sounds like a really fun problem!
My first thought (from looking at your diagram above) would be to look for 2 adjacent right angles turning the same direction. I'm sure that won't catch every case where a rectangle will help, but from a user's point of view, it's an obvious case (square corners on the outside = this ought to be a rectangle).
Once you've found an adjacent pair of right angles, take the length of the shorter leg, and there's one rectangle. Subtract this from the polygon left to tile, and repeat. When there's no more obvious external rectangles to remove, then do your normal tiling thing (Peter's answer sounds great) on that.
Disclaimer: I'm no expert on this, and I haven't even tried it...

Resources