distanceTo method

double distanceTo(
  1. Point otherPoint
)

Computes the Euclidean distance between this point and another.

If both points are 3-dimensional (i.e., z is not null), the Euclidean distance in 3D space is returned. Otherwise, the 2D Euclidean distance is returned.

Example 1:

var p1 = Point(3, 4);
var p2 = Point(6, 8);
print(p1.distanceTo(p2)); // Output: 5.0

Example 2:

var point1 = Point(1, 2, 3);
var point2 = Point(4, 5, 6);
print(point1.distanceTo(point2)); // Output: 5.196152422706632

Returns the distance as a double.

Implementation

double distanceTo(Point otherPoint) {
  var dx = otherPoint.x - x;
  var dy = otherPoint.y - y;
  if (z != null && otherPoint.z != null) {
    var dz = otherPoint.z! - z!;
    return sqrt(dx * dx + dy * dy + dz * dz);
  } else {
    return sqrt(dx * dx + dy * dy);
  }
}