forked from benaich/JavaDataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
99 lines (86 loc) · 2.26 KB
/
Point.java
File metadata and controls
99 lines (86 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package ECC;
import java.math.BigInteger;
public class Point {
private BigInteger x;
private BigInteger y;
private boolean isInfinity;
private static Point INFINITY;
public Point(BigInteger x, BigInteger y) {
this.x = x;
this.y = y;
isInfinity = false;
}
public Point(Point p) {
this.x = p.getX();
this.y = p.getY();
isInfinity = p.isInfinity();
}
public Point(long x, long y) {
this( BigInteger.valueOf(x), BigInteger.valueOf(y));
}
public Point() {
this.x = this.y = BigInteger.ZERO;
isInfinity = true;
}
public static Point make(int[] array){
String x, y;
x = y = "";
for (int i = 0; i < 2 * ECC.PAD; i++) {
if(i<ECC.PAD)
x += array[i];
else y += array[i];
}
return new Point(Integer.parseInt(x, 2), Integer.parseInt(y, 2));
}
public BigInteger getX() {
return x;
}
public BigInteger getY() {
return y;
}
public boolean isInfinity() {
return isInfinity;
}
public Point negate() {
if (isInfinity()) {
return getInfinity();
}
return new Point(x, y.negate());
}
public static Point getInfinity() {
if(INFINITY == null)
INFINITY = new Point();
return INFINITY;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof Point) {
if (this.x != null) {
Point otherPoint = (Point)other;
// no need to check for null in this case
return this.x.equals(otherPoint.x) &&
this.y.equals(otherPoint.y);
} else {
return other == getInfinity();
}
}
return false;
}
@Override
public String toString() {
if (isInfinity()) {
return "INFINITY";
} else {
return "(" + x.toString() + ", " + y.toString() + ")";
}
}
@Override
public int hashCode(){
if (this.isInfinity) {
return x.hashCode() * 31 + x.hashCode();
}
return 11;
}
}