Closest pair - too many points? - algorithm

Let's examine this picture a bit.
Basically we follow the well-known steps: we sort the points, we separate the array of points in half and we recursively compute the smallest distance from the right side and from the left side.
And we consider δ as being the minimum of the two computed distances.
Let's consider a point p, from the left side. Now we have these assumptions:
"All points from the right side, within δ distance of p reside in a δ x 2δ rectangle, R. If each pair is at least δ apart, then there are at most 6 points inside R".
These assumptions are a bit ambiguous.
1. Where exactly should we place the rectangle? Should A be the projection of p on the border?
2. The 6 points "inside" R are actually the vertices and 2 of the midpoints of the rectangle?
3. Why are the 3 points inside the red circle candidates? The distance from A to the ones that are vertices is δ√2 > δ. And if we consider the distance between p and A being x, then the distance between p and the other point (the midpoint) would be x + δ > δ.
Source: https://www.cs.ucsb.edu/~suri/cs235/ClosestPair.pdf

The rectangle shall be placed in region P2, and yes, A shall be a projection of p on the border. The left side of the rectangle shall coincide with the median line.
The 6 points are the maximum number of points that can be found in the region R because the minimum distance between any two pairs of points is delta. If there are 6 points then yes the location of those points shall be as you described.
There can be a case where p coincides with A. Hence we know that except the two right-most vertex points, all the other 4 points can be valid candidates. Now the points on the vertex cannot by themselves be candidates but they act as the boundaries for points that we should consider as candidates.

Related

Maximum enclosing disk

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.

On convex decomposition of two simple polygons with the smallest number of Steiner points

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?

Algorithm to join 4 co-ordinates such that it always results in a quadilateral

We're given the coordinates of 4 points on the 2D plane. How can we find an order to join them with lines to form a quadilateral (whenever it's possible)?
Consider the partition of the plane obtained by drawing the three lines defined by the first three points. It defines 7 regions. You can easily find to which region the fourth point belongs by means of three signed area tests (algebraic area of the triangles ABD, BCD, CAD).
Drawing a quadrilateral in every case is straightforward (there can be one, two or three solutions per case).
In the example below, with D in the region -++, ADBC will do.
Actually two area evaluations are enough: if the first test returns - (regions -+-, -++ or --+), ADBC is a solution, else if the second test returns - (regions +-+ or +--), ABDC is a solution, else (regions ++- or +++) ABCD is a solution .
Consider the affine transform
Px = Ax + u (Bx - Ax) + v (Cx - Ax)
Py = Ay + u (By - Ay) + v (Cy - Ay)
It maps (0, 0) to A, (1, 0) to B and (0, 1) to C. (This puts the triangle ABC in a canonical position.)
Solving the 2x2 linear system
Dx = Ax + u (Bx - Ax) + v (Cx - Ax)
Dy = Ay + u (By - Ay) + v (Cy - Ay)
gives you the values of (u, v) corresponding to D.
Then,
if u < 0 => ABCD
else if v < 0 => BCAD
else => CABD
The resulting quadrilateral has the same orientation as the triangle ABC.
For the sake of clarity, I'm considering as a point p_n a point with coordinates (x_n, y_n).
In order to connect 4 points you could follow these steps:
Get the point p_1 with the smallest x.
Calculate the slope of the 3 lines that go from p_1 to each of the remaining points.
Connect p_1 with the point p_2 that composes the line with the greatest slope.
Connect p_1 with the point p_3 that composes the line with the smallest slope.
Connect the remaining point p_4 with p_2 and p_3.
Let me know if something is unclear.
Edit: This answer has already proven to be wrong.
Calculate the center of the four points.
Enumerate the points in the counter-clock wise direction (or clockwise).
Link them together.
EDIT: As pointed out in the comments, it only works in specific situations and thus is a bad answer.
The quadrilateral can be drawn by finding which pair of points amongst the 4 points is separated by the largest distance. Once this pair is found, the quadrileteral is drawn by linking the two remaining points with each point of the pair.
You can use a simplification of any of the well-known algorithms for convex hull. Jarvis would be easy. If the convex hull is a triangle, the quadrilateral is convex. Just insert the missing point anywhere in the edge list. If the convex hull is a line (2 endpoints), just sort all the points on either x- or y-coordinate to get a degenerate quadrilateral. (If closer to horizontal (abs delta x > abs delta y), use x for sorting, else use y.)
i think, you just need to join two points each time, we will get a line segment and check if the remaining other two points are on the same side of that line segments, if not thats not a line segment of required quadrilateral, if yes then proceed with different set of points (i.e on point can be one of the coordinate of this segment and other one would be form remaining two points) and check the same thing until u get 4 line segments
Look around for convex hull algorithms. One of them consists of two steps: building an ordinary polygon on a given set of vertices (which may be concave), then removing 'concave vertices' so that the remaining polygon becomes convex.
The first step is a solution for your problem.
Of course it is kind of overkill. For 4 vertices just set them in a sequence (in any order) then verify if line segments joining points 1-2 and 3-4 intersect; if so, swap points 2 and 3; or possibly edges 2-3 and 1-4 intersect – then swap points 3 and 4. Done.
To verify if segment AB and CD intersect test if points A and B are on opposite sides of the line CD and points C and D are on opposite sides of line AB.
To identify the side of a line PQ where a point K lies, calculate the Z-part of the PQ×PK vector product: (xq-xp)(yk-yp)-(yq-yp)(xk-xp). The expression is positive on one side of the line and negative on the other one (and zero on the line).
One can solve this problem very easily with the help of a function that says whether two line segments intersect.
Given points A, B, C, D, there's only three different orders to join up the vertices: ABCD, ABDC and ACBD (vertex A either connects to vertex B or it doesn't. If it does, there's two ways to order C and D. If it doesn't then A connects to both C and D and each of those must connect to B).
An ordering of the four points produces a quadrilateral if none of the edges intersect (except at the corners). That gives this procedure for finding a working order:
If AB intersects with CD then return ACBD.
If AD intersects with BC then return ABDC.
Otherwise return ABCD.
The proof that this works is easy:
both ABCD and ABDC include the edges AB and CD, so if those pair of edges intersect, the good order must be ACBD.
both ABCD and ACBD include the edges AD and BC, so if those pair of edges intersect, the good order must be ABDC.
if neither AB/CD and AD/BC intersect, then the order ABCD produces a quadrilateral.
The code for determining if two line segments intersect can be found online if you can't figure it out yourself.
Here is a trivial, non-mathematical way of doing it. I've tried it on several examples and could not prove it wrong. Please let me know if you do or know how to make it better:
Choose the two points that are on the right of the others, say point 1 and 2.
Link points 1-2 together, as well as points 3-4.
Each point must be linked to only two other points so there are only 2 possibilities: linking points 1-3 and 2-4, or points 1-4 and 2-3.
Check if each line intersects another.
That should do it.
Note: when choosing the first two points, here are some special cases:
Point 1 is on the right of all others, but points 2 and 3 are on the same x coordinate. Pick the point that's closest to point 1.
If three points 1,2 and 3 have the same x-coordinate, link the two that are closest to each other. If the points are evenly separated, pick one of the two possibilities.
A few stages, assuming the end result needs to be 4 couples of points (and/or the equation of the line between them):
Take any three points, and make a triangle.
If the forth point is inside the traingle, swap it with ny of the three.
Having only the last point left, calculate which two point you want to attach it to. This is done by playing with the line eqations and finding where the intersecting points are. Clarification below.
Return two sides of the triangle + the 2 lines between the forth point and the ones you chose from stage 2.
Step 2 clarification:
Let us say that point A is not in the triangle (which is BCD). Every line devides the plain into two sides. we want to find the point (B, C or D) s.t. the line between it and A runs between the other two (they are on opposite sides of the line). This is the point we DO NOT want to attach to A.
Example:
Given A(0,0), B(10,0), C(10,10) and D(0,10). We have the triangle BCD. The line BC leaves A & D on the same side of the plain. So does DC. The line AC leaves B & D on opposite sides of the plain - so we want to connect A to B & C.

Closest pair of points

In
http://en.wikipedia.org/wiki/Closest_pair_of_points_problem
we can see that it mentions that is at most 6 points that is closest to the point on the other half, which can be represented as the graph below:
My question is for point P1 and Point P2, the distance to the red point will exceed sqrt(2)*d, why it is part of the solution? Why it is not at most 4 points that is closest to P rather than at most 6 points? Thanks.
P1 and P2 are not part of the solution, but they have to be examined on the way to the solution, because the algorithm examines all points in the box, and P1 and P2 are in the box.
Note that no such point as your Q can exist, because by hypothesis the minimum distance between points in the right-hand half of the diagram is d.
Edited to add: you seem to think that the Wikipedia article is making a claim like this:
There may be up to 6 points on the right side of the line that are within a distance d of P.
This claim would be false. But the article does not make such a claim. Instead, it makes two separate claims, both of which are true:
All the points on the right side of the line that are within a distance d of P are inside the box.
There may be up to 6 points in the box.
We are only counting the maximum number of points that can lie in the right d x 2d rectangle. Since any two points are constrained to have a minimum distance of d, we can place at most 6 points in the rectangle while satisfying this constraint, as shown in the figure.
Note that the points on the right side that are within d distance from P should all lie within a circular segment of a circle centered at P and whose radius is d. There can be at most 4 points in this segment. However, finding the number of points within a segment is more complicated than finding the number of points within a rectangle. So we use the rectangle instead and incur an extra cost of having to search for at most 2 additional points.
The bound is only important for complexity estimation. Code-wise, you may simply scan up and down within the distance dRmin. The bound here suggest that you'll at most see 6 points in each such scan, making this O(1).

skyline algorithm for triangles

I am trying to write an algorithm to find the upper envelop (skyline) of triangles.
By following you can see the skyline of rectangles:
I have an algorithm for merging two skylines(L and R) of rectangles as follows:
represent each rectangle by (x1, x2, h) where h is height and x2-x1 is width
represent each skyline by a list of couple (x, h)
for i= min(L[ 1].x, R[ 1].x) to max(L[size of L].x, R[size of R].x) choose max(L[i].h, R[i].h)
Now, my question is how can I represent triangle and how I can merge skylines of two triangles
any idea will be appreciated
In the following I assume that the triangles are with their baseline on the bottom. I'm also assuming that all triangles are such that the upper corner is above the baseline (i.e. if you go straight down from the upper corner, you get inside the triangle, not outside). However I'm not assuming that only symmetric triangles are allowed.
Actually a merging of triangles will give a skyline where simply points are connected with lines. so the representation of a triangle skyline could just be a ordered list of points (x_i, y_i) with the restriction that y_0 = 0 and y_N = 0 where N is the index of the last point. A single triangle would then be represented by the three-element list (x_0, 0), (x_1, h), (x2,0) where x_0 and x_2 are the left and right endpoint (the two points where the triangle reaches 0), x_1 gives the horizontal position of the upper corner, and h gives the height.
The merging of two skylines can then for example be done as follows:
Step 1: For each line segment (x_i, y_i)--(x_{i+1}, y_{i+1}) from skyline 1 and each line segment (x_j,y_j)--(x_{j+1},y_{j+1}) calculate whether they intersect, and if so, where (this means solving a simple system of two linear equations). Collect the intersection points into a new list, intersections. So now you have three lists of points: skyline1, skyline2 and intersections. Since all intersections will be part of the skyline, use that as the basis for the new Skyline. (a special case is when both skylines agree over an interval, but in such intervals the combined skyline is the same as each single one anyway, so just use the start and end points of those intervals as intersection points)
Now for each pair of intersection points (and also left of the first intersection and right of the last intersection), there will be always exactly one skyline which is above the other (unless they agree, but then it doesn't matter which you choose). Add the points in the interval from that skyline to your combined skyline. You find out the larger one by just choosing an arbitrary point of one skyline (except if the intersection point is also a skyline point, that one should not be chosen) and detect the height of the other skyline at its x value (if the other skyline also has a point at the same x value, it's a simple comparison of the y value, otherwise you have to interpolate the y values of the preceding and following points).
After doing that, you should have the correct combined skyline.

Resources