Counting number of ways to make a 3 Partition of n dots in a triangle such that each partition fit in a "Y" shape. (More description below) - algorithm

I want to find an algorithm to tackle this problem but I have no idea what this problem is even called so I can't google it.
Problem:
There is a triangle, and inside the triangle there are n dots. For example, as below.
Now assume 3 sides of triangle are rubber bands and dots are nails on the wall. We tuck each rubber bands into some dot(s). But each rubber bands must be convex towards the center of triangle.
Also, there should be no intersection of any 2 rubber bands apart from the 3 vertices of original triangle.
Also, there should not be any remaining dot inside the polygon formed by 3 rubber bands.
How many valid arrangement of rubberbands, such that all given conditions are satisfied, are there?
Here, in this picture, the leftmost picture is the original state, 2nd picture is a valid composition, 3rd is invalid since blue and red rubber bands intersect, 4th is invalid since there is a nail remaining inside red-blue-green polygon
My approach: After some thoughts: I guessed that if we pick an arbitrary location inside the triangle which is not one of the given dots(nails), and then we make a Y shape starting from that location towards 3 vertices of outer triangle, that is guaranteed to make a unique valid arrangement of rubber bands, if we imagine that each pair of 2 neighboring edges of Y shape is a rubberband and then we release those bands. They will sort themselves out to form a valid arrangement with elasticity.
So from each 3 vertices of triangle, I draw a line towards all n dot(nails). And then for each polygon partitions that are created by those lines and/or 3 sides of triangle(approx n^3 polygon partitions in total), I can make exactly 1 valid composition by picking any point in the polygon and making Y shape.
For example, if I pull rubber bands to form a y-shape as in the left picture, I can then make a valid composition by releasing those rubber bands, as in the right picture.
However, the problem is that 2 or more polygons may yield same valid composition, so we will count more than the correct answer.
For example, if there are only 2 dots(nails), we could make a bunch of polygon partitions by connecting lines(colored brown) starting from each 3 vertices of triangle to 2 dots(nails). so there are 17 partitions. each 17 partitions would yield a valid composition but the answer would be 6. (3 cases where 2 dots go under same rubber band, 3 cases where 1 dot goes under a band and the other dot goes under the other band.)
So I drew several small examples and tried to figure out a pattern to make an algorithm to de-duplicate it and I kind of see a pattern but can't really formulate it into a pseudo code. Any help is appreciated.

Related

Constructing a polygon (hexagon) given length of one side, 2 points and the perimeter?

I have a general question on an algorithm that I would like to use to construct an irregular polygon.
I have the following scenario:
I have a start and end position for a ship, thus I know the distance between the two position (say 40km). Using this info, I would like to calculate the remaining vertices of a polygon that includes this as one side, while the other sides should be equal in length and sum up to say 200km (as a matter of fact, any user supplied length). I therefore know the perimeter of the resulting polygon and the 2 vertices that make up one of the sides.
In short, how do I get the other 4 vertices so that each side besides the initially calculated is the same size (and the perimeter of the hexagon adds up to 200 + initial side)?
Thanks in advance!

Algorithm to find a frontline between enemy and friendly bases?

Please look at this picture first:
As you can see, blue and red both have a line showing the frontline bases.
Let's assume two players start playing against eachother.
One placing a base at the far left, the other at the far right.
The players continue by expanding and gaining territory.
After a while, the two sides will meet and battle will start.
Question is, in a randomly ordered list of base positions how would one find the two lines that are drawn in the picture?
You could get the distance between enemy bases, where the distance is shorter is the frontline.
Example:
0 1 2 3 4 5 6
0 aa A B bbb
1 aA B bb
2 A B b
3 aA B b
4 aaaA Bb
5 aaaA B
6 aaaA B
If you substract the X positions of the enemy bases in the same row, the ones with the less distance between them are the front lines, B.x - A.x will give you a smaller number that b.x - a.x. Then you can check which ones were the ones that gave you the smaller distance for each row.
Unless I'm getting your question wrong.
area
many games do not draw territory border by polylines
instead they draw a disc with constant radius (or dependent on base strength) at every base position
if the radius is set big enough then these discs/circles will overlap
and create seamless area of single color representing controlled territory
it is easy but inaccurate
perimeter polyline
first cluster all bases and create lists of bases close together
this can be done by grouping bases with distance <= treshold
process each group
find the outer most bases of group
something like perimeter points
this may help with it
also it contains inverse problem of yours which can solve the whole thing more here
now find the perimeter closed loop of area
the algorithm for that is also in the link above
when done then use this list of points as BEZIER cubics poly-curve control points
if borderline too close to the bases then enlarge the points
first compute avg point ap of group
then any point p is computed p=((p-ap)*scale)+ap
it is not exact but for your purposes it is enough
if you want something better then
p=p-ap
l=|p|
p=ap+(p*(l+dl)/l)
where dl is the step at which your border is enlarged
and if you want the accurate thing then you have to compute polygon enlargement
which is not easy task
handle edge cases
for singular base groups draw circle around
this approach has problem in close proximity of bases
one solution is not to count bases too close to any enemy base
per pixel borders
you can process the map image by pixels
something like this: (beware this is not tested)
per each pixel
compute the min distance to any player bases
remember two closest distances of different players
distance0 is closest
distance1 is second closest (but base owns another player)
select territory ownership
the player which has closest base own this
if the distance0 > treshold then this area is uncontrolled
in that case stop processing this pixel
if ((distance0 >= d-w)&&(distance0 <= d+w)&&(distance1>d+w+s)) then set pixel color to border polyline color
d is border line distance from base
w is half-size of borderline thickness
s is min distance between close front lines
edge case
previous step ignore border points that are closer to bases (close enemy bases)
to add them just add
if ((distance0<d)&&(|distance0-distance1|<=s+w)&&(|distance0-distance1|>=s-w)) then set pixel color to player ownership
also this will fail if any two opposing bases are closer then s-w
[Notes]
I think the best way is the per pixel
it is most close to the solution you want
the render can be a bit slower but you need to change it only if any base is conquered

