Search Unity

Lamda help - C#

Discussion in 'Scripting' started by jalapen0, Nov 5, 2018.

  1. jalapen0

    jalapen0

    Joined:
    Feb 20, 2013
    Posts:
    136
    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):
    1. List<LineSegment> temp_l = new List<LineSegment>(l);
    2.  
    3. LineSegment testLine = temp_l[0];
    4.  
    5. LineSegment match = temp_l.Find(x => (x.p0 == testLine.p0 )||(x.p0 == testLine.p1) || (x.p1 == testLine.p0) || (x.p1 == testLine.p1));
    6.  
    7.  

    /edit: missed some code
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    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.
     
    jalapen0 likes this.
  3. jalapen0

    jalapen0

    Joined:
    Feb 20, 2013
    Posts:
    136

    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
     
  4. == 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?
     
    jalapen0 likes this.
  5. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    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.
     
    Kurt-Dekker likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    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.
     
    jalapen0 likes this.
  7. 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.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    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.
     
    Lurking-Ninja likes this.
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    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):
    1. (vector1 - vector2).sqrMagnitude < .01f
     
  10. jalapen0

    jalapen0

    Joined:
    Feb 20, 2013
    Posts:
    136
    Thanks for the example. I will test later today.
     
  11. jalapen0

    jalapen0

    Joined:
    Feb 20, 2013
    Posts:
    136
    Worked great, thank you for everyone's help.
     
    Kurt-Dekker likes this.