VEX : How to Measure Polyline Length

There are a couple of ways to get the length of a poly line primitive segment, the Measure SOP does this out of the box. And in most cases just use that it works perfectly and its fast. That being said here is how to do it in VEX if you need it in the middle of a block of code or want to calculate it in another network like DOPs.

Here is how you do it for a primitive with 2 points. Create a wrangle and set it to run over primitives. Then the algorithm is simply get prim point A and B get their positions and then get the distance between them. Which gives you the following code:

int pt1;
int pt2;
vector pos1;
vector pos2;

pt1 = primpoint(0, @primnum, 0);
pt2 = primpoint(0, @primnum, 1);
pos1 = point(0, "P", pt1);
pos2 = point(0, "P", pt2);

f@edgelength = length(pos1-pos2);

If the primitive has more then two points use a for loop instead adding up the distance between each set of points like so:

int pts[] = primpoints(0,@primnum);

float edgelength = 0;

foreach(int i; pts)
{
if( i == 0)
continue;

vector pos1 = point(0, "P", i);
vector pos2 = point(0, "P", i-1); 

edgelength += length(pos1 - pos2);
}

f@edgelength = edgelength;

note: If the primitive already exists and is not created in the current vex snippet you can also just use the primitiveintrinsic() function to access the length just get the intrinsic: measuredperimeter. Then you have it as a one liner..

Leave a Reply

Your email address will not be published. Required fields are marked *