AlmostEquals









How do I use Almost Equals
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: Extensions.cs Copy
33     public static bool AlmostEquals(this Vector3 target, Vector3 second, float sqrMagnitudePrecision)
34     {
35         return (target - second).sqrMagnitude < sqrMagnitudePrecision; // TODO: inline vector methods to optimize?
36     }
File name: Extensions.cs Copy
39     public static bool AlmostEquals(this Vector2 target, Vector2 second, float sqrMagnitudePrecision)
40     {
41         return (target - second).sqrMagnitude < sqrMagnitudePrecision; // TODO: inline vector methods to optimize?
42     }
File name: Extensions.cs Copy
45     public static bool AlmostEquals(this Quaternion target, Quaternion second, float maxAngle)
46     {
47         return Quaternion.Angle(target, second) < maxAngle;
48     }
File name: Extensions.cs Copy
51     public static bool AlmostEquals(this float target, float second, float floatDiff)
52     {
53         return Mathf.Abs(target - second) < floatDiff;
54     }
File name: NetworkingPeer.cs Copy
3271     // calls OnPhotonSerializeView (through ExecuteOnSerialize)
3273     private Hashtable OnSerializeWrite(PhotonView view)
3274     {
3275         PhotonStream pStream = new PhotonStream( true, null );
3276         PhotonMessageInfo info = new PhotonMessageInfo( this.mLocalActor, this.ServerTimeInMilliSeconds, view );
3277
3278         // each view creates a list of values that should be sent
3279         view.SerializeView( pStream, info );
3280
3281         if( pStream.Count == 0 )
3282         {
3283             return null;
3284         }
3285
3286         object[] dataArray = pStream.data.ToArray();
3287
3288         if (view.synchronization == ViewSynchronization.UnreliableOnChange)
3289         {
3290             if (AlmostEquals(dataArray, view.lastOnSerializeDataSent))
3291             {
3292                 if (view.mixedModeIsReliable)
3293                 {
3294                     return null;
3295                 }
3296
3297                 view.mixedModeIsReliable = true;
3298                 view.lastOnSerializeDataSent = dataArray;
3299             }
3300             else
3301             {
3302                 view.mixedModeIsReliable = false;
3303                 view.lastOnSerializeDataSent = dataArray;
3304             }
3305         }
3306
3307         // EVDATA:
3308         // 0=View ID (an int, never compressed cause it's not in the data)
3309         // 1=data of observed type (different per type of observed object)
3310         // 2=compressed data (in this case, key 1 is empty)
3311         // 3=list of values that are actually null (if something was changed but actually IS null)
3312         Hashtable evData = new Hashtable();
3313         evData[(byte)0] = (int)view.viewID;
3314         evData[(byte)1] = dataArray; // this is the actual data (script or observed object)
3315
3316
3317         if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
3318         {
3319             // compress content of data set (by comparing to view.lastOnSerializeDataSent)
3320             // the "original" dataArray is NOT modified by DeltaCompressionWrite
3321             // if something was compressed, the evData key 2 and 3 are used (see above)
3322             bool somethingLeftToSend = this.DeltaCompressionWrite(view, evData);
3323
3324             // buffer the full data set (for next compression)
3325             view.lastOnSerializeDataSent = dataArray;
3326
3327             if (!somethingLeftToSend)
3328             {
3329                 return null;
3330             }
3331         }
3332
3333         return evData;
3334     }
File name: NetworkingPeer.cs Copy
3396     private bool AlmostEquals(object[] lastData, object[] currentContent)
3397     {
3398         if (lastData == null && currentContent == null)
3399         {
3400             return true;
3401         }
3402
3403         if (lastData == null || currentContent == null || (lastData.Length != currentContent.Length))
3404         {
3405             return false;
3406         }
3407
3408         for (int index = 0; index < currentContent.Length; index++)
3409         {
3410             object newObj = currentContent[index];
3411             object oldObj = lastData[index];
3412             if (!this.ObjectIsSameWithInprecision(newObj, oldObj))
3413             {
3414                 return false;
3415             }
3416         }
3417
3418         return true;
3419     }
File name: NetworkingPeer.cs Copy
3546     bool ObjectIsSameWithInprecision(object one, object two)
3547     {
3548         if (one == null || two == null)
3549         {
3550             return one == null && two == null;
3551         }
3552
3553         if (!one.Equals(two))
3554         {
3555             // if A is not B, lets check if A is almost B
3556             if (one is Vector3)
3557             {
3558                 Vector3 a = (Vector3)one;
3559                 Vector3 b = (Vector3)two;
3560                 if (a.AlmostEquals(b, PhotonNetwork.precisionForVectorSynchronization))
3561                 {
3562                     return true;
3563                 }
3564             }
3565             else if (one is Vector2)
3566             {
3567                 Vector2 a = (Vector2)one;
3568                 Vector2 b = (Vector2)two;
3569                 if (a.AlmostEquals(b, PhotonNetwork.precisionForVectorSynchronization))
3570                 {
3571                     return true;
3572                 }
3573             }
3574             else if (one is Quaternion)
3575             {
3576                 Quaternion a = (Quaternion)one;
3577                 Quaternion b = (Quaternion)two;
3578                 if (a.AlmostEquals(b, PhotonNetwork.precisionForQuaternionSynchronization))
3579                 {
3580                     return true;
3581                 }
3582             }
3583             else if (one is float)
3584             {
3585                 float a = (float)one;
3586                 float b = (float)two;
3587                 if (a.AlmostEquals(b, PhotonNetwork.precisionForFloatSynchronization))
3588                 {
3589                     return true;
3590                 }
3591             }
3592
3593             // one does not equal two
3594             return false;
3595         }
3596
3597         return true;
3598     }

AlmostEquals 118 lượt xem

Gõ tìm kiếm nhanh...