Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active November 8, 2023 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trikitrok/3608503f2df6bafa71be499911b9242a to your computer and use it in GitHub Desktop.
Save trikitrok/3608503f2df6bafa71be499911b9242a to your computer and use it in GitHub Desktop.
// From Rachel M. Carmena's https://github.com/rachelcarmena/code-smells
class DistanceCalculator {
betweenPoints(x1, y1, x2, y2) {
return Math.sqrt(Math.pow((y2 - y1), 2) + Math.pow(x2 - x1, 2));
}
toOriginFrom(x, y) {
return Math.sqrt(Math.pow(y, 2) + Math.pow(x, 2));
}
}
// In some client
const distanceCalculator = new DistanceCalculator();
const distance = distanceCalculator.betweenPoints(1, 2, 3, 4);
const distanceToOrigin = distanceCalculator.toOriginFrom(5, 5);
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
export class Point {
static origin() {
return Point.new(0, 0);
}
static new(x, y) {
return new Point(x, y);
}
constructor(x, y) {
this.x = x;
this.y = y;
}
distanceTo(another) {
return Math.sqrt(
Math.pow((another.y - this.y), 2) +
Math.pow(another.x - this.x, 2)
);
}
}
// In some client
const distance = Point.new(1, 2).distanceTo(Point.new(3, 4));
const distanceToOrigin = Point.origin().distanceTo(Point.new(5, 5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment