Feel free to cannibalize parts of my ImageTrimmer, which smooths jaggedness...
You pass the edges' collected vertex points, and set an epsilon factor... thus.
### epsilon is a float perhaps < 0.2
### perhaps it's set by the user ? experiment for best default...
### the vertices are collected from the current outline[s]
points = vertices.collect{|v| v.position }
simplified_curve = douglas_peucker(points, epsilon) ### might return []
simplified_curve << simplified_curve[0] if simplified_curve[0] ### so it 'loops'
edges = some_entities.add_curve(simplified_curve) if simplified_curve[0]
### sort out edges' faces, hide edges etc as desired
the 'douglas_peucker()' method is this...
### http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
### http://en.wiki.mcneel.com/default.aspx/McNeel/
PolylineSimplification.html
###
def douglas_peucker(points, epsilon)
###
return points if points.length < 3
### Find the point with the maximum distance
dmax = 0
index = 0
line = [points.first, points.last]
1.upto(points.length - 2) { |i|
d = points[i].distance_to_line(line)
if d > dmax
index = i
dmax = d
end
}
### If max distance is greater than epsilon, recursively simplify
result = []
if dmax >= epsilon
### Recursive call
recResults1 = ImageTrimmer.douglas_peucker(points[0..index], epsilon)
recResults2 = ImageTrimmer.douglas_peucker(points[index...points.length], epsilon)
### Build the result list
result = recResults1[0...-1] + recResults2
else
result = [points.first, points.last]
end#if
###
return result
###
end #def