This question already has answers here:
Algorithm to generate random 2D polygon
(5 answers)
Closed 5 years ago.
It's possible to generate polygon with random shape, filled and with outline? Something like this:
Link to image:
https://image.ibb.co/kWAWk5/solid1.png
I mean what algorithm can be used? What programming language it uses doesn't matter.
Given a number of vertices v where v > 2,
Draw a straight edge ex between vertices v0 and v1, and add it to a list of edges e.
For each of the remaining vertices v2 through vn-1,
Randomly select one of the edges (ey) from e.
Randomly pick another point p that's not along ey.
Replace ey with 2 new edges: 1 from e's starting point to p, and 1 from e's ending point to p.
For the final vertex vn-1, add an edge from it to v0.
Fill the path.
Related
I have a list of 8 points given by their coordinates (x, y, z).
These 8 points are the 8 vertices of an hexahedron, which means that there are 6 faces which are quadrilaterals; the 4 points on each face are guaranteed to be coplanar.
I want to sort the list of points in a particular order:
the first 4 points must be on the same face, and in counterclockwise order;
the next 4 points must be their corresponding points on the opposite face.
Example:
I found similar question asked, but this algorithm fails on this example hexahedron:
I also have an idea to sort points in direct way by finding points in the same plane, then the other point in this plane, then point on same edge and etc.
But is there any easier way to order these points?
Let A and B be two sets of points, each consisting of n points, all lying in unit square S.
I am trying to find a efficient algorithm for finding the largest disk D such that:
(i) The center of D lie in S.
(ii) The interior of D is empty.
(iii) The boundary of D touches atleast one point from A and one point from B.
Im having a real problem with this question. Any hints will be usefull.
To complete Yves Daoust's partial solution, compute the Voronoi diagram (which is dual to the Delaunay triangulation) bounded by S. We can find an optimal circle center at some Voronoi vertex (i.e., a point in the interior of S where the nearest three points in A ∪ B are equidistant, or a point on the boundary of S where the nearest two points in A ∪ B are equidistant) where one of the nearest points is in A and another is in B.
Such a vertex is clearly feasible as a center. If we try to take any other center, then we can apply stark's observation. This center must be equidistant from a point in A and a point in B, so assuming that A ∩ B is empty (I really don't want to think about the degenerate cases; we can always perturb our way out), we can slide the center along the perpendicular bisector of AB until we hit either a third point or the boundary.
I am currently working on a node-based house-builder for Unity. The system is pretty simple in its workflow: users can create nodes, which are simply cubes, and connect them with each other to create walls. The mesh processing is already done and it works nice and smooth.
What I am trying to do now is to detect how many closed rooms have been created and what vertices are involved in each one of them. The possible inputs can be seen in the following images:
In the first picture, the loops would be
(1,5,3,4), (1,2,6,8,7,5), (6,9,12,11,10,8), (8,10,14,13) and (10,11,17,16,15,14).
In the second one they'd be
(1,2,5,6,8,7), (2,3,4,14,13,6,5), (6,13,12,11,10) and (8,6,10,9).
Each node can be connected to up to four other nodes, one per cardinal side, and every link is stored on both sides. I do not need the nodes to come in any particular order.
I thought I could use a generic loop-detection algorithm and recursively search for sub-loops until the loop I find has has no internal connections, but this would be extremely resource-consuming. There must be some properties I can use to detect loops with no internal connections without iterating over the graph so many times, but I haven't been able to find it.
Do you have any suggestion?
For the following algorithm to work, you need the following:
A unique direction of the edge (which you probably already have)
Two flags for every edge that specify if the edge has been used in the forward and the backward direction
A list of vertices with unused edges
Then the idea is the following. Take any node with unused edges and go along any of the unused edges to the neighbor (keep the direction in mind). Doing so, immediately mark the edge in the according direction as used. At this neighbor, you know the direction from which you came. Look in counter-clockwise order until you find the first unused edge (again watch out for the edge direction). You can also search in clockwise order, this will define the order of all your output faces. E.g. if you came from the left edge, then check the bottom, right, top edges, respectively. Go across this edge (mark as used) and repeat until you arrive at the start vertex. All visited vertices form your room.
Doing so, you should update the list of vertices with unused edges accordingly.
Eventually, you will also create a face for the border. You can detect this e.g. by calculating its orientation:
v1 x v2 + v2 x v3 + v3 x v4 + ... + vn x v1
, where v are the positions of the vertices and x represents the z-component of the cross product (which represents the face orientation):
(x1, y1) x (x2, y2) = (x1 * y2) - (x2 * y1)
The boundary face will have a different sign for this orientation than all other faces. The actual sign depends on whether you used counter-clockwise or clockwise order during the edge traversal.
This is an answer only to the first question, but it might help you with the second one. The number of closed rooms actually has a closed formula:
1 - V + E where V is the number of vertices and E is the number of edges. In your second example, there are 14 vertices, 17 edges and 4 rooms.
The mathematics are a bit complicated, but the key word is Euler characteristic.
Before I go into my problem, here are some definition that might be helpful:
A diagonal of a polygon P is a segment that joins two vertices of P and remains strictly inside P.
Steiner point: an extra point added to a vertex set that was not part of the original set, e.g. the vertex 7 in the polygon as shown in Fig. 1.
A link path is a polyline that is completely inside a polygon. The link path could be a diagonal of a polygon or a collection of line segments. For example, the line segment shown in red dash in Fig. 1 is not completely inside the polygon (intersect with other edge), we add a Steiner point, 7, using the algorithm here such that we have a 2-link path connect vertex 2 and 5.
We have two simple polygons without holes with the corresponding vertices labeled as 1, 2, …, n.
Let us first look at the simplified problem. As shown in Fig. 2, we have two polygons P and Q. I want to start from the concave polygon P and decompose P into a set of convex sub-polygons using only diagonals without introducing any Steiner points into polygon P. At the same time, we also find the link path in the other polygon Q using the same vertex pair in polygon Q.
For example, we use the diagonal connecting vertex 2 and 5 in P to partition P as shown in Fig. 3. We find the link path that connects vertex 2 and 5 in polygon Q, which introduces a Steiner point 7. (Here, let us just assume we can always find the link path in polygon Q with the smallest number of Steiner points needed)
We want to partition polygon P using diagonals of P while introducing the smallest number of Steiner points into polygon Q. We iteratively decompose P and Q until all the sub-polygon pairs contain a convex polygon. For example, in Fig. 3, sub-polygon pair P_{2345} and Q_{2345} has already contains a convex polygon P_{2345} and we do not need to decompose them anymore. Then, we need to consider sub-polygon pair P_{1256} and Q_{1256} as either of them is convex.
Here is one example of partitioning polygon P and Q with one Steiner point 7 as shown in Fig. 4.(We add a corresponding point 7 on the diagonal that connects vertex 2 and 5 in P based a linear ratio. The linear ratio is computed by the length of line segment (2,7) in polygon Q overt the total length of the link path that connects vertex 2 and 5 in Q.)
Here are some more cases on convexly decompose polygon P and Q as shown in Fig. 5.
More general problem:
(1) At the beginning, we can start from either polygon P or Q.
(2) During the partition process, we can also starting from the other corresponding sub-poly.
For example, at the beginning, we start from polygon P to decompose P using diagonal that connecting vertex 2 and 4 in P. Then, for sub-polygon pair P_{12456} and Q_{12456}, we could start from Q_{12456} instead of P_{12456} for decomposing. Say, we pick diagonal that connecting vertex 4 and 6 in Q shown in Fig. 6.
I realized that if I want to find the optimum solution there would be exponential combinations. My question is, is there any algorithm can speed it up?
I have a list a vertices and a list of triangles. I'd like to split this single mesh into, say 5, randomly shaped meshes. When the 5 randomly shaped meshes are in place the sphere should all line up and look like 1 solid mesh.
I need a algorithm to do this programmatically, not a tool to do it form me. Any pointers would be great!
Similar to Svante's proposal but a slightly different approach:
Select five random vertices, mark these vertices as "visited" with the number 1 to 5
From each of the visited vertices, go to all adjacent vertices. Store the same number there as well.
If you visit a vertex which already has a number assigned, stop there
Stop, if all vertices are visited. All vertices marked with the same number end up in the same piece
This appears to me to be the simplest to implement, while still resulting in nice puzzle pieces. For added random-ness, you could add a probability, of visiting each adjacent vertex.
Nevertheless, any "too random" approach might result in heavily concave pieces, like very long pieces consisting only of a long strip of single triangles; and pieces with deep ugly thin cuts into them. You should possibly specify another question on how to make nice puzzle pieces (and what nice puzzle pieces are!) if you care for that.
You could make a crack by walking a random walk across the edges, until the number of pieces you want is achieved. If you want to have mostly big pieces, you could modify the randomness of the walk by reducing the attractiveness of edges that are close to an existing crack.
Select 3 random points and split along that plane. What is the problem?
When you split a triangle along a plane you end up in one of two situations: either the plane doesn't intersect either line segments in the triangle or it intersects exactly two line segments. Only triangles that are intersected are interesting.
If you have triangle (A, B, C), with A, B and C being vertices.
Assume that the plane intersects the line segments (A, B) and (A, C) in the points D and E.
Define a vertex, F, between B and C (for example B + (C - B) / 2), but any vertex on the line segment between B and C will do).
Your new triangles are then the following
(A, D, E), (B, D, F), (D, E, F) and (C, E, F)