Point
geometry :: IPoint :: defaultinit
geometry $ IPoint :: core_serialize_to
Actual serialization ofself
to serializer
geometry $ IPoint :: from_deserializer
Create an instance of this class from thedeserializer
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
geometry :: IPoint :: defaultinit
geometry :: Boxed :: defaultinit
core :: Object :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
geometry :: Boxed :: intersects
Doesself
intersect with other
?
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
Serializer::serialize
# Abstract 2d point, strongly linked to its implementation `Point`
interface IPoint[N: Numeric]
# Horizontal coordinate
fun x: N is abstract
# Vertical coordinate
fun y: N is abstract
redef fun to_s do return "({x}, {y})"
# Distance with `other`
#
# ~~~
# var p0 = new Point[Float](0.0, 0.0)
# var p1 = new Point[Float](2.0, 3.0)
# assert p0.dist(p1).is_approx(3.6, 0.01)
# ~~~
#
# 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)
end
# Square of the distance with `other`
#
# May be used as an approximation to compare distance between two points.
#
# ~~~
# var p0 = new Point[Float](0.0, 0.0)
# var p1 = new Point[Float](2.0, 3.0)
# assert p0.dist2(p1) == 13.0
# ~~~
#
# 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)
var s = (dx.mul(dx)).add(dy.mul(dy))
return x.value_of(s)
end
# Linear interpolation between `self` and `other` at `p` out of `1.0`
#
# ~~~
# var p0 = new Point[Float](0.0, 0.0)
# var p1 = new Point[Float](2.0, 3.0)
# assert p0.lerp(p1, 0.0) == p0
# assert p0.lerp(p1, 1.0) == p1
# assert p0.lerp(p1, 0.5) == new Point[Float](1.0, 1.5)
# ~~~
#
# TODO 3D implementation.
fun lerp(other: Point[Numeric], p: Float): Point[N]
do
var xx = x.to_f + (other.x.to_f - x.to_f).to_f * p
var yy = y.to_f + (other.y.to_f - y.to_f).to_f * p
return new Point[N](x.value_of(xx), y.value_of(yy))
end
redef fun ==(o) do return o isa IPoint[Numeric] and o.x == x and o.y == y
end
lib/geometry/points_and_lines.nit:22,1--114,3