Author: schaveyt
Date: 2007-03-11 21:49:07 -0500 (Sun, 11 Mar 2007)
New Revision: 74088

Modified:
   trunk/cocoa-sharp/src/Cocoa/Point.cs
Log:
Added overrides for Equals, GetHashCode, == operator, and != operator


Modified: trunk/cocoa-sharp/src/Cocoa/Point.cs
===================================================================
--- trunk/cocoa-sharp/src/Cocoa/Point.cs        2007-03-12 02:46:50 UTC (rev 
74087)
+++ trunk/cocoa-sharp/src/Cocoa/Point.cs        2007-03-12 02:49:07 UTC (rev 
74088)
@@ -1,3 +1,5 @@
+using System;
+using System.Runtime.InteropServices;
 using Cocoa;
 
 namespace Cocoa {
@@ -4,10 +6,80 @@
        public struct Point {
                public float X;
                public float Y;
-
+        
                public Point (float x, float y) {
-                       this.X = x;
-                       this.Y = y;
+                       X = x;
+                       Y = y;
                }
-       }
+        
+        public override bool Equals( object aObject )
+        {
+            if( aObject == null )
+            {
+                return false;
+            }
+            
+            if( !(aObject is Point) )
+            {
+                return false;
+            }
+            
+            
+            Point p2 = (Point)aObject;
+            
+            /// We should really use a threshold difference here...
+            ///
+            return X == p2.X && Y == p2.Y;
+        }
+        
+        public bool Equals( Point p2 )
+        {
+            if( (object)p2 == null )
+            {
+                return false;
+            }
+            
+            /// Because we are dealing with floating points...we cannot perfrom
+            /// a straight comparison..but must apply a threshold comparison.
+            ///
+            if( !(Math.Abs( X - p2.X ) < .001) )
+            {
+                return false;
+            }
+            
+            if( !(Math.Abs( Y - p2.Y ) < .001) )
+            {
+                return false;
+            }
+            
+            
+            return true;
+        }
+        
+        public override int GetHashCode()
+        {
+            return (int)X ^ (int)Y;
+        }
+        
+        public static bool operator == ( Point p1, Point p2)
+        {
+            if( System.Object.ReferenceEquals( p1, p2 ) )
+            {
+                return true;
+            }
+            
+            if( (object)p1 == null || (object)p2 == null )
+            {
+                return false;
+            }
+            
+            return p1.Equals( p2 );
+        }
+        
+        public static bool operator != ( Point p1, Point p2)
+        {
+            return !(p1 == p2);
+        }
+        
+    }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to