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)
(0,0) (0,1) (0,2) (2,2) (2,1) (2,0)
expected falsy? \$\endgroup\$ – tsh Nov 8 at 2:26(1,0) (2,0) (2,1) (3,1) (3,0) (0,0) (0,1) (1,1)
? \$\endgroup\$ – tsh Nov 8 at 2:30