Skip to content

Instantly share code, notes, and snippets.

@chonghorizons
Last active November 6, 2021 19:08
Show Gist options
  • Save chonghorizons/9049bb071c22e440110ef79a6f415852 to your computer and use it in GitHub Desktop.
Save chonghorizons/9049bb071c22e440110ef79a6f415852 to your computer and use it in GitHub Desktop.
DART: Playing with class, initialization list.
import 'dart:math' as math;
const double originX=5;
const double originY=5;
class Point {
double x;
double y;
Point(this.x, this.y);
Point.zero(): x=0, y=0;
Point.origin(): x=originX, y=originY;
double distanceSquared(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return math.sqrt(dx * dx + dy * dy);
}
}
main() {
print("hello");
Point myPoint=Point(10,5);
print('x is' + myPoint.x.toString());
print('should be zero and is ' + myPoint.distanceSquared(Point(10,5)).toString() );
print('should be 25 and is ' + myPoint.distanceSquared(Point.origin()).toString() );
print('should be 125 and is ' + myPoint.distanceSquared(Point.zero()).toString() );
print('should be unknown and is ' + myPoint.distanceSquared(Point(10,6)).toString() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment