Last active
August 29, 2015 14:06
-
-
Save chmullig/3658bf500e1f3b1a1a49 to your computer and use it in GitHub Desktop.
point-distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Point { | |
private double x; | |
private double y; | |
public Point(double x, double y) { | |
this.x = x; | |
this.y = y; | |
} | |
public double distance(Point that) { | |
double xdist = this.x - that.x; | |
double ydist = this.y - that.y; | |
double distance = Math.sqrt( (xdist*xdist) + (ydist*ydist) ); | |
return distance; | |
} | |
//imagine getX, getY, setX, setY functions here | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//// in some main file, imagine using it like so: | |
public class Main { | |
public static void main { | |
Point p1 = new Point(1, 2); | |
Point p2 = new Point(10, -2); | |
int distance = p1.distance(p2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __DISTANCE_H__ | |
#define __DISTANCE_H__ | |
/* Let's create the simplest struct we could want, a pair of numbers */ | |
struct point { | |
double x; | |
double y; | |
}; | |
/* now let's define a distance function that takes two struct points*/ | |
double distance(struct point p1, struct point p2); | |
/* just for fun, let's define a distance function that takes 4 doubles directly */ | |
double distance(double x1, double y1, double x2, double y2); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "distance.h" | |
#include <math.h> /* need it for the sqrt function */ | |
double distance(struct point p1, struct point p2) { | |
double xdist = p1.x - p2.x; | |
double ydist = p1.y - p2.y; | |
double distance = sqrt( (xdist*xdist) + (ydist*ydist) ); | |
return distance; | |
} | |
double distance(double x1, double y1, double x2, double y2) { | |
double xdist = x1 - x2; | |
double ydist = y1 - y2; | |
double distance = sqrt( (xdist*xdist) + (ydist*ydist) ); | |
return distance; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "distance.h" | |
int main(int argc, char **argv) { | |
struct point p1; | |
p1.x = 1; | |
p1.y = 2; | |
struct point p2; | |
p2.x = 10; | |
p2.y = -2; | |
double dist = distance(p1, p2); | |
/* here's an alternate way of doing it, without using structs */ | |
double x1 = 1; | |
double y1 = 2; | |
double x2 = 10; | |
double y2 = -2; | |
dist = distance(1, 2, 10, -2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment