cirno

package module
v0.0.0-...-f13a757 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2021 License: MIT Imports: 5 Imported by: 4

README

Cirno GoDoc

An easy to use collision detection and resolution library written in Go programming language. The library is still in development and the API might change.

Installation

No C dependencies required to compile the library. Just use the following command to install it.

go get github.com/zergon321/cirno

Tutorial

The tutorial series explains how to use different API methods and data types of Cirno to handle everything related to collisions in games.

Examples

All the example programs using the library are located in examples directory. Pixel is required to run any of them. The most important demos are described below.

  • benchmark - a small demo application that creates 1000 rectangles, randomly moves and rotates them and serches for collisions between them. It clearly showcases quad tree in action;

  • contacts - a small demo application that showcases finding contact points between shape outlines;

  • raycast - a small demo application that showcases raycast, just like Physics2D.Raycast from Unity;

  • sliding - a small demo that showcases shapes movement with sliding collision.

Games

The most noteable game created with Cirno at the current time is a Touhou style danmaku demo. The less noteable one is a tiny platrformer level in examples directory.

Danmaku Platformer
Danmaku Platformer

If you have a game using Cirno and you want it to be present in this list, just create a new PR with your changes to the README.

Features

  • Shapes to attach to game objects to detect and resolve collisions between them:
    • circle;
    • line segment (or just line);
    • rectangle (OBB, oriented bounding box).
  • Quadtree space index
  • Raycast
  • Contacts finding methods
  • Normal computing methods
  • Movement and rotation approximation
  • Tag system

Contributing

For minor and unimportant errors such as typos please just create issues instead of PRs fixing them.

Code changes adding features, optimizations, tests and bugfixes are welcome. All the contributions in examples directory should be really small, preferrably one-file. If your game is quite big, consider placing links to it in the list of games in the README.

Documentation

Index

Constants

View Source
const (
	// RadToDeg is a factor to transfrom radians to degrees.
	RadToDeg float64 = 180.0 / math.Pi
	// DegToRad is a factor to transform degrees to radians.
	DegToRad float64 = math.Pi / 180.0
	// Epsilon is the constant for approximate comparisons.
	Epsilon float64 = 0.000001
	// CollinearityThreshold is the constant to detect if two vectors
	// are effectively collinear.
	CollinearityThreshold float64 = 10.0
)

Variables

This section is empty.

Functions

func AdjustAngle

func AdjustAngle(angle float64) float64

AdjustAngle adjusts the value of the angle so it is bettween 0 and 360.

func Angle

func Angle(a, b Vector) (float64, error)

Angle returns the angle between two vectors (in degrees).

func AngleRadians

func AngleRadians(a, b Vector) (float64, error)

AngleRadians returns the angle between two vectors (in radians).

func Approximate

func Approximate(shape Shape, moveDiff Vector, turnDiff float64, shapes Shapes, intensity int, useTags bool) (Vector, float64, Shape, error)

Approximate attempts to move the shape in the specified direction to detect the closest point until the shape collides other shapes.

func CollisionCircleToCircle

func CollisionCircleToCircle(a, b *Circle) (bool, error)

CollisionCircleToCircle detects if there's an intersection between two circles.

func CollisionRectangleToCircle

func CollisionRectangleToCircle(rect *Rectangle, circle *Circle) (bool, error)

CollisionRectangleToCircle detects if there's an intersection between an oriented rectangle and a circle.

func CollisionRectangleToRectangle

func CollisionRectangleToRectangle(a, b *Rectangle) (bool, error)

CollisionRectangleToRectangle detects if there is an intersection between two oriented rectangles.

func Cross

func Cross(a, b Vector) float64

Cross returns the cross product of two vectors.

func Distance

func Distance(a, b Vector) float64

Distance returns the value of distance between two points represented as vectors.

func Dot

func Dot(a, b Vector) float64

Dot returns the dot product of two vectors.

func IntersectionLineToCircle

func IntersectionLineToCircle(line *Line, circle *Circle) (bool, error)

IntersectionLineToCircle detects if a line and a circle do intersect.

func IntersectionLineToLine

func IntersectionLineToLine(a, b *Line) (bool, error)

IntersectionLineToLine detects if two lines intersect.

