nitlanguage
/
nit.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
1400e65
)
geometry: implement `Point::dist` for 3D points
author
Alexis Laferrière
<alexis.laf@xymus.net>
Sat, 20 May 2017 17:45:03 +0000
(10:45 -0700)
committer
Alexis Laferrière
<alexis.laf@xymus.net>
Sun, 28 May 2017 19:45:16 +0000
(15:45 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>
lib/geometry/points_and_lines.nit
patch
|
blob
|
history
diff --git
a/lib/geometry/points_and_lines.nit
b/lib/geometry/points_and_lines.nit
index
eef5606
..
16efae0
100644
(file)
--- a/
lib/geometry/points_and_lines.nit
+++ b/
lib/geometry/points_and_lines.nit
@@
-38,7
+38,16
@@
interface IPoint[N: Numeric]
# assert p0.dist(p1).is_approx(3.6, 0.01)
# ~~~
#
# 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)
fun dist(other: Point[Numeric]): N
do
return x.value_of(dist2(other).to_f.sqrt)
@@
-54,8
+63,28
@@
interface IPoint[N: Numeric]
# assert p0.dist2(p1) == 13.0
# ~~~
#
# 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
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)
do
var dx = other.x.sub(x)
var dy = other.y.sub(y)
@@
-100,6
+129,19
@@
interface IPoint3d[N: Numeric]
fun z: N is abstract
redef fun to_s do return "({x}, {y}, {z})"
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`
end
# 3D point with `x`, `y` and `z`