I am trying to check two normal vectors for equality. My normal vector is represented as a three-element list, one element for each spacial coordinate (X, Y, and Z). All coordinates are rounded to 4 decimal places. I want to check if two surfaces have the same normal, so I have something like the following:
if (surface1.normal in [surface2.normal, self.NegatedNormal(surface2.normal)]):
# do stuff here
The problem is my normals look like this:
surface1.normal: [0.9947, 0.0155, 0.1015]
surface2.normal: [0.9947, 0.0155, 0.1014]
Note that the z-coordinate is off by 0.0001. So is there a way I can override the equals operator to accept answers within 0.0001 from each other that is compatible with comparisons in other data structures (like a list in my case)? I have a feeling I'd have to write my own __eq__ method but I'm not quite sure how I would do this.
Also, if this is not the best course of action, is their a better way to compare two lists of floats within a given tolerance?
You can write a custom function to do this. For example:
def comp_floats(x, y, delta):
return abs(x - y) < delta
You can obviously do whatever type of error correction you want in the function itself, but this is just one example.