func IntersectionLineToRectangle

func IntersectionLineToRectangle(line *Line, rect *Rectangle) (bool, error)

IntersectionLineToRectangle detects if there's an intersection between a line and a rectangle.

func LinesDistance

func LinesDistance(a, b *Line) (float64, error)

LinesDistance returns the shortest distance between two lines.

func ResolveCollision

func ResolveCollision(one, another Shape, useTags bool) (bool, error)

ResolveCollision assumes types of the given shapes and detects if they collide.

func Sign

func Sign(number float64) float64

Sign returns the sign of the number according to Epsilon.

func SquaredDistance

func SquaredDistance(a, b Vector) float64

SquaredDistance returns the value of distance in the power of 2 between two points represented as vectors.

Types

type Circle

type Circle struct {
	// contains filtered or unexported fields
}

Circle represents a geometric euclidian circle.

func NewCircle

func NewCircle(position Vector, radius float64) (*Circle, error)

NewCircle create a new circle with the given parameters.

func (*Circle) Angle

func (c *Circle) Angle() float64

Angle doesn't return any valuable data because there is no sense to rotate the circle.

This method is added just to match the Shape interface.

func (*Circle) AngleRadians

func (c *Circle) AngleRadians() float64

AngleRadians doesn't return any valuable data because there is no sense to rotate the circle.

This method is added just to match the Shape interface.

func (*Circle) Center

func (c *Circle) Center() Vector

Center returns the coordinates of the center of the circle.

func (*Circle) ContainsPoint

func (c *Circle) ContainsPoint(point Vector) bool

ContainsPoint detects if the given point is inside the circle.

func (*Circle) Data

func (data *Circle) Data() interface{}

Data returns whatever is saved in the data field of the shape.

func (Circle) GetIdentity

func (t Circle) GetIdentity() int32

GetIdentity returns the valye of the shape identity.

func (Circle) GetMask

func (t Circle) GetMask() int32

GetMask the value of the shape mask.

func (*Circle) Move

func (c *Circle) Move(direction Vector) Vector

Move moves the circle in the specified direction.

func (*Circle) NormalTo

func (circle *Circle) NormalTo(shape Shape) (Vector, error)

NormalTo returns the normal from the given circle to the other shape.

func (*Circle) NormalToCircle

func (circle *Circle) NormalToCircle(other *Circle) (Vector, error)

NormalToCircle returns the normal from the given circle to the other circle.

func (*Circle) NormalToLine

func (circle *Circle) NormalToLine(line *Line) (Vector, error)

NormalToLine returns the normal from the given circle to the line.

func (*Circle) NormalToRectangle

func (circle *Circle) NormalToRectangle(rect *Rectangle) (Vector, error)

NormalToRectangle returns the normal from the given circle to the rectangle.

func (*Circle) Radius

func (c *Circle) Radius() float64

Radius returns the radius og the circle.

func (*Circle) Rotate

func (c *Circle) Rotate(angle float64) float64

Rotate does nothing to the circle because there is no sense to rotate it.

This method is added just to match the Shape interface.

func (*Circle) RotateAround

func (c *Circle) RotateAround(angle float64, base Vector) Vector

RotateAround rotates the circle around the specified point changing the circle's position.

func (*Circle) RotateAroundRadians

func (c *Circle) RotateAroundRadians(angle float64, base Vector) Vector

RotateAroundRadians rotates the circle around the base point at the specified angle in radians.

func (*Circle) RotateRadians

func (c *Circle) RotateRadians(angle float64) float64

RotateRadians does nothing to the circle because there is no sense to rotate it.

This method is added just to match the Shape interface.

func (*Circle) SetAngle

func (c *Circle) SetAngle(angle float64) float64

SetAngle does nothing to the circle because there is no sense to rotate it.

This method is added just to match the Shape interface.

func (*Circle) SetAngleRadians

func (c *Circle) SetAngleRadians(angle float64) float64

SetAngleRadians does nothing to the circle because there is no sense to rotate it.

This method is added just to match the Shape interface.

func (*Circle) SetData

func (data *Circle) SetData(newData interface{})

SetData assigns new data to the shape.

func (*Circle) SetIdentity

func (t *Circle) SetIdentity(newIdentity int32)

