Provides interfaces and classes to represent basic geometry needs.

Introduced classes

class Box[N: Numeric]

geometry :: Box

A 2d bounded object and an implementation of Boxed
class Box3d[N: Numeric]

geometry :: Box3d

A 3d bounded object and an implementation of Boxed
interface Boxed[N: Numeric]

geometry :: Boxed

An 2d abstract bounded object
interface Boxed3d[N: Numeric]

geometry :: Boxed3d

An 3d abstract bounded object
class BoxedArray[E: Boxed[Numeric]]

geometry :: BoxedArray

BoxedCollection implemented by an array
interface BoxedCollection[E: Boxed[Numeric]]

geometry :: BoxedCollection

Base for all data structures containing multiple Boxed Objects

Redefined classes

redef interface ILine[N: Numeric]

geometry :: boxes $ ILine

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

geometry :: boxes $ ILine3d

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

geometry :: boxes $ IPoint

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

geometry :: boxes $ IPoint3d

Abstract 3d point, strongly linked to its implementation Point3d

All class definitions

class Box[N: Numeric]

geometry $ Box

A 2d bounded object and an implementation of Boxed
class Box3d[N: Numeric]

geometry $ Box3d

A 3d bounded object and an implementation of Boxed
interface Boxed[N: Numeric]

geometry $ Boxed

An 2d abstract bounded object
interface Boxed3d[N: Numeric]

geometry $ Boxed3d

An 3d abstract bounded object
class BoxedArray[E: Boxed[Numeric]]

geometry $ BoxedArray

BoxedCollection implemented by an array
interface BoxedCollection[E: Boxed[Numeric]]

geometry $ BoxedCollection

Base for all data structures containing multiple Boxed Objects
redef interface ILine[N: Numeric]

geometry :: boxes $ ILine

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

geometry :: boxes $ ILine3d

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

geometry :: boxes $ IPoint

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

geometry :: boxes $ IPoint3d

Abstract 3d point, strongly linked to its implementation Point3d
package_diagram geometry::boxes boxes geometry::points_and_lines points_and_lines geometry::boxes->geometry::points_and_lines serialization serialization geometry::points_and_lines->serialization ...serialization ... ...serialization->serialization geometry::geometry geometry geometry::geometry->geometry::boxes geometry::quadtree quadtree geometry::quadtree->geometry::boxes gamnit::cameras cameras gamnit::cameras->geometry::geometry gamnit::cameras... ... gamnit::cameras...->gamnit::cameras a_star-m a_star-m a_star-m->geometry::quadtree 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

serialization :: serialization

General serialization services
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 points_and_lines

geometry :: points_and_lines

Interfaces and classes to represent basic geometry needs.

Children

module geometry

geometry :: geometry

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

geometry :: quadtree

QuadTree API mostly used for 2 dimensional collision detection

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 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 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
# Provides interfaces and classes to represent basic geometry needs.
module boxes

import points_and_lines

# An 2d abstract bounded object
interface Boxed[N: Numeric]
	# Left bound
	#
	# require: left <= right
	fun left: N is abstract

	# Right bound
	#
	# require: right >= left
	fun right: N is abstract

	# Top bound
	#
	# require: top >= bottom
	fun top: N is abstract

	# Bottom bound
	#
	# require: bottom <= top
	fun bottom: N is abstract

	# Is `other` contained within `self`?
	#
	#     var a = new Box[Int].lbwh(1, 1, 4, 4)
	#     var b = new Box[Int].lbwh(2, 2, 2, 2)
	#     var c = new Box[Int].lbwh(0, 2, 8, 2)
	#     assert a.contains(b)
	#     assert not b.contains(a)
	#     assert c.contains(b)
	#     assert not b.contains(c)
	#     assert not a.contains(c)
	#     assert not c.contains(a)
	fun contains(other: Boxed[N]): Bool
	do
		return self.top >= other.top and self.bottom <= other.bottom and
			self.left <= other.left and self.right >= other.right
	end

	# Does `self` intersect with `other`?
	#
	#     var a = new Box[Int].lbwh(0, 0, 2, 2)
	#     var b = new Box[Int].lbwh(1, 1, 8, 2)
	#     var c = new Box[Int].lbwh(3, 0, 2, 8)
	#     assert a.intersects(b)
	#     assert b.intersects(a)
	#     assert b.intersects(c)
	#     assert c.intersects(b)
	#     assert not c.intersects(a)
	#     assert not a.intersects(c)
	fun intersects(other: Boxed[N]): Bool
	do
		return self.left <= other.right and other.left <= self.right and
			self.top >= other.bottom and other.top >= self.bottom
	end

	# Create a bounding box that encloses the actual bounding box.
	# `dist` is the distance between the inner boundaries and the outer boundaries.
	# ~~~
	# var p = new Point[Int](5,10)
	# var b = p.padded(3)
	# assert b.left == 2 and b.right == 8 and b.top == 13 and b.bottom == 7
	# ~~~
	fun padded(dist: N): Box[N] do return new Box[N].lrtb(left - dist, right + dist, top + dist, bottom - dist)

	redef fun to_s do return "<{class_name} left: {left}, right: {right}, top: {top}, bottom: {bottom}>"
