-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoint.java
82 lines (76 loc) · 2.03 KB
/
Point.java
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
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* The Point class stores an abscissa and ordinate.
*/
public class Point{
/**
* The abscissa.
*/
private float x;
/**
* The ordinate.
*/
private float y;
/**
* Constructs a Point given an abscissa and an ordinate.
*@param x the abscissa for the Point
*@param y the ordinate for the Point
*/
public Point(float x, float y){
this.x=x;
this.y=y;
}
/**
* Accessor for the abscissa.
*@return the abscissa of the Point
*/
public float getX(){
return x;
}
/**
* Accessor for the ordinate.
*@return the ordinate of the Point
*/
public float getY(){
return y;
}
/**
* Finds the midpoint of the two points.
* This can overflow and break.
* @param a A point
* @param b A point
* @return The midpoint of a and b.
*/
public static Point midpoint(Point a, Point b){
return new Point((a.x + b.x) / 2, (a.y + b.y) / 2);
}
/**
* Performs the square of the distance between points. This is to avoid doing the useless calculation
* of sqrt, which is expensive.
*
* d^2 = (x1 - x2)^2 + (y1 - y2)^2
* Note that the "^" means exponent and not the exclusive or symbol in java
* @return Distance between this point and the other point. This does not calculate square root
*/
public double distancePortion(Point other){
return Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return Float.compare(point.x, x) == 0 &&
Float.compare(point.y, y) == 0;
}
@Override
public int hashCode(){
return (int) (x * 10_000 + y);
}
@Override
public String toString() {
return String.format("x: %f y: %f", x, y);
}
}