SetIdentity assigns a new value to the tag identity.

func (*Circle) SetMask

func (t *Circle) SetMask(newMask int32)

SetMask assigns a new value to the tag mask.

func (*Circle) SetPosition

func (c *Circle) SetPosition(pos Vector) Vector

SetPosition sets the circle position to the given coordinates.

func (Circle) ShouldCollide

func (t Circle) ShouldCollide(other Shape) (bool, error)

ShouldCollide returns true if the shape should collide another one accodring to their tags.

func (*Circle) TypeName

func (c *Circle) TypeName() string

TypeName returns the name of the shape type.

type Line

type Line struct {
	// contains filtered or unexported fields
}

Line represents a geometric euclidian line segment from p to q.

func NewLine

func NewLine(p Vector, q Vector) (*Line, error)

NewLine returns a new line segment with the given parameters.

func (*Line) Angle

func (l *Line) Angle() float64

Angle returns the rotation angle of the line (un degrees).

func (*Line) AngleRadians

func (l *Line) AngleRadians() float64

AngleRadians returns the rotation angle of the line (un radians).

func (*Line) Center

func (l *Line) Center() Vector

Center returns the coordinates of the middle point between p and q.

func (*Line) CollinearTo

func (l *Line) CollinearTo(other *Line) (bool, error)

CollinearTo returns true if the lines are collinear, and false otherwise.

func (*Line) ContainsPoint

func (l *Line) ContainsPoint(point Vector) bool

ContainsPoint detects if the point lies on the line.

func (*Line) Data

func (data *Line) Data() interface{}

Data returns whatever is saved in the data field of the shape.

func (*Line) GetBoundingBox

func (l *Line) GetBoundingBox() (Vector, Vector)

GetBoundingBox returns the bounding box for the line.

func (Line) GetIdentity

func (t Line) GetIdentity() int32

GetIdentity returns the valye of the shape identity.

func (Line) GetMask

func (t Line) GetMask() int32

GetMask the value of the shape mask.

func (*Line) Length

func (l *Line) Length() float64

Length returns the length of the line.

func (*Line) Move

func (l *Line) Move(direction Vector) Vector

Move moves the line in the specified direction and returns its new position.

func (*Line) NormalTo

func (line *Line) NormalTo(shape Shape) (Vector, error)

NormalTo returns the normal from the given line to the other shape.

func (*Line) NormalToCircle

func (line *Line) NormalToCircle(circle *Circle) (Vector, error)

NormalToCircle returns the normal from the given line to the circle.

func (*Line) NormalToLine

func (line *Line) NormalToLine(other *Line) (Vector, error)

NormalToLine returns the normal from the given line to the other line.

func (*Line) NormalToRectangle

func (line *Line) NormalToRectangle(rect *Rectangle) (Vector, error)

NormalToRectangle returns the normal from the given line to the rectangle.

func (*Line) Orientation

func (l *Line) Orientation(point Vector) int

Orientation returns 0 if the point is collinear to the line, 1 if orientation is clockwise, -1 if orientation is counter-clockwise.

func (*Line) P

func (l *Line) P() Vector

P returns the starting point of the line.

func (*Line) ParallelTo

func (l *Line) ParallelTo(other *Line) (bool, error)

ParallelTo checks if two line segments are collinear but don't lie on the same line.

func (*Line) ProjectPoint

func (l *Line) ProjectPoint(point Vector) Vector

ProjectPoint returns the projection of the point onto the line.

func (*Line) Q

func (l *Line) Q() Vector

Q returns the ending point of the line.

func (*Line) Rotate

func (l *Line) Rotate(angle float64) float64

Rotate rotates the line at the specified angle (in degrees).

Returns the rotation angle of the line (in degrees).

func (*Line) RotateAround

func (l *Line) RotateAround(angle float64, base Vector) Vector

RotateAround rotates the line around the base point.

func (*Line) RotateAroundRadians

func (l *Line) RotateAroundRadians(angle float64, base Vector) Vector

RotateAroundRadians rotates the line around the base point at the angle in radians.

func (*Line) RotateRadians

func (l *Line) RotateRadians(angle float64) float64

RotateRadians rotates the line at the specified angle (in radians).

Returns the rotation angle of the line (in radians).