end

# A 2d bounded object and an implementation of `Boxed`
#
# This class offers many constructors specialized for different usage. They are
# named according to the order of their arguments.
class Box[N: Numeric]
	super Boxed[N]

	redef var left: N
	redef var right: N
	redef var top: N
	redef var bottom: N

	# Create a `Box` covering all of the `boxed`
	#
	#     var box = new Box[Int].around(new Point[Int](-4,-4), new Point[Int](4,4))
	#     assert box.left == -4 and box.bottom == -4
	#     assert box.right == 4 and box.top == 4
	init around(boxed: Boxed[N]...)
	do
		assert not boxed.is_empty

		var left: nullable N = null
		var right: nullable N = null
		var top: nullable N = null
		var bottom: nullable N = null

		for box in boxed do
			if left == null or box.left < left then left = box.left
			if right == null or box.right > right then right = box.right
			if top == null or box.top > top then top = box.top
			if bottom == null or box.bottom < bottom then bottom = box.bottom
		end

		assert left != null and right != null and top != null and bottom != null

		init(left, right, top, bottom)
	end

	# Create a `Box` using left, right, bottom and top
	init lrbt(left, right, bottom, top: N)
	do
		init(left, right, top, bottom)
	end

	# Create a `Box` using left, right, top and bottom
	init lrtb(left, right, top, bottom: N)
	do
		init(left, right, top, bottom)
	end

	# Create a `Box` using left, bottom, width and height
	init lbwh(left, bottom, width, height: N)
	do
		init(left, left + width, bottom + height, bottom)
	end

	# Create a `Box` using left, top, width and height
	init ltwh(left, top, width, height: N)
	do
		init(left, left+width, top, top - height)
	end
end

# An 3d abstract bounded object
interface Boxed3d[N: Numeric]
	super Boxed[N]

	# Front bound
	#
	# require: front >= back
	fun front: N is abstract

	# Back bound
	#
	# require: back <= bottom
	fun back: N is abstract

	#     var a = new Box3d[Int].lbfwhd(1, 1, -1, 4, 4, 4)
	#     var b = new Box3d[Int].lbfwhd(2, 2, -2, 2, 2, 2)
	#     var c = new Box3d[Int].lbfwhd(2, 2,  0, 2, 2, 8)
	#     assert a.contains(b)
	#     assert not b.contains(a)
	#     assert c.contains(b)
	#     assert not b.contains(c)
	#     assert not a.contains(c)
	#     assert not c.contains(a)
	redef fun contains(other)
	do
		return super and (not other isa Boxed3d[N] or
			(self.front >= other.front and self.back <= other.back))
	end

	#     var a = new Box3d[Int].lbfwhd(0, 0, 0, 2, 2, 2)
	#     var b = new Box3d[Int].lbfwhd(1, 1, 1, 8, 2, 2)
	#     var c = new Box3d[Int].lbfwhd(3, 0, 0, 2, 2, 8)
	#     assert a.intersects(b)
	#     assert b.intersects(a)
	#     assert b.intersects(c)
	#     assert c.intersects(b)
	#     assert not c.intersects(a)
	#     assert not a.intersects(c)
	redef fun intersects(other)
	do
		return super and (not other isa Boxed3d[N] or
			(self.back <= other.front and other.back <= self.front))
	end

	redef fun padded(dist): Box3d[N] do return new Box3d[N].lrtbfb(left - dist, right + dist, top + dist, bottom - dist, front + dist, back - dist)

	redef fun to_s do return "<{class_name} left: {left}, right: {right}, top: {top}, bottom: {bottom}, front: {front}, back: {back}>"
