Prettify scan result screen, I think
[zxing.git] / csharp / ResultPoint.cs
index 378c0cd..def8e2f 100755 (executable)
@@ -1,5 +1,5 @@
-/*\r
-* Copyright 2008 ZXing authors\r
+/*\r
+* Copyright 2007 ZXing authors\r
 *\r
 * Licensed under the Apache License, Version 2.0 (the "License");\r
 * you may not use this file except in compliance with the License.\r
 * See the License for the specific language governing permissions and\r
 * limitations under the License.\r
 */\r
+using System;\r
 namespace com.google.zxing\r
 {\r
-    using System;\r
-    using System.Text;\r
+       \r
+       /// <summary> <p>Encapsulates a point of interest in an image containing a barcode. Typically, this\r
+       /// would be the location of a finder pattern or the corner of the barcode, for example.</p>\r
+       /// \r
+       /// </summary>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
 \r
-    /// <summary> A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a\r
-    /// unsigned container, it's up to you to do byteValue & 0xff at each location.\r
-    /// *\r
-    /// JAVAPORT: I'm not happy about the argument ordering throughout the file, as I always like to have\r
-    /// the horizontal component first, but this is for compatibility with the C++ code. The original\r
-    /// code was a 2D array of ints, but since it only ever gets assigned -1, 0, and 1, I'm going to use\r
-    /// less memory and go with bytes.\r
-    /// *\r
-    /// </summary>\r
-    /// <author>  dswitkin@google.com (Daniel Switkin)\r
-    /// \r
-    /// </author>\r
-    public interface ResultPoint\r
-    {\r
-        float getX();\r
-        float getY();\r
-    }\r
+       public class ResultPoint\r
+       {\r
+               virtual public float X\r
+               {\r
+                       get\r
+                       {\r
+                               return x;\r
+                       }\r
+                       \r
+               }\r
+               virtual public float Y\r
+               {\r
+                       get\r
+                       {\r
+                               return y;\r
+                       }\r
+                       \r
+               }\r
+               \r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'x '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private float x;\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'y '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private float y;\r
+               \r
+               public ResultPoint(float x, float y)\r
+               {\r
+                       this.x = x;\r
+                       this.y = y;\r
+               }\r
+               \r
+               public  override bool Equals(System.Object other)\r
+               {\r
+                       if (other is ResultPoint)\r
+                       {\r
+                               ResultPoint otherPoint = (ResultPoint) other;\r
+                               return x == otherPoint.x && y == otherPoint.y;\r
+                       }\r
+                       return false;\r
+               }\r
+               \r
+               public override int GetHashCode()\r
+               {\r
+            // Redivivus.in Java to c# Porting update\r
+            // 30/01/2010 \r
+            // Commented function body\r
+\r
+                       //return 31 * Float.floatToIntBits(x) + Float.floatToIntBits(y);\r
+            return 0;\r
+               }\r
+               \r
+               public override System.String ToString()\r
+               {\r
+                       System.Text.StringBuilder result = new System.Text.StringBuilder(25);\r
+                       result.Append('(');\r
+                       result.Append(x);\r
+                       result.Append(',');\r
+                       result.Append(y);\r
+                       result.Append(')');\r
+                       return result.ToString();\r
+               }\r
+               \r
+               /// <summary> <p>Orders an array of three ResultPoints in an order [A,B,C] such that AB < AC and\r
+               /// BC < AC and the angle between BC and BA is less than 180 degrees.\r
+               /// </summary>\r
+               public static void  orderBestPatterns(ResultPoint[] patterns)\r
+               {\r
+                       \r
+                       // Find distances between pattern centers\r
+                       float zeroOneDistance = distance(patterns[0], patterns[1]);\r
+                       float oneTwoDistance = distance(patterns[1], patterns[2]);\r
+                       float zeroTwoDistance = distance(patterns[0], patterns[2]);\r
+                       \r
+                       ResultPoint pointA, pointB, pointC;\r
+                       // Assume one closest to other two is B; A and C will just be guesses at first\r
+                       if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance)\r
+                       {\r
+                               pointB = patterns[0];\r
+                               pointA = patterns[1];\r
+                               pointC = patterns[2];\r
+                       }\r
+                       else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance)\r
+                       {\r
+                               pointB = patterns[1];\r
+                               pointA = patterns[0];\r
+                               pointC = patterns[2];\r
+                       }\r
+                       else\r
+                       {\r
+                               pointB = patterns[2];\r
+                               pointA = patterns[0];\r
+                               pointC = patterns[1];\r
+                       }\r
+                       \r
+                       // Use cross product to figure out whether A and C are correct or flipped.\r
+                       // This asks whether BC x BA has a positive z component, which is the arrangement\r
+                       // we want for A, B, C. If it's negative, then we've got it flipped around and\r
+                       // should swap A and C.\r
+                       if (crossProductZ(pointA, pointB, pointC) < 0.0f)\r
+                       {\r
+                               ResultPoint temp = pointA;\r
+                               pointA = pointC;\r
+                               pointC = temp;\r
+                       }\r
+                       \r
+                       patterns[0] = pointA;\r
+                       patterns[1] = pointB;\r
+                       patterns[2] = pointC;\r
+               }\r
+               \r
+               \r
+               /// <returns> distance between two points\r
+               /// </returns>\r
+               public static float distance(ResultPoint pattern1, ResultPoint pattern2)\r
+               {\r
+                       float xDiff = pattern1.X - pattern2.X;\r
+                       float yDiff = pattern1.Y - pattern2.Y;\r
+                       //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+                       return (float) System.Math.Sqrt((double) (xDiff * xDiff + yDiff * yDiff));\r
+               }\r
+               \r
+               /// <summary> Returns the z component of the cross product between vectors BC and BA.</summary>\r
+               private static float crossProductZ(ResultPoint pointA, ResultPoint pointB, ResultPoint pointC)\r
+               {\r
+                       float bX = pointB.x;\r
+                       float bY = pointB.y;\r
+                       return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX));\r
+               }\r
+       }\r
 }
\ No newline at end of file