func (*Line) SameLineWith

func (l *Line) SameLineWith(other *Line) (bool, error)

SameLineWith returns true if two line segments lie on the same line.

func (*Line) SetAngle

func (l *Line) SetAngle(angle float64) float64

SetAngle sets the rotation angle of the line to the specified value (in degrees).

func (*Line) SetAngleRadians

func (l *Line) SetAngleRadians(angle float64) float64

SetAngleRadians sets the rotation angle of the line to the specified value (in radians).

func (*Line) SetData

func (data *Line) SetData(newData interface{})

SetData assigns new data to the shape.

func (*Line) SetIdentity

func (t *Line) SetIdentity(newIdentity int32)

SetIdentity assigns a new value to the tag identity.

func (*Line) SetMask

func (t *Line) SetMask(newMask int32)

SetMask assigns a new value to the tag mask.

func (*Line) SetPosition

func (l *Line) SetPosition(pos Vector) Vector

SetPosition sets the position of the line to the given coordinates.

func (Line) ShouldCollide

func (t Line) ShouldCollide(other Shape) (bool, error)

ShouldCollide returns true if the shape should collide another one accodring to their tags.

func (*Line) SquaredLength

func (l *Line) SquaredLength() float64

SquaredLength returns the length of the line in the power of 2.

func (*Line) TypeName

func (l *Line) TypeName() string

TypeName returns the name of the shape type.

type Rectangle

type Rectangle struct {
	// contains filtered or unexported fields
}

Rectangle represents an oriented euclidian rectangle.

func NewRectangle

func NewRectangle(position Vector, width, height, angle float64) (*Rectangle, error)

NewRectangle returns a new rectangle with specified parameters.

func (*Rectangle) Angle

func (r *Rectangle) Angle() float64

Angle returns the angle of the rectangle (in degrees).

func (*Rectangle) AngleRadians

func (r *Rectangle) AngleRadians() float64

AngleRadians returns the angle of the rectangle (in radians).

func (*Rectangle) Center

func (r *Rectangle) Center() Vector

Center returns the coordinates of the center of the rectangle.

func (*Rectangle) ContainsPoint

func (r *Rectangle) ContainsPoint(point Vector) bool

ContainsPoint detects if the given point is inside the rectangle.

func (*Rectangle) Data

func (data *Rectangle) Data() interface{}

Data returns whatever is saved in the data field of the shape.

func (Rectangle) GetIdentity

func (t Rectangle) GetIdentity() int32

GetIdentity returns the valye of the shape identity.

func (Rectangle) GetMask

func (t Rectangle) GetMask() int32

GetMask the value of the shape mask.

func (*Rectangle) Height

func (r *Rectangle) Height() float64

Height returns the height of the rectangle.

func (*Rectangle) Max

func (r *Rectangle) Max() Vector

Max returns the upper right point of the rectangle with no rotation.

func (*Rectangle) Min

func (r *Rectangle) Min() Vector

Min returns the lower left point of the rectangle with no rotation.

func (*Rectangle) Move

func (r *Rectangle) Move(direction Vector) Vector

Move moves the rectangle in the specified direction; returns its new position.

func (*Rectangle) NormalTo

func (rect *Rectangle) NormalTo(shape Shape) (Vector, error)

NormalTo returns the normal from the given rectangle to the other shape.

func (*Rectangle) NormalToCircle

func (rect *Rectangle) NormalToCircle(circle *Circle) (Vector, error)

NormalToCircle returns the normal from the given rectangle to the circle.

func (*Rectangle) NormalToLine

func (rect *Rectangle) NormalToLine(line *Line) (Vector, error)

NormalToLine returns the normal between the given rectangle and the line.

func (*Rectangle) NormalToRectangle

func (rect *Rectangle) NormalToRectangle(other *Rectangle) (Vector, error)

NormalToRectangle returns the normal from the given rectangle to the other rectangle.

func (*Rectangle) Rotate

func (r *Rectangle) Rotate(angle float64) float64

Rotate rotates the whole rectangle at the specified angle (in degrees).

Returns the new angle of the rectangle (in degrees).

func (*Rectangle) RotateAround

func (r *Rectangle) RotateAround(angle float64, base Vector) Vector