end

# A 3d bounded object and an implementation of Boxed
#
# This class offers many constructors specialized for different usage. They are
# named according to the order of their arguments.
class Box3d[N: Numeric]
	super Boxed3d[N]
	super Box[N]

	redef var front: N
	redef var back: N

	# Create a `Box` covering all of the `boxed`
	#
	#     var box = new Box[Int].around(new Point[Int](-4,-4), new Point[Int](4,4))
	#     assert box.left == -4 and box.bottom == -4
	#     assert box.right == 4 and box.top == 4
	init around(boxed: Boxed3d[N]...)
	do
		super

		assert not boxed.is_empty

		var left: nullable N = null
		var right: nullable N = null
		var top: nullable N = null
		var bottom: nullable N = null
		var front: nullable N = null
		var back: nullable N= null

		for box in boxed do
			if left == null or box.left < left then left = box.left
			if right == null or box.right > right then right = box.right
			if top == null or box.top > top then top = box.top
			if bottom == null or box.bottom < bottom then bottom = box.bottom
			if front == null or box.front > front then front = box.front
			if back == null or box.back < back then back = box.back
		end

		assert left != null and right != null and top != null and bottom != null

		self.left = left
		self.right = right
		self.top = top
		self.bottom = bottom
	end

	# Create a `Box3d` using left, right, bottom, top, front and back
	init lrbtfb(left, right, bottom, top, front, back: N)
	do
		lrbt(left, right, bottom, top)

		self.front = front
		self.back = back
	end

	# Create a `Box3d` using left, right, top, bottom, front and back
	init lrtbfb(left, right, top, bottom, front, back: N)
	do
		lrtb(left, right, top, bottom)

		self.front = front
		self.back = back
	end

	# Create a `Box3d` using left, top, front, width, height and depth
	init lbfwhd(left, bottom, front, width, height, depth: N)
	do
		lbwh(left, bottom, width, height)

		self.front = front
		self.back = front - depth
	end

	# Create a `Box3d` using left, top, front, width, height and depth
	init ltfwhd(left, top, front, width, height, depth: N)
	do
		ltwh(left, top, width, height)

		self.front = front
		self.back = front - depth
	end
end

redef class IPoint[N]
	super Boxed[N]

	redef fun left do return x
	redef fun right do return x
	redef fun top do return y
	redef fun bottom do return y
end

redef class IPoint3d[N]
	super Boxed3d[N]

	redef fun front do return z
	redef fun back do return z
end

redef class ILine[N]
	super Boxed[N]

	redef fun left do return point_left.x
	redef fun right do return point_right.x
	redef fun top do return point_left.y.max(point_right.y)
	redef fun bottom do return point_left.y.min(point_right.y)
end

redef class ILine3d[N]
	super Boxed3d[N]

	redef fun front do return point_left.z.min(point_right.z)
	redef fun back do return point_left.z.max(point_right.z)
end

# Base for all data structures containing multiple Boxed Objects
interface BoxedCollection[E: Boxed[Numeric]]
	super SimpleCollection[E]

	# returns all the items overlapping with `region`
	fun items_overlapping(region :Boxed[Numeric]): SimpleCollection[E] is abstract
end

# `BoxedCollection` implemented by an array
#
# Linear performances for searching, but really fast creation and filling.
class BoxedArray[E: Boxed[Numeric]]
	super BoxedCollection[E]
	super Array[E]

	redef fun items_overlapping(item)
	do
		var arr = new Array[E]
		for i in self do
			if i.intersects(item) then arr.add(i)
		end
		return arr
	end
end
lib/geometry/boxes.nit:17,1--340,3