Algorithm for the decomposition of polygons

Does anyone know a relatively fast algorithm for decomposing a set of polygons into their distinct overlapping and non-overlapping regions, i.e. Given a set of n polygons, find all the distinct regions among them?
For instance, the input would be 4 polygons representing circles as shown below
and the output will be all polygons representing the distinct regions shown in the different colours.
I can write my own implementation using polygon operations but the algorithm will probably be slow and time consuming. I'm wondering if there's any optimised algorithm out there for this sort of problem.
Your problem in called the map overlay problem. It can be solved in O(n*log(n)+k*log(k)) time, where n is the number of segments and k is the number of segment intersections.
First you need to represent your polygons as a doubly connected edge list, different faces corresponding to the interiors of different polygons.
Then use the Bentley–Ottmann algorithm to find all segment intersections and rebuild the edge list. See: Computing the Overlay of Two Subdivisions or Subdivision representation and map overlay.
Finally, walk around each cycle in the edge list and collect faces of that cycle's half-edges. Every set of the faces will represent a distinct overlapping region.
See also: Shapefile Overlay Using a Doubly-Connected Edge List.
I don't think it is SO difficult.
I have answered the similar question on the friendly site and it was checked by a smaller community:
https://cs.stackexchange.com/questions/20039/detect-closed-shapes-formed-by-points/20247#20247
Let's look for a more common question - let's take curves instead of polygons. And let's allow them to go out of the picture border, but we'll count only for simple polygons that wholly belong to the picture.
find all intersections by checking all pairs of segments, belonging to different curves. Of course, filter them before real check for intersection.
Number all curves 1..n. Set some order of segments in them.
For every point create a sequence of intersections SOI, so: if it starts from the border end, SOI[1] is null. If not, SOI[1]= (number of the first curve it is intersecting with, the sign of the left movement on the intersecting curve). Go on, writing down into SOI every intersection - number of curve if there is some, or 0 if it is the intersection with the border.
Obviously, you are looking only for simple bordered areas, that have no curves inside.
Pieces of curves between two adjacent non-null intersection points we'll call segments.
Having SOI for each curve:
for segment of the curve 1, starting from the first point of the segment, make 2 attempts to draw a polygon of segments. It is 2 because you can go to 2 sides along the first intersecting curve.
For the right attempt, make only left turns, for the left attempt, make only the right turns.
If you arrive at point with no segment in the correct direction, the attempt fails. If you return to the curve 1, it success. You have a closed area.
Remember all successful attempts
Repeat this for all segments of curve 1
Repeat this for all other curves, checking all found areas against the already found ones. Two same adjacent segments is enough to consider areas equal.
How to find the orientation of the intersection.
When segment p(p1,p2) crosses segment q(q1,q2), we can count the vector multiplication of vectors pXq. We are interested in only sign of its Z coordinate - that is out of our plane. If it is +, q crosses p from left to right. If it is -, the q crosses p from right to left.
The Z coordinate of the vector multiplication is counted here as a determinant of matrix:
0 0 1
p2x-p1x p2y-p1y 0
q2x-q1x q2y-q1y 0
(of course, it could be written more simply, but it is a good memorization trick)
Of course, if you'll change all rights for lefts, nothing really changes in the algorithm as a whole.

Calculating the percentage of a plane that is underneath a slope

I am a bit stuck with this problem. I have an NxN square and a line that passes through this square. How can I calculate the percentage of the NxN square that falls above and beneath the line given an arbitrary slope? I imagine I have to use the Pythagorean theorem on this one, I am just not sure how to apply it. Any help is appreciated. (Not homework)
Basic idea: find the 0, 1 or 2 points where the line intersects the square. If there are 0 or 1 intersections, the square is entirely on one side of the line. If there are two, the square is bisected into two polygons (a triangle and a pentagon if the two intersections are on adjacent sides, or two quadrilaterals if the intersections are on opposite sides). Find the respective area of the two parts, and that will give you the percentages.

maximum number of dominoes can be placed inside a figure

Suppose some figure on the squared paper. Sides of the figure go straight on the lines of squared paper. Figure may have any (not even convex) shape. How to find the maximum number of dominoes (1x2 rectangular) that can be placed in that figure. It is not allowed to put domino over another one. It is allowed to put domino only in such way, when its sides fall exactly on the lines of squared paper.
Looks like the maximum cardinality matching problem in a bipartite graph. The squares are the vertices and the dominoes are the edges that belong to the matching.
To see that the graph is bipartite, imagine the squares are checkerboard-painted. Black ones only neighbour white ones and vice versa.
You can classify squares by the number of neighbor free squares as type 0, 1, 2, 3 or 4.
I believe that should work:
find a type 1 square, place there a domino in the only possible way and repeat
else, find a free corner formed by two contiguous type 2 and type 3 squares, place there a domino and go to 1
else, place a domino in any type 2 square and go to 1
you are done

Resources