RotateAround rotates the rectangle around the specified base point.

func (*Rectangle) RotateAroundRadians

func (r *Rectangle) RotateAroundRadians(angle float64, base Vector) Vector

RotateAroundRadians rotates the rectangle around the specified base point at the angle in radians.

func (*Rectangle) RotateRadians

func (r *Rectangle) RotateRadians(angle float64) float64

RotateRadians rotates the whole rectangle at the specified angle (in radians).

Returns the new angle of the rectangle (in radians).

func (*Rectangle) SetAngle

func (r *Rectangle) SetAngle(angle float64) float64

SetAngle sets the angle of the rectangle to the given value (in degrees).

func (*Rectangle) SetAngleRadians

func (r *Rectangle) SetAngleRadians(angle float64) float64

SetAngleRadians sets the angle of the rectangle to the given value (in radians).

func (*Rectangle) SetData

func (data *Rectangle) SetData(newData interface{})

SetData assigns new data to the shape.

func (*Rectangle) SetIdentity

func (t *Rectangle) SetIdentity(newIdentity int32)

SetIdentity assigns a new value to the tag identity.

func (*Rectangle) SetMask

func (t *Rectangle) SetMask(newMask int32)

SetMask assigns a new value to the tag mask.

func (*Rectangle) SetPosition

func (r *Rectangle) SetPosition(pos Vector) Vector

SetPosition sets the position of the rectangle to the given coordinates.

func (Rectangle) ShouldCollide

func (t Rectangle) ShouldCollide(other Shape) (bool, error)

ShouldCollide returns true if the shape should collide another one accodring to their tags.

func (*Rectangle) TypeName

func (r *Rectangle) TypeName() string

TypeName returns the name of the shape type.

func (*Rectangle) Vertices

func (r *Rectangle) Vertices() [4]Vector

Vertices returns the array of the rectangle vertices.

func (*Rectangle) Width

func (r *Rectangle) Width() float64

Width returns the width of the rectangle.

type Shape

type Shape interface {
	// Common methods.
	TypeName() string
	Center() Vector
	Angle() float64
	AngleRadians() float64
	Move(Vector) Vector
	Rotate(float64) float64
	RotateRadians(float64) float64
	RotateAround(float64, Vector) Vector
	RotateAroundRadians(float64, Vector) Vector
	SetPosition(Vector) Vector
	SetAngle(float64) float64
	SetAngleRadians(float64) float64
	ContainsPoint(Vector) bool
	NormalTo(Shape) (Vector, error)

	// Tag-related methods.
	GetIdentity() int32
	SetIdentity(int32)
	GetMask() int32
	SetMask(int32)
	ShouldCollide(Shape) (bool, error)

	// Data-related methods.
	Data() interface{}
	SetData(data interface{})
	// contains filtered or unexported methods
}

Shape represents a shape in the space.

type Shapes

type Shapes map[Shape]none

Shapes represents a list of shapes.

func (Shapes) Contains

func (shapes Shapes) Contains(shape Shape) (bool, error)

Contains checks if the list of shapes contains the given shape.

func (Shapes) Copy

func (shapes Shapes) Copy() Shapes

Copy returns a new hash set with same shapes.

func (Shapes) FilterByCollision

func (shapes Shapes) FilterByCollision(shape Shape) (Shapes, error)

FilterByCollision returns all the shapes that should collide or get collided by the given shape.

func (Shapes) FilterByCollisionLeft

func (shapes Shapes) FilterByCollisionLeft(shape Shape) (Shapes, error)

FilterByCollisionLeft returns all the shapes that should collide the given shape.

func (Shapes) FilterByCollisionRight

func (shapes Shapes) FilterByCollisionRight(shape Shape) (Shapes, error)

FilterByCollisionRight returns all the shapes the given shape should collide.

func (Shapes) FilterByIdentity

func (shapes Shapes) FilterByIdentity(identity int32) Shapes

FilterByIdentity returns all the shapes matching the specified identity template.

func (Shapes) FilterByMask

func (shapes Shapes) FilterByMask(mask int32) Shapes

FilterByMask returns all the shapes matching the specified mask template.

func (Shapes) Insert

func (shapes Shapes) Insert(shapesToInsert ...Shape) error

