15
Today's challenge:
Given an ordered list of at least 3 unique integer 2D points forming a polygon, determine if the resulting polygon is Rectilinear.
A polygon is rectilinear if every interior angle is a right angle. The edges do not necessarily have to be purely vertical or horizontal (parallel to the x or y axis), as long as the angles are all right (This is slightly different than Wikipedia's definition, but it's the definition we'll be sticking with). For example, the following shape is a perfect square rotated 45°
(0, -1)
(1, 0)
(0, 1)
(-1, 0)
None of the lines are parallel to the x or y axis, but all of the angles are right so it is considered truthy for today's challenge. Note that the order of the points is important. The following reordering of these points gives a self-intersecting polygon, which is basically an hourglass shape rotated 45°. This is falsy:
(0, -1)
(0, 1)
(1, 0)
(-1, 0)
You must take the input as an ordered set of points. Angles between the points or a "shape object" if your language has one, are not valid inputs.
You must output one consistent value for truthy and a different consistent value for falsy.
Note that truthy inputs will always have an even number of points.
It is acceptable if your submission fails for very large inputs because of floating-point inaccuracies.
Note: If you want to visualize a polygon, I've found this tool and Desmos Graphing Calculator both very useful.
Truthy examples
(0, 0)
(0, 20)
(20, 20)
(20, 0)
(1, 1)
(0, 3)
(6, 6)
(7, 4)
# This example is self-intersecting
(2, 3)
(3, 1)
(-3, -2)
(-2, -4)
(2, -2)
(0, 2)
(4, 0)
(4, 2)
(0, 2)
(0, 4)
(2, 4)
(2, 5)
(4, 5)
(4, 3)
(6, 3)
(6, 0)
Falsy examples
(0, 0)
(0, 20)
(20, 20)
(20, 10)
(100, 100)
(100, 50)
(50, 100)
(2, 2)
(3, 3)
(6, 2)
(7, 3)
(100, 100)
(100, -100)
(-100, -100)
(-100, 99)
Is
(0,0) (0,1) (0,2) (2,2) (2,1) (2,0)
expected falsy? – tsh – 2019-11-08T02:26:00.707What should output for
(1,0) (2,0) (2,1) (3,1) (3,0) (0,0) (0,1) (1,1)
? – tsh – 2019-11-08T02:30:56.590Isn't the answer always false if there are only three points? – Esolanging Fruit – 2019-11-08T02:37:06.140
May we take points as complex numbers? – xnor – 2019-11-08T08:52:38.340
Can we swap truthy and falsey? – flawr – 2019-11-08T15:56:12.483
@flawr Yes, any two consistent values are fine. – James – 2019-11-08T16:00:10.427
@xnor Yes, that's acceptable. – James – 2019-11-08T16:03:11.287
@EsolangingFruit Correct, because no triangles are comprised entirely of right angles. – James – 2019-11-08T16:03:29.337
@tsh Let's say to keep things simple, no inputs will have A) Multiple consecutive points along the same line, such as
(0,0) (0,1) (0,2)
or B) Shapes with 0-width sections such as(0, 2), (0, 0), (2, 0), (2, 2), (3, 2), (-1, 2)
– James – 2019-11-08T16:06:49.000