Problem 820. Eliminate unnecessary polygon vertices
Suppose you have an npoint polygon represented as an nby2 matrix of polygon vertices, P. Assume that the polygon is closed; that is, assume that P(end,:) is the same as P(1,:).
Remove as many vertices as possible from P to make a second polygon, P2, that has exactly the same shape as P. P2 must also be closed. Your vertices in P2 should be in the same direction as P. That is, if the vertices in P are in clockwise order, then the vertices in P2 should also be in clockwise order.
If the first vertex in P is retained in the solution, it should be the first vertex in P2. If the first vertex in P needs to remove, then the first vertex in P2 should be the next retained vertex in P.
You can test your solution graphically as follows:
plot(P(:,1), P(:,2), 'r', 'LineWidth', 5); hold on plot(P2(:,1), P2(:,2), 'b'); hold off
The two plotted shapes should overlap exactly.
EXAMPLE
P = [1 1 2 1 3 1 4 1 4 2 4 3 3 3 2 3 1 3 1 2 1 1];
P2 = [1 1 4 1 4 3 1 3 1 1];
This problem is related to my 09Jul2012 blog post on MATLAB Central.
Solution Stats
Problem Comments

9 Comments
Thanks for Cris for pointing out a hole in my original test suite. I added a test case that has a removable vertex as the first row of P and rescored all entries.
I think test case 7 is wrong  the starting vertex [1 2] can be removed can't it?
Thanks, Richard. I have updated test case 7, and the solutions are being rescored.
And now I think the vertices for test case 7 are coming out in the wrong order (cyclic shifted)] :)
I agree with Richard. I believe my solution is correct now, but test case 7 is failing it.
This is the second time I've gotten up in the middle of the night to try to fix this. ;) In my previous attempt to fix test case 7, I was fooled by the diagram I drew.
I've modified test case 7 again. When I run your latest solution, Cris, it passes. We'll see what happens when the solutions all get rescored again.
Sorry Steve! Hope you got some sleep in the end :)
Final discussion of the problem is up on the MATLAB blogs now:
http://blogs.mathworks.com/steve/2012/08/28/wrappinguptheanalysisofcodysolutions/
Mixing vertices, lines and polygons is not fair because vertices and lines are not polygons and their simplification algorithm is not the same (multiple vertices can be simplified by using the function unique for instance). The author should at least change the problem description imho to "Eliminate unnecessary vertices at any shape or form". Anyway, It's a good problem (having nonmanifold polygons is hard enough even without nonpolygons).
Solution Comments
Show commentsProblem Recent Solvers24
Suggested Problems

71 Solvers

83 Solvers

Make an awesome ramp for a tiny motorcycle stuntman
610 Solvers

667 Solvers

String substitution, sub problem to cryptoMath
228 Solvers
More from this Author5
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!