Korean translation from Chang Hyun Park
[zxing.git] / csharp / ResultPoint.cs
1 /*\r
2 * Copyright 2007 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 namespace com.google.zxing\r
18 {\r
19         \r
20         /// <summary> <p>Encapsulates a point of interest in an image containing a barcode. Typically, this\r
21         /// would be the location of a finder pattern or the corner of the barcode, for example.</p>\r
22         /// \r
23         /// </summary>\r
24         /// <author>  Sean Owen\r
25         /// </author>\r
26         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
27         /// </author>\r
28 \r
29         public class ResultPoint\r
30         {\r
31                 virtual public float X\r
32                 {\r
33                         get\r
34                         {\r
35                                 return x;\r
36                         }\r
37                         \r
38                 }\r
39                 virtual public float Y\r
40                 {\r
41                         get\r
42                         {\r
43                                 return y;\r
44                         }\r
45                         \r
46                 }\r
47                 \r
48                 //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
49                 private float x;\r
50                 //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
51                 private float y;\r
52                 \r
53                 public ResultPoint(float x, float y)\r
54                 {\r
55                         this.x = x;\r
56                         this.y = y;\r
57                 }\r
58                 \r
59                 public  override bool Equals(System.Object other)\r
60                 {\r
61                         if (other is ResultPoint)\r
62                         {\r
63                                 ResultPoint otherPoint = (ResultPoint) other;\r
64                                 return x == otherPoint.x && y == otherPoint.y;\r
65                         }\r
66                         return false;\r
67                 }\r
68                 \r
69                 public override int GetHashCode()\r
70                 {\r
71             // Redivivus.in Java to c# Porting update\r
72             // 30/01/2010 \r
73             // Commented function body\r
74 \r
75                         //return 31 * Float.floatToIntBits(x) + Float.floatToIntBits(y);\r
76             return 0;\r
77                 }\r
78                 \r
79                 public override System.String ToString()\r
80                 {\r
81                         System.Text.StringBuilder result = new System.Text.StringBuilder(25);\r
82                         result.Append('(');\r
83                         result.Append(x);\r
84                         result.Append(',');\r
85                         result.Append(y);\r
86                         result.Append(')');\r
87                         return result.ToString();\r
88                 }\r
89                 \r
90                 /// <summary> <p>Orders an array of three ResultPoints in an order [A,B,C] such that AB < AC and\r
91                 /// BC < AC and the angle between BC and BA is less than 180 degrees.\r
92                 /// </summary>\r
93                 public static void  orderBestPatterns(ResultPoint[] patterns)\r
94                 {\r
95                         \r
96                         // Find distances between pattern centers\r
97                         float zeroOneDistance = distance(patterns[0], patterns[1]);\r
98                         float oneTwoDistance = distance(patterns[1], patterns[2]);\r
99                         float zeroTwoDistance = distance(patterns[0], patterns[2]);\r
100                         \r
101                         ResultPoint pointA, pointB, pointC;\r
102                         // Assume one closest to other two is B; A and C will just be guesses at first\r
103                         if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance)\r
104                         {\r
105                                 pointB = patterns[0];\r
106                                 pointA = patterns[1];\r
107                                 pointC = patterns[2];\r
108                         }\r
109                         else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance)\r
110                         {\r
111                                 pointB = patterns[1];\r
112                                 pointA = patterns[0];\r
113                                 pointC = patterns[2];\r
114                         }\r
115                         else\r
116                         {\r
117                                 pointB = patterns[2];\r
118                                 pointA = patterns[0];\r
119                                 pointC = patterns[1];\r
120                         }\r
121                         \r
122                         // Use cross product to figure out whether A and C are correct or flipped.\r
123                         // This asks whether BC x BA has a positive z component, which is the arrangement\r
124                         // we want for A, B, C. If it's negative, then we've got it flipped around and\r
125                         // should swap A and C.\r
126                         if (crossProductZ(pointA, pointB, pointC) < 0.0f)\r
127                         {\r
128                                 ResultPoint temp = pointA;\r
129                                 pointA = pointC;\r
130                                 pointC = temp;\r
131                         }\r
132                         \r
133                         patterns[0] = pointA;\r
134                         patterns[1] = pointB;\r
135                         patterns[2] = pointC;\r
136                 }\r
137                 \r
138                 \r
139                 /// <returns> distance between two points\r
140                 /// </returns>\r
141                 public static float distance(ResultPoint pattern1, ResultPoint pattern2)\r
142                 {\r
143                         float xDiff = pattern1.X - pattern2.X;\r
144                         float yDiff = pattern1.Y - pattern2.Y;\r
145                         //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
146                         return (float) System.Math.Sqrt((double) (xDiff * xDiff + yDiff * yDiff));\r
147                 }\r
148                 \r
149                 /// <summary> Returns the z component of the cross product between vectors BC and BA.</summary>\r
150                 private static float crossProductZ(ResultPoint pointA, ResultPoint pointB, ResultPoint pointC)\r
151                 {\r
152                         float bX = pointB.x;\r
153                         float bY = pointB.y;\r
154                         return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX));\r
155                 }\r
156         }\r
157 }