My problem is to find if a generic (convex or concave) polygon and a rectangular polygon in 3D space has a not-null intersection. Each polygon is defined by the set of the ordinated contour points (if point p1 is after/before point p2 the edge p1-p2 exists).
It is easy to find the intersection line of the two plane of the polygons so the problem is finding the intersections of a line and the finite polygons and if the resulting intersections have a portion in common. I found algorithms for the intersection of a line and a convex polygon but I can't find anything for the general case of concave polygon.
Any suggestion?
Thank you
find the intersection point of the plane-intersection line with every edge of both figures. From there its s straightforward problem of looking at the ordering of the points on the line to check for any overlap.
Of course the special case where they are coplanar is a whole other problem..
There are usually no fast solutions for concave polygon intersection / containment/ etc queries.
The general solution is always to triangulate the polygon into a series of convex triangles, then run your intersection test with those triangles.
If you can rely on the polygon to be planar, you can first intersect the line with the plane and then transform the intersection point into the coordinate system of the plane.
Assuming you have also transformed all the vertices of the polygon, the problem is now to decide whether the 2D intersection point is within a 2D polygon.
Related
Given two convex polygons P, Q separated by a line, how can I find their common tangents?
There should be 4 total.
Geometry isn't my strong side so any help will be appreciated :)
Any tangent will be generated from (at least) one point on each polygon. The tangent forms a half-space for each polygon, containing that entire polygon; different tangents are generated by requiring that each polygon be above or below the tangent line.
Whenever you have a problem involving two convex polygons, the algorithm probably involves "pick a point on each one, then iterate." This algorithm is no different.
What you do, basically, is start with a random guess, and refine. Pick a vertex on each polygon and calculate the line through the two vertices. Look at polygon A and see whether either of the two neighboring vertices is on the wrong side of the line. If one is, replace the current vertex with that vertex. Then do the same check on polygon B. Then, if you updated either vertex, repeat. Eventually the lines will converge to tangents with each vertex the extremal one in the desired direction.
I would like to calculate Non-Fit Polygon (NFP) for two convex polygons.
I have read the article below and found an algorithm (Algorithm 1, pp. 18, See also the picture).
https://www.diva-portal.org/smash/get/diva2:699750/FULLTEXT01.pdf
Intuitively, this algorithm finds an NFP by moving one polygon (orbit polygon) around the other (fixed polygon), keeping the orbit polygon touching the fixed polygon but not intersecting (assuming both polygons don't rotate).
More concretely, It sorts the edges of the polygons by their angle, gets a polygon by connecting them as an NFP, and places the NFP based on a "reference point" of the orbit polygon.
However, I could not understand how to determine the reference point.
It seems that we could not set an arbitrary reference point because the polygons intersect if we choose the wrong reference point.
Any idea to choose a correct reference point?
I have two 3D polygonal chains and I want to know if they intersect or not. This problem is known as the Polygonal Chain Intersection. Good algorithms exists for the 2D case, but I have not seen any for the 3D case
Anybody can help with this?
Ok, maybe this is a stupid algorithm, but why don't you pick some plane (in general position, or one of the coordinate planes), project orthogonally the two 3D chains onto the plane and solve the 2D problem using one of these efficient algorithms, which will allow you to locate all points of intersection between the projections and all pairs of segments from the two 3D chains, whose projections intersect at the intersection points. Then from each intersection 2D point, draw the line through that point, perpendicular to the plane and check where this line intersects each of the two 3D segments, one from the first 3D chain, the other from the second, whose projections intersect at the 2D intersection point. If the resulting points are different, the 2D projected intersection point is not a true 3D intersection point. Else, you get an intersection point for the 3D chains.
I have been trying to find an algorithm which computes the intersecting area of two triangles but I failed to find any. Can anybody give a clue how to write this algorithm?
I would like something like:
double getAreaOfIntersection(Vector2 p1,Vector2 p2, Vector2 p3,Vector2 p4,Vector2 p5,Vector2 p6 )
where pX represents the 2 triangles.
You could first compute the polygon which describes the intersection area by a clipping algorithm, e.g.:
Sutherland-Hodgman algorithm
Then you would compute the area of the resulting convex polygon, which is rather easy, see, e.g., here:
Area of a Convex Polygon
Determining wether or not a point lies within a given polygon is easy (and even easier for triangles since those are simple polygons). You can use the winding number algorithm (and the crossing number algorithm for simple polygons) which is implemented and well explained here.
Using this you can obtain all the vertices of your intersection polygon:
The vertices pX of a triangle that are contained in the other triangle as well
The points where the two triangles intersect (see intersection of line segments)
You will need to loop over your edges to find all the intersection points, so this should be quick enough as long as you only want to determine intersections of triangles but i would not suggest to try to find intersections of arbitrary polygons this way.
What is the best algorithm to solve point in polygon in programming contests?
Shoot a ray(in arbitrary direction) from the point and check the number of times it has crossed the edges of polygon if it is even then the point is outside the polygon otherwise the point is inside the polygon.
If you need to do it for lots of query points, you can triangulate the polygon (actually triangulate both inside and the region between the polygon the convex containing it) so that you could do ray shooting in O(log n)
if you have a convex polygon you can use this :
http://e-maxx.ru/algo/pt_in_polygon