How to fix non-coplannar polygons? - polygon

I have a 3-D object described by surface facets. It is saved as OFF file. However, some of the vertices in a surface facet (polygon) are not exactly coplannar. Is there any good idea to fix these non-coplannar polygons to let them become coplannar?
My idea is
1) for every polygon, find the base plane of the polygon that most vertices fall into the plane, for example, the distance of vertex to the plane is less than machine precision (but how to find this base plane?);
2) for these vertices that are not fall into the base plane of the polygon, find the intersection of vertex's associated polygons' base planes and take it as the new location of this vertex.
Is there any problem? Do you have any better idea?
Thanks,
Tang Laoya

Maybe you should concentrate on fixing quadrilaterals, and repeat that over and over. Say two adjacent triangles have vertices abc and cbd, sharing edge bd.
Compute the volume of the tetrahedron abcd, and if it is small, decided to flatten. Or, maybe better, compute the dihedral angle at bd and flatten if
near pi; yes, that is probably a better measure.
Once you decide you would like to repair abcd, project d to the plane determined by abc, and replace d with that projected point. Now abcd are coplanar. You could do this for all the permutations and replace the point that moves the least.
Unfortunately, it seems this is order-dependent and could lead to cycles when
repeated over and over on all pairs of triangles.

Related

Coordinates of vectors going out from single point

I'm trying to build a visualization. The idea is to form a sphere out of circles. Similar to this:
In other words I want to pack certain number, lets say N, of circles in a sphere. All circles should have same radius and closest circles should have same distance from their centers.
I know how to draw a circle so the real problem is to find centers of the circles. How to find points on sphere where all neighboring points will be located on the same distance from each other and what is this distance is equal to.
Describing this task as a function it would have following notation
[RadiusOfSphere, NumberOfPoints] -> [CenterOfCircle, MaximumCircleRadius]
As the problem is stated, there may be no possible solution. The obstacle is that not every number of points N may admit a packing of the kind you are specifying.
You can take the radius to be fixed at 1 and scale afterwards in all cases here.
The requirement that the circles have the same radius and have the same distance to nearest neighbors is tantamount to saying that nearest neighbors are tangent to each other. All nearest neighbors, under this assumption, have the same gap on a line segment connecting their centers. Expand the radius by one-half this gap and now all closest circles are tangent. The centers don't move under this transformation. So we can assume the circles are tangent.
Circle packing is not a trivial mathematical problem, and proofs minimality or non-existence have tended to require computer-assisted proofs. I don't even know about that existence for arbitrary N. There might be a result out there, but it didn't come up in a brief search.
If you don't need every N, but simply a series of them so you can get N big enough, every quasiregular polyhedron and their duals (such as the rhombic triacontahedron, which folks on here might know better as a 30-sided die) all admit circle packings. So do their subdivisions (the figure induced by splitting edges into k pieces). So for given number of circles >N, pick one of the above polyhedrons and subdivide it so that the number of faces is large enough.

circles and triangles problem

