Point2D class
A point in 2D cartesian space.
class Point2D implements Comparable<Point2D>{ /// the x-coordinate final num x; /// the y-coordinate final num y; /** * Creates a point for the [x] and [y] coordinate. */ Point2D(this.x, this.y); /** * Creates a point from another object [other]. * * ## Possible values for [other] * * * another [Point2D], in which case [other] is cloned * * a [List] with exactly two [num] values. * * ## Examples * * var p1 = new Point2D.from([0,1]); * var p2 = new Point2D.from(p1); */ factory Point2D.from(other) { fromPoint(other) => new Point2D(other.x, other.y); fromList(other) { _require(other.length == 2, "expected list of length 2"); _require(other.every((e) => e is num), "expected elements of type num only"); return new Point2D(other[0], other[1]); } if (other is Point2D) return fromPoint(other); if (other is List) return fromList(other); _require(false, "expected Point2D or List, got $other"); } /** * Creates a new point at position (0,0) */ Point2D.origin() : this(0,0); int compareTo(Point2D other) { var c = x.compareTo(other.x); if (c != 0) return c; return y.compareTo(other.y); } bool operator==(other) => x == other.x && y == other.y; /** * Creates a new point by adding [other] to this point. * * ### Possible values for [other] * * [Point2D] - vector addition * * [Dimension] - adds `x` and `width`, and `y` and `height` */ Point2D operator +(other) { if (other is Point2D) return new Point2D(x + other.x, y + other.y); if (other is Dimension) return new Point2D(x + other.width, y + other.height); throw new ArgumentError("expected Point2D or Dimension, got $other"); } Point2D operator -() => new Point2D(-x, -y); /** * Creates a new point by subtracting [other] from this point. * * ### Possible values for [other] * * [Point2D] - vector subtraction * * [Dimension] - subtracts `width` from `x`, and `height` from `y` */ Point2D operator -(other) { if (other is Point2D) return new Point2D(x - other.x, y - other.y); if (other is Dimension) return new Point2D(x - other.width, y - other.height); throw new ArgumentError("expected Point2D or Dimension, got $other"); } Point2D operator *(num factor) => new Point2D(x * factor, y * factor); Point2D operator /(num divisor) => new Point2D(x / divisor, y / divisor); int get hashCode => x.hashCode * 31 + y.hashCode; /// returns a new point whose coordinates are converted to [int] Point2D toInt() => new Point2D(x.toInt(), y.toInt()); /// returns a new point with the minimal coordinates of this and [other] Point2D min(other) => new Point2D(math.min(x, other.x), math.min(y, other.y)); /// returns a new point with the maximal coordinates of this and [other] Point2D max(other) => new Point2D(math.max(x, other.x), math.max(y, other.y)); /// returns a new point translate by [dx] in x direction and [dy] in /// y direction Point2D translate({num dx:0, num dy: 0}) => new Point2D(x + dx, y + dy); /// returns a new point scaled by [sx] in x direction and by [sy] /// in y direction Point2D scale({num sx:1, num sy:1}) => new Point2D(x* sx, y * sy); /// returns a new point for which the sign of the y-coordinates is inverted Point2D flipY() => scale(sy:-1); /** * returns a new point whose coordinates are truncated to * [afterDecimalPoint] digits after the decimal point. */ Point2D truncate([num afterDecimalPoint=0]) { var d = math.pow(10, afterDecimalPoint); var x= (this.x * d).truncateToDouble() / d; var y= (this.y * d).truncateToDouble() / d; return new Point2D(x,y); } String toString() => "{Point2D: x=$x, y=$y}"; }
Implements
Constructors
factory Point2D.from(other) #
Creates a point from another object other.
Possible values for other
- another Point2D, in which case other is cloned
- a
List
with exactly twonum
values.
Examples
var p1 = new Point2D.from([0,1]);
var p2 = new Point2D.from(p1);
factory Point2D.from(other) { fromPoint(other) => new Point2D(other.x, other.y); fromList(other) { _require(other.length == 2, "expected list of length 2"); _require(other.every((e) => e is num), "expected elements of type num only"); return new Point2D(other[0], other[1]); } if (other is Point2D) return fromPoint(other); if (other is List) return fromList(other); _require(false, "expected Point2D or List, got $other"); }
new Point2D.origin() #
Creates a new point at position (0,0)
Point2D.origin() : this(0,0);
Properties
final int hashCode #
Get a hash code for this object.
All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator ==
. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.
If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.
int get hashCode => x.hashCode * 31 + y.hashCode;
Operators
Point2D operator +(other) #
Creates a new point by adding other to this point.
Possible values for other
Point2D operator +(other) { if (other is Point2D) return new Point2D(x + other.x, y + other.y); if (other is Dimension) return new Point2D(x + other.width, y + other.height); throw new ArgumentError("expected Point2D or Dimension, got $other"); }
Point2D operator -(other) #
Creates a new point by subtracting other from this point.
Possible values for other
Point2D operator -(other) { if (other is Point2D) return new Point2D(x - other.x, y - other.y); if (other is Dimension) return new Point2D(x - other.width, y - other.height); throw new ArgumentError("expected Point2D or Dimension, got $other"); }
Point2D operator *(num factor) #
Point2D operator *(num factor) => new Point2D(x * factor, y * factor);
Point2D operator /(num divisor) #
Point2D operator /(num divisor) => new Point2D(x / divisor, y / divisor);
bool operator ==(other) #
The equality operator.
The default behavior for all Object
s is to return true if and
only if this
and
other are the same object.
If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.
bool operator==(other) => x == other.x && y == other.y;
Methods
int compareTo(Point2D other) #
Compares this object to another Comparable
Returns a value like a Comparator
when comparing this
to
other.
May throw an ArgumentError
if
other is of a type that
is not comparable to this
.
int compareTo(Point2D other) { var c = x.compareTo(other.x); if (c != 0) return c; return y.compareTo(other.y); }
Point2D flipY() #
returns a new point for which the sign of the y-coordinates is inverted
Point2D flipY() => scale(sy:-1);
Point2D max(other) #
returns a new point with the maximal coordinates of this and other
Point2D max(other) => new Point2D(math.max(x, other.x), math.max(y, other.y));
Point2D min(other) #
returns a new point with the minimal coordinates of this and other
Point2D min(other) => new Point2D(math.min(x, other.x), math.min(y, other.y));
Point2D scale({num sx: 1, num sy: 1}) #
returns a new point scaled by sx in x direction and by sy in y direction
Point2D scale({num sx:1, num sy:1}) => new Point2D(x* sx, y * sy);
Point2D toInt() #
returns a new point whose coordinates are converted to int
Point2D toInt() => new Point2D(x.toInt(), y.toInt());
String toString() #
Returns a string representation of this object.
String toString() => "{Point2D: x=$x, y=$y}";
Point2D translate({num dx: 0, num dy: 0}) #
returns a new point translate by dx in x direction and dy in y direction
Point2D translate({num dx:0, num dy: 0}) => new Point2D(x + dx, y + dy);
Point2D truncate([num afterDecimalPoint = 0]) #
returns a new point whose coordinates are truncated to afterDecimalPoint digits after the decimal point.
Point2D truncate([num afterDecimalPoint=0]) { var d = math.pow(10, afterDecimalPoint); var x= (this.x * d).truncateToDouble() / d; var y= (this.y * d).truncateToDouble() / d; return new Point2D(x,y); }