Clipping and triangulating a triangle with a non convex polygon - 2d

I'm starting with a single 2D triangle that I want to clip with a (potentially) convex 2D polygon. It's not self-intersecting, but may 'keep' or 'discard' the intersecting area based on the winding order.
I want to end up with a triangulation, i.e. a list of n vertices and m triangles, defined by 3 vertices each, of the clipped region in 2D space.
What would be the easiest (for me as a developer), and what the fastest (in terms of computation) way to achieve this?

If I a right, you want to clip inside the polygon, i.e. get the intersection between the triangle and the polygon.
As the triangle is a convex shape, the Sutherland-Hodgman algorithm is appropriate and no too difficult to implement (https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm).
Notice that if the intersection is not simply connected, the resulting polygon will be connected, with double edges joining the would-be parts. Some cleanup might be required.
After finding the intersection, you re-triangulate using the ear-clipping method or a more efficient one (https://en.wikipedia.org/wiki/Polygon_triangulation).
Alternatively, you can triangulate the polygon and perform the clipping of every triangle with the original one.
The triangle-triangle clipping problem is again solved with Sutherland-Hodgman, somewhat simplified as the input polygons have a constant size, and their intersection is convex and at worse an hexagon. Trigulation of a convex polygon is immediate.

Related

Fit surface to 3 to 100 points where z values are 3D

I found myself in quite a big problem. I am average in math and I need to solve something, which is not very covered on the internet.
My problem: I have 2D space defined by X and Y. This space is just a drawing space. I want to assign to particular Xs,Ys a color with RGB values.
So let says I have 4 points with defined position in XY and color in Z:
[0,0, [255,0,0]]
[0,10, [0,255,0]]
[10,10,[0,0,255]]
[5,5, [0,0,0]]
and my drawing space is xy: 15x15.
And I want to distribute the colors to all empty points
For me its quite a delicate problem, because Z axis is basicly 3D space by itself.
My whole intention is to create a color map in which points 1,2,3,4 have between them smooth transition.
I am able to solve this in 1D where the transition is between 2 points. But I need to create 2D color map in XY drawing space based on fitted surface to these 4 points, which kind of depend both on the space of 3D-RGB and distance between them in XY drawing space.
Thanks in advance for help
You do not show any algorithm or code, so I will just explain a high-level algorithm. If you need more details or code or mathematical formulae, show more of your own work then ask. You do not explain just what you mean by "smooth transition"--there are multiple meanings. This will result in continuous shading but may not be smooth enough for your purposes.
First, given your points in the rectangular drawing space, find the Voronoi diagram for those points. This divides the drawing space into convex polygons, each polygon around one of your points.
For each vertex in the Voronoi diagram, figure which points are closest to the vertex--there will usually be just three of your points but there could be more. Then at that vertex point, assign the color that is the average of the RGB values of the nearby given points. That is, average the R values and the G values and the B values separately.
For any point on a Voronoi polygon edge, its color is the weighted average of the two colors at the endpoints. I.e. If the point is one-third of the distance from one end, its RGB value is one-third of the distance from the values at the endpoints.
Finally, for any point inside a Voronoi polygon, calculate the ray from the point that defined that polygon (the "center point") through the current point you are looking at. Find where that ray intersects the polygon. The RGB value is then the weighted average of the values of the center point and the polygon-intersection point.
The hardest part of all that is finding the Voronoi diagram. Fortune's algorithm can do this in a reasonable time. You can probably find a library to do that for you in your chosen programming language.
Another algorithm is to start with a triangulation of your given points and the corners of the drawing region. Then the color of any point in a triangle is the weighted average of the colors of the vertices. This will be automatically consistent for points on the vertices or edges of the triangles, so this is probably simpler than my previous algorithm. The difficulty here is finding a triangulation (any will do).

How to compute Voronoi tesselation based on manhattan distance in R

I am trying to compute a Voronoi tesselation in 2D with the Manhattan distance in R.
Ideally this would be a function that takes a set of two-dimensional points and outputs a list of polygons that partition the space. I am not certain what representations of Voronoi tesselations are standard.
There are of course many ways to do this with the Euclidean metric (packages like deldir and qhull make this very easy), but I haven't found a way to do this for the manhattan distance. A search using sos's findFn('voronoi') also yielded no results.
Info: taxicabgeometry.net
Interactive: Manhattan-metric Voronoi diagram(Click version)
I've been rolling my own in python, and can sum up the basics here:
Between neighboring centroids is a perpendicular line, in manhattan metric - two rays and a 45 degree diagonal most likely, if the centroids are randomly generated, but a straight horizontal, vertical, or 45 degree diagonal line may also occur. Given a set of such lines for every centroid pair, the edges separating the regions are among them. Collect intersect points of each pair of lines which are equal-distant (within an epsilon), in manhattan metric, to it's 3 nearest centroids. Also collect the two mid points of the 45 degree diagonal which are similarly equal-distant to their nearest two centroids. The outer polies won't be closed. How to deal with them depends on what you need. The poly borders and border verts will need sorting, so your polies aren't a zigzagged mess. Winding order can be determined if they should be clockwise or other. More can be done, just depends on what you need.
Unfortunately, this gets exponentially slower the more points are involved. The intersecting of every bisector to every other bisector is the bottleneck. I've been attempting an insertion method, with some success, but . Now I'm thinking to try first creating a nearest-neighbor linkage between the centroids. If the neighbors are known, the bisectors to intersect will be minimal, and many centroids can be computed quickly.
Anyway, the brute-force approach does work:
The point near the cursor is actually 2 points of a tiny diagonal. It's a precise method, but more complicated than it first seems. The java code from the interactive link above may be faster, but was difficult to get solid and precise geometry from.
Sorry, I dont know R.
Maybe the question is about finding the maximum area of a square that match inside a circumcircle (of a triangle). The equation for such a square abs(x)+abs(y)=r (www.mathematische-basteleien.de/taxicabgeometry.htm). When you have a mesh of triangles the voronoi diagram is the dual.

How can I detect if two polygons have the same shape?

I would like to check if two polygons (number of vectors unclear) have the same shape. Without rotations this is easy, but how do I do this with rotated polygons? I need to know the rotation angle, too.
boolean polygonsHaveSameShape(PVector[] polygon1, PVector[] polygon2){
…
}
float getRotationAngle(PVector[] polygon1, PVector[] polygon2){
…
}
With a small number of vertices it might be worth checking the distances between each vertex and the others.
In your square example dist(p1,p2), dist(p1,p3), dist(p1,p4), dist(p2,p3), dist(p2,p4) and dist(p3,p4). These values will exist for each polygon. There will be a point that has the same distance set as p1, and as p2, and so on.
Once you have a vertex in one polygon where all the distances connected to it are the same as those in the second polygon then you can use one of those lines to determine the angle of rotation.
Hope that made sense.

Subtraction and addition of convex polygons

I have two 2D convex polygons. I want to
subtract one from the other one and
add one to the other one
The resulting polygon must be either concave or a - better - a set of convex polygons (e.g. triangles).
Do you have any idea how I can accomplish this?

Determining the cut of any given polygon and pyramid

I am trying to implement in C++ a function that determines the cut of any given polygon and pyramid.
This has actually turned out to be far simpler than I had first imagined.
Firstly for each edge of the pyramid, test line-plane intersection (the given polygon is a plane, made up of 3 points). This will result in the new vertices at the cutting plane.
Secondly, since the polygon is not an infinite plane one needs to test for line-line intersection between the polygon edges (three) and each of the edges.
Indeed, this is not a simple problem. For simplicity, let's assume that there are no parallel line segments.
First determine the plane where your convex polygon is in. Then detemerine the intersection of that plane with the pyramid. This results in a second convex polygon.
Now you should find the intersection of the two convex polygons. How this can be done, you can find here.

Resources