I have an interesting problem here I've been trying to solve for the last little while:
I have 3 circles on a 2D xy plane, each with the same known radius. I know the coordinates of each of the three centers (they are arbitrary and can be anywhere).
What is the largest triangle that can be drawn such that each vertex of the triangle sits on a separate circle, what are the coordinates of those verticies?
I've been looking at this problem for hours and asked a bunch of people but so far only one person has been able to suggest a plausible solution (though I have no way of proving it).
The solution that we have come up with involves first creating a triangle about the three circle centers. Next we look at each circle individually and calculate the equation of a line that passes through the circle's center and is perpendicular to the opposite edge. We then calculate two intersection points of the circle. This is then done for the next two circles with a result of 6 points. We iterate over the 8 possible 3 point triangles that these 6 points create (the restriction is that each point of the big triangle must be on a separate circle) and find the maximum size.
The results look reasonable (at least when drawn out on paper) and it passes the special case of when the centers of the circles all fall on a straight line (gives a known largest triangle). Unfortunate i have no way of proving this is correct or not.
I'm wondering if anyone has encountered a problem similar to this and if so, how did you solve it?
Note: I understand that this is mostly a math question and not programming, however it is going to be implemented in code and it must be optimized to run very fast and efficient. In fact, I already have the above solution in code and tested to be working, if you would like to take a look, please let me know, i chose not to post it because its all in vector form and pretty much impossible to figure out exactly what is going on (because it's been condensed to be more efficient).
Lastly, yes this is for school work, though it is NOT a homework question/assignment/project. It's part of my graduate thesis (abet a very very small part, but still technically is part of it).
Thanks for your help.
Edit: Heres a new algorithm that i came up with a little while ago.
Starting at a circle's centre, draw a line to the other two centres. Calculate the line that bisects the angle created and calculate the intersections between the circle and the line that passes through the centre of your circle. You will get 2 results. Repeat this for the other two circles to get a total of 6 points. Iterate over these 6 points and get 8 possible solutions. Find the maximum of the 8 solutions.
This algorithm will deal with the collinear case if you draw your lines in one "direction" about the three points.
From the few random trials i have attempted using CAD software to figure out the geometries for me, this method seems to outperform all other methods previously stated However, it has already been proven to not be an optimal solution by one of Victor's counter examples.
I'll code this up tomorrow, for some reason I've lost remote access to my university computer and most things are on it.
I've taken the liberty of submitting a second answer, because my original answer referred to an online app that people could play with to get insight. The answer here is more a geometric argument.
The following diagram illuminates, I hope, what is going on. Much of this was inspired by #Federico Ramponi's observation that the largest triangle is characterized by the tangent at each vertex being parallel to the opposite side.
(source: brainjam.ca)
The picture was produced using a trial version of the excellent desktop program Geometry Expressions. The diagram shows the three circles centered at points A,E, and C. They have equal radii, but the picture doesn't really depend on the radii being equal, so the solution generalizes to circles of different radii. The lines MN, NO, and OM are tangent to the circles, and touch the circles at the points I,H, and G respectively. The latter points form the inner triangle IHG which is the triangle whose size we want to maximize.
There is also an exterior triangle MNO which is homethetic to the interior triangle, meaning that its sides are parallel to that of IHG.
#Federico observed that IHG has maximal area because moving any of its vertices along the corresponding circle will result an a triangle that has the same base but less height, therefore less area. To put it in slightly more technical terms, if the triangle is parameterized by angles t1,t2,t3 on the three circles (as pointed out by #Charles Stewart, and as used in my steepest descent canvas app), then the gradient of the area w.r.t to (t1,t2,t3) is (0,0,0), and the area is extremal (maximal in the diagram).
So how is this diagram computed? I'll admit in advance that I don't quite have the full story, but here's a start. Given the three circles, select a point M. Draw tangents to the circles centered at E and C, and designate the tangent points as G and I. Draw a tangent OHN to the circle centered at A that is parallel to GI. These are fairly straightforward operations both algebraically and geometrically.
But we aren't finished. So far we only have the condition that OHN is parallel to GI. We have no guarantee that MGO is parallel to IH or that MIN is parallel to GH. So we have to go back and refine M. In an interactive geometry program it's no big deal to set this up and then move M until the latter parallel conditions are met (by eyeballs, anyways). Geometry Expressions created the diagram, but I used a bit of a cheat to get it to do so, because its constraint solver was apparently not powerful enough to do the job. The algebraic expressions for G, I, and H are reasonably straightforward, so it should be possible to solve for M based on the fact that MIHG is a parallelogram, either explicitly or numerically.
I should point out that in general if you follow the construction starting from M, you have two choices of tangent for each circle, and therefore eight possible solutions. As in the other attempted answers to the question, unless you have a good heuristic to help you choose in advance which of the tangents to compute, you should probably compute all eight possible triangles and find the one with maximum area. The other seven will be extremal in the sense of being minimal area or saddle points.
That's it. This answer is not quite complete in that it leaves the final computation of M somewhat open ended. But it's reduced to either a 2D search space or the solution of an ornery but not humongous equation.
Finally, I have to disagree with #Federico's conclusion that this confirms that the solution proposed by the OP is optimal. It's true that if you draw perpendiculars from the circle centers to the opposite edge of the inner triangle, those perpendiculars intersect the circle to give you the triangle vertex. E.g. H lies on the line through A perpendicular to GI), but this is not the same as in the original proposed solution (which was to take the line through A and perpendicular to EC - in general EC is not parallel to GI).
I've created an HTML5 canvas app that may be useful for people to play with. It's pretty basic (and the code is not beautiful), but it lets you move three circles of equal radius, and then calculates a maximal triangle using gradient/steepest descent. You can also save bitmaps of the diagram. The diagram also shows the triangle whose vertices are the circle centers, and one of the altitudes. Edit1: the "altitude" is really just a line segment through one of the circle centers and perpendicular to the opposite edge of the triangle joining the centers. It's there because some of the suggested constructions use it. Edit2: the steepest descent method sometimes gets stuck in a local maximum. You can get out of that maximum by moving a circle until the black triangle flips and then bringing the circle back to its original position. Working on how to find the global maximum.
This won't work in IE because it doesn't support canvas, but most other "modern" browsers should work.
I did this partially because I found some of the arguments on this page questionable, and partially because I've never programmed a steepest descent and wanted to see how that worked. Anyways, I hope this helps, and I hope to weigh in with some more comments later.
Edit: I've looked at the geometry a little more and have written up my findings in a separate answer.
Let A, B, C be the vertexes of your triangle, and suppose they are placed as in your solution.
Notice that the key property of your construction is that each of the vertexes lies on a tangent to its circle which is parallel to the opposite side of the triangle. Obviously, the circle itself lies entirely on one side of the tangent, and in the optimal solution each tangent leaves its circle on the same side as the other vertexes.
Consider AB as the "base" of the triangle, and let C float in its circle. If you move C to another position C' within the circle, you will obtain another triangle ABC' with the same base but a smaller height, hence also with a smaller area:
figure 1 http://control.ee.ethz.ch/~ramponif/stuff/circles1.png
For the same reason, you can easily see that any position of the vertexes that doesn't follow your construction cannot be optimal. Suppose, for instance, that each one of the vertexes A', B', C' does not lie on a tangent parallel to the side connecting the other two.
Then, constructing the tangent to the circle that contains (say) C', which is parallel to A'B' and leaves the circle on the same side as A'B', and moving C' to the point of tangency C, it is always possible to construct a triangle A'B'C which has the same base, but a greater height, hence also a greater area:
figure 2 http://control.ee.ethz.ch/~ramponif/stuff/circles2.png
Since any triangle that does not follow your construction cannot be optimal, I do believe that your construction is optimal. In the case when the centers of the circles are aligned I'm a bit confused, but I guess that it is possible to prove optimality along the same lines.
I believe this is a convex optimization problem (no it's not, see below), and hence can be solved efficiently using well known methods.
You essentially want to solve the problem:
maximize: area(v1,v2,v3) ~ |cross((v2-v1), (v3-v1))|
such that: v1 in C1, v2 in C2, v3 in C3 (i.e., v_i-c_i)^2 - r_i^2 <= 0)
Each of the constraints are convex, and the area function is convex as well. Now, I don't know if there is a more efficient formulation, but you can at least use an interior point method with derivatives since the derivative of the area with respect to each vertex position can be worked out analytically (I have it written down somewhere...).
Edit: grad(area(v1,v2,v3))(v_i) = rot90(vec(vj,vk)), where vec(a,b) is making a 2D vector starting at a and ending at b, and rot90 means a positive orientation rotation by 90 degrees, assuming (vi,vj,vk) was positively oriented.
Edit 2: The problem is not convex, as should be obvious considering the collinear case; two degenerate solutions is a sure sign of non-convexity. However, the configuration starting at the circle centers should be in the globally optimal local maximum.
Not optimal, works well when all three are not colinear:
I don't have a proof (and therefore don't know if it's guaranteed to be biggest). Maybe I'll work on one. But:
We have three circles with radius R with positions (from center) P0, P1, and P2. We wish to find the vertices of a triangle such that the area of the triangle is maximum, and the vertices lie on any point of the circles edges.
Find the center of all the circles and call that C. Then C = (P0 + P1 + P2) / 3. Then we find the point on each circle farthest from C.
Find vectors V0, V1, and V2, where Vi = Pi - C. Then find points Q0, Q1, and Q2, where Qi = norm(Vi) * R + Pi. Where norm indicates normalization of a vector, norm(V) = V / |V|.
Q0, Q1, and Q2 are the vertices of the triangle. I assume this is optimal because this is the farthest the vertices could be from each other. (I think.)
My first thought is that you should be able to find an analytic solution.
Then the equations of the circles are:
(x1-h1)^2 + (y1-k1)^2 = r^2
(x2-h2)^2 + (y2-k2)^2 = r^2
(x3-h3)^2 + (y3-k3)^2 = r^2
The vertices of your triangle are (x1, y1), (x2, y2), and (x3, y3). The side lengths of your triangle are
A = sqrt((x1-x2)^2 + (y1-y2)^2)
B = sqrt((x1-x3)^2 + (y1-y3)^2)
C = sqrt((x2-x3)^2 + (y2-y3)^2)
So the area of the triangle is (using Heron's formula)
S = (A+B+C)/2
area = sqrt(S(S-A)(S-B)(S-C))
So area is a function of 6 variables.
At this point I realize this is not a fruitful line of reasoning. This is more like something I'd drop into a simulated annealing system.
So my second thought is to choose the point on circle with centre A as follows: Construct line BC joining the centres of the other two circles, then construct the line AD that is perpendicular to BC and passes through A. One vertex of the triangle is the intersection of AD and circle with centre A. Likewise for the other vertices. I can't prove this but I think it gives different results than the simple "furthest from the centre of all the circles" method, and for some reason it feels better to me. I know, not very mathematical, but then I'm a programmer.
Let's assume the center of the circles to be C0,C1 and C2; and the radius R.
Since the area of a triangle is .5*base*height, let's first find the maximum base that can be constructed with the circles.
Base = Max {(|C0-C1|+2R),(|C1-C2|+2R,(|C2-C0|+2R}
Once the base length is determined between 2 circles, then we can find the farthest perpendicular point from the base line to the third circle. (product of the their slopes is -1)
For special cases such as circles aligned in a single line, we need to perform additional checks at the time of determining the base line.
It appears that finding the largest Apollonius circle for the three circles and then inscribing an equilateral triangle in that circle would be a solution. Proof left as an exercise ;).
EDIT
This method has issues for collinear circles like other solutions here, too and doesn't work.
Some initial thoughts.
Definition Call the sought-after triangle, the maximal triangle. Note that this might not be unique: if the circles all have the same centre, then there are infinitely many maximal triangles obtained by rotation around the center, and if the centres are colinear, then there will be two maximal triangles, each a mirror image of the other.
Definition Call the triangle (possibly, degenerately, either a point or a line) whose vertices are the centres of the circles the interior triangle.
Observation The solution can be expressed as three angles, indicating where on the circumference of each circle the triangle is to be found.
Observation Given two exterior vertices, we can determine a third vertex that gives the maximal area: draw the altitude of the triangle between the two exterior vertices and the centre of the other circle. This line intersects the circumference in two places; the further away point is the maximising choice of third vertex. (Fixed incorrect algorithm, Federico's argument can be adapted to show correctness of this observation)
Consequence The problem is reduced to from a problem in three angles to one in two.
Conjecture Imagine the diagram is a pinboard, with three pins at the three centres of the circles. Imagine also a closed loop of string of length equal to the perimiter of the interior triangle, plus the radius of a circle, and we place this loop around the pins. Take an imaginary pen and imaginarily draw the looping figure where the loop is always tight. I conjecture that the points of the maximal triangle will all lie on this looping figure, and that in the case where the interior triangle is not degenerate, the vertices of the maximal triangle will be the three points where the looping figure intersects one of the circle circumferences. Many counterexamples
More to follow when I can spare time to think about it.
This is just a thought, no proof or math to go along with the construction just yet. It requires that the circle centers not be colinear if the radii are the same for each circle. This restriction can be relaxed if the radii are different.
Construction:
(1) Construct a triangle such that each side of the triangle is tangent to two circles, and therefore, each circle has a tangent point on two sides of the triangle.
(2) Draw the chord between these two tangent points on each circle
(3) Find the point on the boundary of the circle on the extended ray starting at the circle's center through the midpoint of the chord. There should be one such point on each of the three circles.
(4) Connect them three points of (3) to fom a triangle.
At that point I don't know if it's the largest such triangle, but if you're looking for something approximate, this might be it.
Later: You might be able to find an approximate answer for the degenerate case by perturbing the "middle" circle slightly in a direction perpendicular to the line connecting the three circles.

Closest Approach question for math/physics heads

I'm using a Segment to Segment closest approach method which will output the closest distance between two segments of length. Each segment corresponds to a sphere object's origin and destination. The speed is simply from one point, to the other.
Closest Approach can succeed even when there won't be a real collision. So, I'm currently using a 10-step method and calculating the distance between 2 spheres as they move along the two segments. So, basically the length of each segment is the object's traverse in the physics step, and the radius is the objects radius. By stepping, I can tell where they collide, and if they collide (Sort of; for the MOST part.)..
I get the feeling that there could be something better. While I sort of believe that the first closest approach call is required, I think that the method immediately following it is a TAD weak. Can anyone help me out? I can illustrate this if needed.
Thanks alot!
(source: yfrog.com)
(I don't know how to post graphics; bear with me.)
All right, we have two spheres with radii r1 and r2, starting at locations X1 and X2, moving with velocities V1 and V2 (X's and V's are vectors).
The velocity of sphere 1 as seen from sphere 2 is
V = V1-V2
and its direction is
v = V/|V|
The distance sphere 1 must travel (in the frame of sphere 2) to closest approach is
s = Xv
And if X is the initial separation, then the distance of closest approach is
h = |X - Xv|
This is where graphics would help. If h > r1+r2, there will be no collision. Suppose h < r1+r2. At the time of collision, the two sphere centers and the point of closest approach will form a right triangle. The distance from Sphere 1's center to the point of closest approach is
u = sqrt((r1 + r2)^2 - h^2)
So the distance sphere 1 has traveled is
s - u
Now just see if sphere 1 travels that far in the given interval. If so, then you know exactly when and where the spheres were (you must shift back from sphere 2's frame, but that's pretty easy). If not, there's no collision.
Closest approach can be done without simulating time if the position function is invertible and explicit.
Pick a path and object.
Find the point on the path where the two paths are closest. If time has bounds (e.g. paths are line segments), ignore the bounds in this step.
Find the time at which the object is at the point from the previous step.
If time has bounds, limit the picked time by the bounds.
Calculate the position of the other object at the time from the previous step.
Check if the objects overlap.
This won't work for all paths (e.g. some cubic), but should work for linear paths.

A simple algorithm for polygon intersection

I'm looking for a very simple algorithm for computing the polygon intersection/clipping.
That is, given polygons P, Q, I wish to find polygon T which is contained in P and in Q, and I wish T to be maximal among all possible polygons.
I don't mind the run time (I have a few very small polygons), I can also afford getting an approximation of the polygons' intersection (that is, a polygon with less points, but which is still contained in the polygons' intersection).
But it is really important for me that the algorithm will be simple (cheaper testing) and preferably short (less code).
edit: please note, I wish to obtain a polygon which represent the intersection. I don't need only a boolean answer to the question of whether the two polygons intersect.
I understand the original poster was looking for a simple solution, but unfortunately there really is no simple solution.
Nevertheless, I've recently created an open-source freeware clipping library (written in Delphi, C++ and C#) which clips all kinds of polygons (including self-intersecting ones). This library is pretty simple to use: https://github.com/AngusJohnson/Clipper2
You could use a Polygon Clipping algorithm to find the intersection between two polygons. However these tend to be complicated algorithms when all of the edge cases are taken into account.
One implementation of polygon clipping that you can use your favorite search engine to look for is Weiler-Atherton. wikipedia article on Weiler-Atherton
Alan Murta has a complete implementation of a polygon clipper GPC.
Edit:
Another approach is to first divide each polygon into a set of triangles, which are easier to deal with. The Two-Ears Theorem by Gary H. Meisters does the trick. This page at McGill does a good job of explaining triangle subdivision.
If you use C++, and don't want to create the algorithm yourself, you can use Boost.Geometry. It uses an adapted version of the Weiler-Atherton algorithm mentioned above.
You have not given us your representation of a polygon. So I am choosing (more like suggesting) one for you :)
Represent each polygon as one big convex polygon, and a list of smaller convex polygons which need to be 'subtracted' from that big convex polygon.
Now given two polygons in that representation, you can compute the intersection as:
Compute intersection of the big convex polygons to form the big polygon of the intersection. Then 'subtract' the intersections of all the smaller ones of both to get a list of subracted polygons.
You get a new polygon following the same representation.
Since convex polygon intersection is easy, this intersection finding should be easy too.
This seems like it should work, but I haven't given it more deeper thought as regards to correctness/time/space complexity.
Here's a simple-and-stupid approach: on input, discretize your polygons into a bitmap. To intersect, AND the bitmaps together. To produce output polygons, trace out the jaggy borders of the bitmap and smooth the jaggies using a polygon-approximation algorithm. (I don't remember if that link gives the most suitable algorithms, it's just the first Google hit. You might check out one of the tools out there to convert bitmap images to vector representations. Maybe you could call on them without reimplementing the algorithm?)
The most complex part would be tracing out the borders, I think.
Back in the early 90s I faced something like this problem at work, by the way. I muffed it: I came up with a (completely different) algorithm that would work on real-number coordinates, but seemed to run into a completely unfixable plethora of degenerate cases in the face of the realities of floating-point (and noisy input). Perhaps with the help of the internet I'd have done better!
I have no very simple solution, but here are the main steps for the real algorithm:
Do a custom double linked list for the polygon vertices and
edges. Using std::list won't do because you must swap next and
previous pointers/offsets yourself for a special operation on the
nodes. This is the only way to have simple code, and this will give
good performance.
Find the intersection points by comparing each pair of edges. Note
that comparing each pair of edge will give O(N²) time, but improving
the algorithm to O(N·logN) will be easy afterwards. For some pair of
edges (say a→b and c→d), the intersection point is found by using
the parameter (from 0 to 1) on edge a→b, which is given by
tₐ=d₀/(d₀-d₁), where d₀ is (c-a)×(b-a) and d₁ is (d-a)×(b-a). × is
the 2D cross product such as p×q=pₓ·qᵧ-pᵧ·qₓ. After having found tₐ,
finding the intersection point is using it as a linear interpolation
parameter on segment a→b: P=a+tₐ(b-a)
Split each edge adding vertices (and nodes in your linked list)
where the segments intersect.
Then you must cross the nodes at the intersection points. This is
the operation for which you needed to do a custom double linked
list. You must swap some pair of next pointers (and update the
previous pointers accordingly).
Then you have the raw result of the polygon intersection resolving algorithm. Normally, you will want to select some region according to the winding number of each region. Search for polygon winding number for an explanation on this.
If you want to make a O(N·logN) algorithm out of this O(N²) one, you must do exactly the same thing except that you do it inside of a line sweep algorithm. Look for Bentley Ottman algorithm. The inner algorithm will be the same, with the only difference that you will have a reduced number of edges to compare, inside of the loop.
The way I worked about the same problem
breaking the polygon into line segments
find intersecting line using IntervalTrees or LineSweepAlgo
finding a closed path using GrahamScanAlgo to find a closed path with adjacent vertices
Cross Reference 3. with DinicAlgo to Dissolve them
note: my scenario was different given the polygons had a common vertice. But Hope this can help
If you do not care about predictable run time you could try by first splitting your polygons into unions of convex polygons and then pairwise computing the intersection between the sub-polygons.
This would give you a collection of convex polygons such that their union is exactly the intersection of your starting polygons.
If the polygons are not aligned then they have to be aligned. I would do this by finding the centre of the polygon (average in X, average in Y) then incrementally rotating the polygon by matrix transformation, project the points to one of the axes and use the angle of minimum stdev to align the shapes (you could also use principal components). For finding the intersection, a simple algorithm would be define a grid of points. For each point maintain a count of points inside one polygon, or the other polygon or both (union) (there are simple & fast algorithms for this eg. http://wiki.unity3d.com/index.php?title=PolyContainsPoint). Count the points polygon1 & polygon2, divide by the amount of points in polygon1 or Polygon2 and you have a rough (depending on the grid sampling) estimate of proportion of polygons overlap. The intersection area would be given by the points corresponding to an AND operation.
eg.
function get_polygon_intersection($arr, $user_array)
{
$maxx = -999; // choose sensible limits for your application
$maxy = -999;
$minx = 999;
$miny = 999;
$intersection_count = 0;
$not_intersected = 0;
$sampling = 20;
// find min, max values of polygon (min/max variables passed as reference)
get_array_extent($arr, $maxx, $maxy, $minx, $miny);
get_array_extent($user_array, $maxx, $maxy, $minx, $miny);
$inc_x = $maxx-$minx/$sampling;
$inc_y = $maxy-$miny/$sampling;
// see if x,y is within poly1 and poly2 and count
for($i=$minx; $i<=$maxx; $i+= $inc_x)
{
for($j=$miny; $j<=$maxy; $j+= $inc_y)
{
$in_arr = pt_in_poly_array($arr, $i, $j);
$in_user_arr = pt_in_poly_array($user_array, $i, $j);
if($in_arr && $in_user_arr)
{
$intersection_count++;
}
else
{
$not_intersected++;
}
}
}
// return score as percentage intersection
return 100.0 * $intersection_count/($not_intersected+$intersection_count);
}
This can be a huge approximation depending on your polygons, but here's one :
Compute the center of mass for each
polygon.
Compute the min or max or average
distance from each point of the
polygon to the center of mass.
If C1C2 (where C1/2 is the center of the first/second polygon) >= D1 + D2 (where D1/2 is the distance you computed for first/second polygon) then the two polygons "intersect".
Though, this should be very efficient as any transformation to the polygon applies in the very same way to the center of mass and the center-node distances can be computed only once.

How to determine ordering of 3D vertices

If I have 5 Vertices in 3D coordinate space how can I determined the ordering of those Vertices. i.e clockwise or anticlockwise.
If I elaborate more on this,
I have a 3D model which consists of set of polygons. Each polygon is collection of vertices and I want to calculate the norm of the polygon surface. To calculate the norm I have to consider the vertices in counter clockwise order . My question is given set of vertices how can I determine whether it is ordered in clockwise or counter clockwise?
This is for navigation mesh generation where I want to remove the polygons which cannot be walked by the agent. To do so my approach is to calculate the surface norm(perpendicular vector of the polygon) and remove the polygon based on the angle with 2D plane. To calculate the norm I should know in which order points are arranged. So for given set of points in polygon how can I determine the order of the arrangement of points.
Ex.
polygon1 consist of Vertex1 = [-21.847065 -2.492895 19.569759], Vertex2 [-22.279873 1.588395 16.017160], Vertex3 [-17.234818 7.132950 7.453146] these 3 points and how can I determine the order of them
As others have noted, your question isn't entirely clear. Is the for something like a 3D backface culling test? If so, you need a point to determine the winding direction relative to. Viewed from one side of the polygon the vertices will appear to wind clockwise. From the other side they'll appear to wind counter clockwise.
But suppose your polygon is convex and properly planar. Take any three consecutive vertices A, B, and C. Then you can find the surface normal vector using the cross product:
N = (B - A) x (C - A)
Taking the dot product of the normal with a vector from the given view point, V, to one of the vertices will give you a value whose sign indicates which way the vertices appear to wind when viewed from V:
w = N . (A - V)
Whether this is positive for clockwise and negative for anticlockwise or the opposite will depend on the handedness of your coordinate system.
Your question is too poorly defined to give a complete answer, but here's the skeleton of one.
The missing part (the meat if you will), is a function that takes any two coordinates and tells you which one is 'greater' than the other. Without a solid definition for this, you won't be able to make anything work.
The rest, the skeleton, is pretty simple. Sort your list of vectors using your comparison function. For five vectors, a simple bubble sort will be all you need, although if the number of vertices increases considerably you may want to look into a faster sorting algorithm (ie. Quicksort).
If your chosen language / libraries provide sorting for you, you've already got your skeleton.
EDIT
After re-reading your question, it also occurred to me that since these n vertices define a polygon, you can probably make the assumption that all of them lie on the same plane (if they don't, then good luck rendering that).
So, if you can map the vector coordinates to 2d positions on that plane, you can reduce your problem to ordering them clockwise or counterclockwise in a two dimensional space.
I think your confusion comes from the fact that methods for computing cross products are sometimes taught in terms of clockwiseness, with a check of the clockwiseness of 3 points A,B,C determining the sign of:
(B-A) X (C - A)
However a better definition actually determines this for you.
In general 5 arbitrary points in 3 dimensions can't be said to have a clockwise ordering but 3 can since 3 points always lie in a plane.

Resources