The Challenge
Given an arbitrary amount of rectangles, output the total count of intersections of those when drawn in a 2D plane.
An intersection here is defined as a point P
which is crossed by two lines which are orthogonal to each other and are both not ending in P
.
Example
Each rectangle here is denoted by a 2-tuple with the coordinates of the upper left corner first and the coordinates of the bottom right corner second.
[(-8,6),(-4,-2)] [(-4,9),(4,3)] [(2,10),(14,4)] [(1,7),(10,-6)] [(7,4),(10,2)] [(5,2),(9,-4)] [(-6,-4),(-2,-6)]
Those rectangles create 6 intersections, which has to be your output.
- As you can see in the image above, touching rectangles will not create intersections here and are not counted.
- You can encode the rectagles in any format you want. Make it clear which format you use.
- If multiple rectangles intersect at the same point, it only counts as one intersection.
- The coordinates will always be integers.
- There won't be any duplicate rectangles in the input.
- You will always get at least one rectangle as input.
- You may not use any builtins which solve this problem directly. Additionally you may not use builtins that solve equations. All other builtins are allowed.
- The output has to be a single integer indicating the intersection count.
Rules
- Function or full program allowed.
- Default rules for input/output.
- Standard loopholes apply.
- This is code-golf, so lowest byte-count wins. Tiebreaker is earlier submission.
Test cases
Same format as in the example above. The rectangles are wrapped in a list.
[[(-8,6),(-4,-2)],[(-4,9),(4,3)],[(2,10),(14,4)],[(1,7),(10,-6)],[(7,4),(10,2)],[(5,2),(9,-4)],[(-6,-4),(-2,-6)]] -> 6 [[(-2,2),(6,-4)]] -> 0 [[(-12,10),(-8,6)],[(-14,6),(-10,2)],[(-10,6),(-6,2)]] -> 0 [[(-4,10),(6,2)],[(-2,8),(4,3)],[(1,6),(8,4)],[(2,11),(5,5)]] -> 10 [[(8,2),(12,-2)],[(10,0),(14,-4)]] -> 2 [[(0,2),(2,0)],[(0,1),(3,0)]] -> 1 [[(-10,-2),(-6,-6)],[(-6,-2),(-2,-6)],[(-8,-4),(-4,-8)]] -> 3
Happy Coding!
[[(0,0),(1,2)],[(0,0),(2,1)]]
would have 1 intersection? \$\endgroup\$