Problem 820. Eliminate unnecessary polygon vertices
Suppose you have an n-point polygon represented as an n-by-2 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 09-Jul-2012 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/wrapping-up-the-analysis-of-cody-solutions/
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 non-manifold polygons is hard enough even without non-polygons).
Solution Comments
Show commentsProblem Recent Solvers24
Suggested Problems
-
Least common multiple of many numbers
235 Solvers
-
Generate N equally spaced intervals between -L and L
868 Solvers
-
512 Solvers
-
Circular Primes (based on Project Euler, problem 35)
494 Solvers
-
Given a matrix, swap the 2nd & 3rd columns
1075 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!