Insert adds the given shape in the set of shapes.

func (Shapes) Items

func (shapes Shapes) Items() []Shape

Items returns the list all the shapes from the set.

func (Shapes) Merge

func (shapes Shapes) Merge(otherShapes Shapes) error

Merge adds all the shapes from the other set to the current set.

func (Shapes) Remove

func (shapes Shapes) Remove(shape Shape) error

Remove removes the specified shape from the set.

type Space

type Space struct {
	// contains filtered or unexported fields
}

Space represents a geometric space with shapes within it.

func NewSpace

func NewSpace(
	subdivisionFactor, shapesInArea int, width,
	height float64, min, max Vector, useTags bool,
) (
	*Space, error,
)

NewSpace creates a new empty space with the given parameters.

func (*Space) Add

func (space *Space) Add(shapes ...Shape) error

Add adds a new shape in the space.

func (*Space) AdjustShapePosition

func (space *Space) AdjustShapePosition(shape Shape) error

AdjustShapePosition changes the position of the shape if it's out of bounds.

func (*Space) Boxcast

func (space *Space) Boxcast(rect *Rectangle) (Shapes, error)

Boxcast casts a box in the space and returns all the shapes overlapped by this box.

func (*Space) Cells

func (space *Space) Cells() map[*Rectangle]Shapes

Cells returns all the cells the space is subdivided to.

func (*Space) Circlecast

func (space *Space) Circlecast(circle *Circle) (Shapes, error)

Circlecast casts a circle in the space and returns all the shapes overlapped by the circle.

func (*Space) Clear

func (space *Space) Clear() error

Clear removes all shapes from the space.

func (*Space) CollidedBy

func (space *Space) CollidedBy(shape Shape) (Shapes, error)

CollidedBy returns the set of shapes collided by the given shape.

func (*Space) CollidingShapes

func (space *Space) CollidingShapes() (map[Shape]Shapes, error)

CollidingShapes returns the dictionary where key is a shape and value is the set of shapes colliding with the key shape.

func (*Space) CollidingWith

func (space *Space) CollidingWith(shape Shape) (Shapes, error)

CollidingWith returns the set of shapes colliding with the given shape.

func (*Space) Contains

func (space *Space) Contains(shape Shape) (bool, error)

Contains returns true if the shape is within the space, and false otherwise.

func (*Space) InBounds

func (space *Space) InBounds(shape Shape) (bool, error)

InBounds detects if the center the given shape is within the space bounds.

func (*Space) Max

func (space *Space) Max() Vector

Max returns the max point of the space.

func (*Space) Min

func (space *Space) Min() Vector

Min returns the min point of the space.

func (*Space) Raycast

func (space *Space) Raycast(origin, direction Vector, distance float64, mask int32) (Shape, Vector, error)

Raycast casts a ray in the space and returns the hit shape closest to the origin of the ray.

Ray cannot hit against the shape within which it's located.

func (*Space) Rebuild

func (space *Space) Rebuild() error

Rebuild rebuilds the space's index of fhapes in purpose to optimize it.

func (*Space) Remove

func (space *Space) Remove(shapes ...Shape) error

Remove removes the shape from the space.

func (*Space) Shapes

func (space *Space) Shapes() Shapes

Shapes returns the list of all shapes within the space.

func (*Space) Update

func (space *Space) Update(shape Shape) (map[Vector]Shapes, error)

Update should be called on the shape whenever it's moved within the space.

func (*Space) UseTags

func (space *Space) UseTags() bool

UseTags indicates whether the space relies on tags for collision detection.

func (*Space) WouldBeCollidedBy

func (space *Space) WouldBeCollidedBy(shape Shape, moveDiff Vector, turnDiff float64) (Shapes, error)

WouldBeCollidedBy returns all the shapes that would be collided by the given shape if it moved in the specified direction.

func (*Space) WouldBeCollidingWith

func (space *Space) WouldBeCollidingWith(shape Shape, moveDiff Vector, turnDiff float64) (Shapes, error)

WouldBeCollidingWith returns all the shapes that would be colliding the given one if it moved in the specified direction.

type Vector

type Vector struct {
	X float64
	Y float64
}

Vector represents a 2-dimensional vector.

