Select all colinear vertices in a fixture

Let us know if you have a nice R.U.B.E script to share!
Post Reply
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Select all colinear vertices in a fixture

Post by iforce2d »

This script will select all vertices of the currently selected fixtures, where the vertex is almost exactly between its neighbors, within a given tolerance. These are good candidates for removal in most applications, and the presence of colinear edges can be a problem case for some algorithms. The tolerance is for comparing the dot product of the two segments on either side of the vertex.

Code: Select all

float tolerance = 0.00001;

fixture[] fs = sf();
for (uint i = 0; i < fs.length; i++) {
	fixture f = fs[i];
	for (int k = 1; k < f.getNumVertices()-1; k++) {
		vertex v0 = f.getVertex(k - 1);
		vertex v1 = f.getVertex(k);
		vertex v2 = f.getVertex(k + 1);
		vec2 s0 = v1.pos - v0.pos;
		vec2 s1 = v2.pos - v1.pos;
		s0.normalize();
		s1.normalize();
		float dot = s0.x * s1.x + s0.y * s1.y;
		if ( abs(dot) > (1-tolerance) ) 
			v1.select();
	}
}
Post Reply