Hey All, I'm just getting my feet wet again with Unity and C#. I've run into a problem I hope I can get some help with. I have a list of custom objects called "LineSegments". The LineSegments are each a couple of Vector2's. I'm trying to find the first match in my list. It works sometimes but not all the time. I can't find any reason for this. I'm thinking I've written it incorrectly. Could someone tell me if this lamda statement looks correct? Code (CSharp): List<LineSegment> temp_l = new List<LineSegment>(l); LineSegment testLine = temp_l[0]; LineSegment match = temp_l.Find(x => (x.p0 == testLine.p0 )||(x.p0 == testLine.p1) || (x.p1 == testLine.p0) || (x.p1 == testLine.p1)); /edit: missed some code
Why you don't use simple for / foreach loop, to check for match? But not sure, if you will get correct answer, if you have bug somewhere else. Put Debug.Log in appropriate places, to track issue.
Replacing with a foreach was a good idea. It still doesn't work. So something in my data is screwy. Thanks for the help, I should have thought of that. :S
== between two Vectors will only be true if the difference (distance) between them is less than 1e-5 (floating point imprecision), maybe it's floating point fluctuation?
I avoid lambda expressions unless I absolutely have to (unity events to pass arguments easier), it can over complicate code making it hard to read. In saying that, I gave up trying to understand the code you posted, I’m sorry.
Is actually comparing two vectors directly, not checking for its hash value? Indeed, do to floating imprecision, I typically subtract two vectors and get squared magnitude, then compare against tolerance, for example 0.0001f.
In case of float Vectors (both 2 and 3): - == compare against Distance and true if the difference is less than 0.00001f - VectorX.Equals goes for the exact match VectorX.Distance(VectorX a, VectorX b) == (a-b).magnitude So you can use simply the VectorX.Distance() method if you're going for the 0.0001f precision, if you're okay with the 0.00001f, then use == operator.
Personally I would be avoiding using distance (magnitude), if not need to use it. It uses square root, which is "complex" math. There is another magnitude parameter, which is not squared. Or simply sum of raised to power 2 elements. Either way, with custom comparison, you can set resolution as you like, if you need it.
I don't like using the Distance function because it's just an extra call and you don't need the actual distance, i just do (like @Antypodish said) i rarely find the .0001 precision useful, i usually go for .01. Code (CSharp): (vector1 - vector2).sqrMagnitude < .01f