Check that a polycurve is rectangular

Good morning,

I’m looking for a node that checks if the polycurve is rectangular.
To avoid reinventing the wheel:
Maybe there is already a custom node that does this?

If this is not the case
here is the process i would use
1-check that there are only 4curves
2-classify the curves by length
3-check if the 2 sides are parallel
4-check a right angle

you may have a more efficient one.

can we stay with a strict equal or more with an approximate equal?

I thank you in advance
Cordially
christian.stan

Get all 4 sides and their vectors, check if the point up/right and if not reverse them. A rectangle should only have two possible vectors from those outcomes.

If the polycurve is closes you could also reverse sides 1 and 2 then check against 3 and 4 for no more than 2 vectors overall.

Thank you, do you mean like this?

Cordially
christian.stan

It could be more simple, I think. The distance between point 1 and 3 should always be the same as the distance between point 2 and 4.

Vectors being parallel doesn’t mean the shape is rectangular, but that it is is a rhombus. You need to check the angles to confirm it’s a rectangle.

crvs = polyCrv.Explode();
vectSet = crv.Direction;
shiftSet = List.ShiftIndicies(vectSet,1);
angles = Vector.AngleWithVector(vectSet, shiftSet);
quadCheck = Math.Sum(angles) == 360;
unique = List.UniqueItems(angles);
angleCheck = List.Count(unique) == 1;
isRect = angleCheck && quadCheck;

Note that these concepts can scale to check for any regular polygon.

Thanks @ all for your contributions.

I blame myself for not thinking about the diagonals (efficiency :+1:).

the generalization to regular polygons (nice resolution :wink:, great shape as usual)

Cordially
christian.stan

You might round the distances before comparing them, they could differ with a very short distance due to double calculation limitations. Another way is to subtract them to see if they are almost equal.

Math.Abs(distance1 - distance2) < 0.0001

It is clear that the 2nd writing would never have occurred to me, thank you for sharing the knowledge, I store this in a still active memory corner :wink:
Cordially
christian.stan

You made me want to try to generalize with regular polygon


it’s not competition :wink:, I’m not able like you to do it from a phone (List.ShiftIndicies)

Cordially
christian.stan