Are polyshape vertices preserved reliably without floating point errors?
Show older comments
Suppose I have two polyshapes p1 and p2 and I want to do operations on them (intersection, unions) that in theory should preserve some of the vertices of p1 and p2. My question is, can I always count on this to occur without floating point error from the underlying software algorithm?
In the following example, it seems to be true. The union of p1 and p2 is a quadrilateral whos vertices ought to be the union of the vertices of p1 and p2 separately,
load Data
figure; plot([p1,p2]);axis equal
and this is indeed shown to be the case without the need to apply a floating point error tolerance,
Union=union(p1,p2);
all(ismember(p1.Vertices,Union.Vertices,'rows'))
all(ismember(p2.Vertices,Union.Vertices,'rows'))
The same thing appears to be true with intersections:
figure; plot([p2,p3]);axis equal
Intersection=intersect(p2,p3);
nnz(ismember(p3.Vertices,Intersection.Vertices,'rows'))
But there's no reason this had to be the case, right? There must be something about the underlying union() and intersect() algorithms that ensure this.
Accepted Answer
More Answers (1)
John D'Errico
on 5 May 2024
Edited: John D'Errico
on 5 May 2024
1 vote
Um, probably, yes. But potentially no.
Yes, because on most operations, you will just be indexing into the original set of vertices. And that MUST preserve the exact numbers.
Potentially no because consider an intersection, where one of the vertices from one of the shapes just happens to fall along an edge of the other. Now there will be floating point operations applied, where the least significant bits could be wrong. Can I say this would never happen? No. But I'd not trust it, at least not without some significant testing. Anyway, that is where I would be looking. Anything else seems unlikely to cause a problem.
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

