Here's an unproven conjecture from geometry: Every simple closed curve in a plane contains all four vertices of some square.
It's kind of remarkable. As far as we can tell right now, you can put at least one square on every single closed curve. Inspired by this, I made a simple discrete Cody-fied version of the problem. (And by the way, I'm not claiming it's mathematically equivalent...)
Given a list of points in [x y] format, return four indices into that list that form a square.
xy = [ 29 13
41 7
53 16
58 48
80 41
78 85
46 80
7 46 ]
Then the answer would be the indices given in ix. These indices point to the rows that contain the four vertices of the square.
ix = [ 2 5 7 8 ]
You can return the indices in any order that honors the cycle around the square, and the answer is not necessarily unique. The test suite just verifies that what you return is indeed a square.
Solution Stats
Problem Comments
5 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers6
Suggested Problems
-
2537 Solvers
-
1620 Solvers
-
Project Euler: Problem 10, Sum of Primes
2088 Solvers
-
Return the Nth Output from an Input Command
68 Solvers
-
979 Solvers
More from this Author54
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Your test suite just checks whether a quadrilateral has equal sides, but that's not enough to form a square: rhombuses also have equal sides. Therefore, the test suite should also check a solution's angles.
PS: A test case filled with rhombuses and a single square on the curve would be instructional too.
Test suite has been updated to check for a solution's angles as well and a mixed test case with rhombus and square mixed has been added.
Solutions have been rescored as well.
Where is the square in the last test? The test passes with [1 3 5 7], but that's a rhombus. The angle test works because the dot products are 3, 3, -3, -3, and their sum is still zero. Perhaps checking that all dot products are zero (or less than a tolerance) would work.
Hello Chris, there is actually a typo from my side in the last test case.
Point #6 is supposed to be (1,-1) instead of (-1, 1) (which occurs twice).
I am unable to correct the test suite at this moment, I will update it inline with your feedback, as soon as I can.
Hello Chris, I have corrected the test case #7 and modified the test suite as per your suggestion.
Also, I have rescored the solutions.
It was interesting to see that quite a many solutions failed the last test case, incorrectly giving the rhombus as the solution
(i.e [1 3 5 7]).