func Contact

func Contact(one, other Shape) ([]Vector, error)

Contact returns the contact points between two given shapes (if they exist).

func ContactCircleToCircle

func ContactCircleToCircle(one, other *Circle) ([]Vector, error)

ContactCircleToCircle returns the contact point between two circles (if it exists).

func ContactLineToCircle

func ContactLineToCircle(line *Line, circle *Circle) ([]Vector, error)

ContactLineToCircle returns the contact points between the line and the circle (if they exist).

func ContactLineToLine

func ContactLineToLine(one, other *Line) ([]Vector, error)

ContactLineToLine returns the contact point between two lines (if it exists).

func ContactLineToRectangle

func ContactLineToRectangle(line *Line, rect *Rectangle) ([]Vector, error)

ContactLineToRectangle returns the contacts between the line and the rectangle (if they exist).

func ContactRectangleToCircle

func ContactRectangleToCircle(rect *Rectangle, circle *Circle) ([]Vector, error)

ContactRectangleToCircle returns the contacts between the rectangle and the circle (if they exist).

func ContactRectangleToRectangle

func ContactRectangleToRectangle(one, other *Rectangle) ([]Vector, error)

ContactRectangleToRectangle returns the contacts between two rectangles (if they exist).

func Down

func Down() Vector

Down is the vector {0; -1}.

func Left

func Left() Vector

Left is the vector {-1; 0}.

func NewVector

func NewVector(x, y float64) Vector

NewVector returns a new vector with the given coordinates.

func Right() Vector

Right is the vector {1; 0}.

func Up

func Up() Vector

Up is the vector {0; 1}.

func Zero

func Zero() Vector

Zero is the vector {0; 0}.

func (Vector) Add

func (v Vector) Add(other Vector) Vector

Add returns the sum of two vectors.

func (Vector) ApproximatelyEqual

func (v Vector) ApproximatelyEqual(other Vector) bool

ApproximatelyEqual returns true if the vector is approximately equal to the other one, and false otherwise.

func (Vector) CollinearTo

func (v Vector) CollinearTo(other Vector) (bool, error)

CollinearTo indicates if the given vector is collinear to the other vector.

func (Vector) Magnitude

func (v Vector) Magnitude() float64

Magnitude returns the length of the vector.

func (Vector) MultiplyBy

func (v Vector) MultiplyBy(other Vector) Vector

MultiplyBy multiplies components of the first vector by the components of the other vector.

func (Vector) MultiplyByScalar

func (v Vector) MultiplyByScalar(scalar float64) Vector

MultiplyByScalar returns the vector multiplied by the specified scalar.

func (Vector) Normalize

func (v Vector) Normalize() (Vector, error)

Normalize returns a normalized vector with magnitude of 1.

func (Vector) PerpendicularClockwise

func (v Vector) PerpendicularClockwise() Vector

PerpendicularClockwise returns a new vector perpendicular to the given one and turned in a clockwise direction relatively to it.

func (Vector) PerpendicularCounterClockwise

func (v Vector) PerpendicularCounterClockwise() Vector

PerpendicularCounterClockwise returns a new vector perpendicular to the given one and turned in a clockwise direction relatively to it.

func (Vector) Project

func (v Vector) Project(axis Vector) Vector

Project returns vector v projected onto the axis.

func (Vector) Rotate

func (v Vector) Rotate(angle float64) Vector

Rotate returns the vector rotated at an angle of n degrees.

func (Vector) RotateAround

func (v Vector) RotateAround(angle float64, base Vector) Vector

RotateAround returns the vector rotated at an angle of n degrees around the base vector.

func (Vector) RotateAroundRadians

func (v Vector) RotateAroundRadians(angle float64, base Vector) Vector

RotateAroundRadians returns the vector totated at an angle of n radians around the base vector.

func (Vector) RotateRadians

func (v Vector) RotateRadians(angle float64) Vector

RotateRadians returns the vector totated at an angle of n radians.

func (Vector) SquaredMagnitude

func (v Vector) SquaredMagnitude() float64

SquaredMagnitude returns the vectors's magnitude in the power of 2.

func (Vector) Subtract

func (v Vector) Subtract(other Vector) Vector

Subtract returns the difference of two vectors.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL