Skip to content

Instantly share code, notes, and snippets.

@SiAust
Created March 25, 2019 14:54
Show Gist options
  • Save SiAust/0791c49b1e4730ff9703f917ca1487dd to your computer and use it in GitHub Desktop.
Save SiAust/0791c49b1e4730ff9703f917ca1487dd to your computer and use it in GitHub Desktop.
Calculates the distance to a point, with no parameters ( distance to origin 0,0), two parameters x and y or a variable of the type Point.
public class Point {
private int x;
private int y;
public Point(){
//empty constructor (no argument constructor)
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double distance(){
//d(A,B) = sqrt (xB - xA) * (xb - xA) + (yB - yA) * (yB - yA)
return Math.sqrt((0 - this.x) * (0 - this.x) + (0 - this.y) * (0 - this.y));
}
public double distance(int x, int y){
return Math.sqrt((x - this.x) * (x - this.x) + (y - this.y) * (y - this.y));
}
public double distance(Point point){
int x = point.getX();
int y = point.getY();
return Math.sqrt((x - this.x) * (x - this.x) + (y - this.y) * (y - this.y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment