# assert p0.dist(p1).is_approx(3.6, 0.01)
# ~~~
#
- # TODO 3D implementation.
+ # If `self` or `other` are in 3D, the distance takes into account the 3 axes.
+ # For a 2D point, the Z coordinate is considered to be 0.
+ #
+ # ~~~
+ # var p2 = new Point3d[Float](0.0, 0.0, 0.0)
+ # var p3 = new Point3d[Float](2.0, 3.0, 4.0)
+ # var p4 = new Point[Float](2.0, 3.0)
+ # assert p2.dist(p3).is_approx(5.385, 0.01)
+ # assert p2.dist(p4).is_approx(3.606, 0.01)
+ # ~~~
fun dist(other: Point[Numeric]): N
do
return x.value_of(dist2(other).to_f.sqrt)
# assert p0.dist2(p1) == 13.0
# ~~~
#
- # TODO 3D implementation.
+ # If `self` or `other` are in 3D, the distance takes into account the 3 axes.
+ # For a 2D point, the Z coordinate is considered to be 0.
+ #
+ # ~~~
+ # var p2 = new Point3d[Float](0.0, 0.0, 0.0)
+ # var p3 = new Point3d[Float](2.0, 3.0, 4.0)
+ # var p4 = new Point[Float](2.0, 3.0)
+ # assert p2.dist2(p3).is_approx(29.0, 0.01)
+ # assert p2.dist2(p4).is_approx(13.0, 0.01)
+ # assert p4.dist2(p2).is_approx(13.0, 0.01)
+ # ~~~
fun dist2(other: Point[Numeric]): N
+ do return x.value_of(other.dist2_with_2d(self))
+
+ private fun dist2_with_2d(other: IPoint[Numeric]): Numeric
+ do return dist2_xy(other)
+
+ private fun dist2_with_3d(other: IPoint3d[Numeric]): Numeric
+ do return dist2_xy(other).add(other.z.mul(other.z))
+
+ # Square of the distance with `other` on the X and Y axes
+ private fun dist2_xy(other: IPoint[N]): N
do
var dx = other.x.sub(x)
var dy = other.y.sub(y)
fun z: N is abstract
redef fun to_s do return "({x}, {y}, {z})"
+
+ redef fun dist2(other)
+ do return x.value_of(other.dist2_with_3d(self))
+
+ redef fun dist2_with_2d(other)
+ do return dist2_xy(other).add(z.mul(z))
+
+ redef fun dist2_with_3d(other)
+ do
+ var dz = other.z.sub(z)
+ var s = dist2_xy(other).add(dz.mul(dz))
+ return x.value_of(s)
+ end
end
# 3D point with `x`, `y` and `z`