Interfaces and classes to represent basic geometry needs.

Introduced classes

interface ILine[N: Numeric]

geometry :: ILine

Abstract 2D line segment between two ordered points
interface ILine3d[N: Numeric]

geometry :: ILine3d

Abstract 3D line segment between two ordered points
interface IPoint[N: Numeric]

geometry :: IPoint

Abstract 2d point, strongly linked to its implementation Point
interface IPoint3d[N: Numeric]

geometry :: IPoint3d

Abstract 3d point, strongly linked to its implementation Point3d
class Line[N: Numeric]

geometry :: Line

2D line segment between two ordered points
class Line3d[N: Numeric]

geometry :: Line3d

3D line segment between two ordered points
class Point[N: Numeric]

geometry :: Point

2D point with x and z
class Point3d[N: Numeric]

geometry :: Point3d

3D point with x, y and z

Redefined classes

redef abstract class Deserializer

geometry :: points_and_lines $ Deserializer

Abstract deserialization service

All class definitions

redef abstract class Deserializer

geometry :: points_and_lines $ Deserializer

Abstract deserialization service
interface ILine[N: Numeric]

geometry $ ILine

Abstract 2D line segment between two ordered points
interface ILine3d[N: Numeric]

geometry $ ILine3d

Abstract 3D line segment between two ordered points
interface IPoint[N: Numeric]

geometry $ IPoint

Abstract 2d point, strongly linked to its implementation Point
interface IPoint3d[N: Numeric]

geometry $ IPoint3d

Abstract 3d point, strongly linked to its implementation Point3d
class Line[N: Numeric]

geometry $ Line

2D line segment between two ordered points
class Line3d[N: Numeric]

geometry $ Line3d

3D line segment between two ordered points
class Point[N: Numeric]

geometry $ Point

2D point with x and z
class Point3d[N: Numeric]

geometry $ Point3d

3D point with x, y and z
package_diagram geometry::points_and_lines points_and_lines serialization serialization geometry::points_and_lines->serialization poset poset serialization->poset meta meta serialization->meta json json serialization->json ...poset ... ...poset->poset ...meta ... ...meta->meta ...json ... ...json->json geometry::boxes boxes geometry::boxes->geometry::points_and_lines geometry::angles angles geometry::angles->geometry::points_and_lines geometry::polygon polygon geometry::polygon->geometry::points_and_lines geometry::geometry geometry geometry::geometry->geometry::boxes geometry::geometry->geometry::angles geometry::quadtree quadtree geometry::quadtree->geometry::boxes geometry::geometry... ... geometry::geometry...->geometry::geometry geometry::quadtree... ... geometry::quadtree...->geometry::quadtree a_star-m a_star-m a_star-m->geometry::polygon a_star-m... ... a_star-m...->a_star-m

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module serialization

serialization :: serialization

General serialization services

Children

module angles

geometry :: angles

Angle related service using Float to represent an angle in radians
module boxes

geometry :: boxes

Provides interfaces and classes to represent basic geometry needs.
module polygon

geometry :: polygon

Convex Polygons manipulations

Descendants

module a_star-m

a_star-m

module bmfont

gamnit :: bmfont

Parse Angel Code BMFont format and draw text
module camera_control

gamnit :: camera_control

Simple camera control for user, as the method accept_scroll_and_zoom
module camera_control_android

gamnit :: camera_control_android

Two fingers camera manipulation, pinch to zoom and slide to scroll
module camera_control_linux

gamnit :: camera_control_linux

Mouse wheel and middle mouse button to control camera
module cameras

gamnit :: cameras

Camera services producing Model-View-Projection matrices
module cameras_cache

gamnit :: cameras_cache

Cache the Matrix produced by Camera::mvp_matrix
module cardboard

gamnit :: cardboard

Update the orientation of world_camera at each frame using the head position given by android::cardboard
module depth

gamnit :: depth

Framework for 3D games in Nit
module depth_core

gamnit :: depth_core

Base entities of the depth 3D game framework
module flat

gamnit :: flat

Simple API for 2D games, built around Sprite and App::update
module flat_core

gamnit :: flat_core

Core services for the flat API for 2D games
module font

gamnit :: font

Abstract font drawing services, implemented by bmfont and tileset
module geometry

geometry :: geometry

Provides interfaces and classes to represent basic geometry needs.
module model_dimensions

gamnit :: model_dimensions

Dimensions related services for Model and Mesh
module more_lights

gamnit :: more_lights

More implementations of Light
module more_materials

gamnit :: more_materials

Various material implementations
module more_meshes

gamnit :: more_meshes

More simple geometric meshes
module more_models

gamnit :: more_models

Services to load models from the assets folder
module particles

gamnit :: particles

Particle effects
module quadtree

geometry :: quadtree

QuadTree API mostly used for 2 dimensional collision detection
module selection

gamnit :: selection

Select Actor from a screen coordinate
module shadow

gamnit :: shadow

Shadow mapping using a depth texture
module stereoscopic_view

gamnit :: stereoscopic_view

Refine EulerCamera and App::frame_core_draw to get a stereoscopic view
module tileset

gamnit :: tileset

Support for TileSet, TileSetFont and drawing text with TextSprites
module virtual_gamepad

gamnit :: virtual_gamepad

Virtual gamepad mapped to keyboard keys for quick and dirty mobile support
module vr

gamnit :: vr

VR support for gamnit depth, for Android only
# Interfaces and classes to represent basic geometry needs.
module points_and_lines is serialize

import serialization

# 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

# 2D point with `x` and `z`
class Point[N: Numeric]
	super IPoint[N]

	redef var x: N = 0.0 is writable, optional
	redef var y: N = 0.0 is writable, optional
end

# Abstract 3d point, strongly linked to its implementation `Point3d`
interface IPoint3d[N: Numeric]
	super IPoint[N]

	# Depth coordinate
	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

	# Get a new `Point3d[Float]` at an offset of `x, y, z` from `self`
	#
	# ~~~
	# var origin = new Point3d[Float](1.0, 1.0, 1.0)
	# assert origin.offset(1.0, 2.0, 3.0).to_s == "(2.0, 3.0, 4.0)"
	# ~~~
	fun offset(x, y, z: Numeric): Point3d[Float]
	do return new Point3d[Float](self.x.to_f+x.to_f,
	                             self.y.to_f+y.to_f,
	                             self.z.to_f+z.to_f)
end

# 3D point with `x`, `y` and `z`
class Point3d[N: Numeric]
	super IPoint3d[N]
	super Point[N]

	redef var z: N = 0.0 is writable, optional
end

# Abstract 2D line segment between two ordered points
interface ILine[N: Numeric]
	# The type of points that ends the segment
	type P: IPoint[N]

	# Point at the left-end of the segment
	fun point_left: P is abstract

	# Point at the right-end of the segment
	fun point_right: P is abstract

	redef fun to_s do return "{point_left}--{point_right}"
end

# 2D line segment between two ordered points
class Line[N: Numeric]
	super ILine[N]

	redef var point_left
	redef var point_right

	init
	do
		var a = point_left
		var b = point_right
		if a.x > b.x then
			point_left = b
			point_right = a
		end
	end
end

# Abstract 3D line segment between two ordered points
interface ILine3d[N: Numeric]
	super ILine[N]

	redef type P: IPoint3d[N]
end

# 3D line segment between two ordered points
class Line3d[N: Numeric]
	super Line[N]
	super ILine3d[N]
end

lib/geometry/points_and_lines.nit:17,1--209,3