r2

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: BSD-3-Clause Imports: 1 Imported by: 50

Documentation

Overview

Package r2 provides 2D vectors and boxes and operations on them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cos added in v0.8.0

func Cos(p, q Vec) float64

Cos returns the cosine of the opening angle between p and q.

func Cross added in v0.9.0

func Cross(p, q Vec) float64

Cross returns the cross product p×q.

func Dot added in v0.9.0

func Dot(p, q Vec) float64

Dot returns the dot product p·q.

func Norm added in v0.8.0

func Norm(p Vec) float64

Norm returns the Euclidean norm of p

|p| = sqrt(p_x^2 + p_y^2).

func Norm2 added in v0.8.0

func Norm2(p Vec) float64

Norm2 returns the Euclidean squared norm of p

|p|^2 = p_x^2 + p_y^2.

Types

type Box

type Box struct {
	Min, Max Vec
}

Box is a 2D bounding box. Well formed Boxes have Min components smaller than Max components.

func NewBox added in v0.12.0

func NewBox(x0, y0, x1, y1 float64) Box

NewBox is shorthand for Box{Min:Vec{x0,y0}, Max:Vec{x1,y1}}. The sides are swapped so that the resulting Box is well formed.

func (Box) Add added in v0.12.0

func (a Box) Add(v Vec) Box

Add adds v to the bounding box components. It is the equivalent of translating the Box by v.

func (Box) Canon added in v0.12.0

func (b Box) Canon() Box

Canon returns the canonical version of b. The returned Box has minimum and maximum coordinates swapped if necessary so that it is well-formed.

func (Box) Center added in v0.12.0

func (a Box) Center() Vec

Center returns the center of the Box.

func (Box) Contains added in v0.12.0

func (a Box) Contains(v Vec) bool

Contains returns true if v is contained within the bounds of the Box.

func (Box) Empty added in v0.12.0

func (a Box) Empty() bool

IsEmpty returns true if a Box's volume is zero or if a Min component is greater than its Max component.

func (Box) Scale added in v0.12.0

func (a Box) Scale(v Vec) Box

Scale returns a new Box scaled by a size vector around its center. The scaling is done element wise, which is to say the Box's X size is scaled by v.X. Negative components of v are interpreted as zero.

func (Box) Size added in v0.12.0

func (a Box) Size() Vec

Size returns the size of the Box.

func (Box) Union added in v0.12.0

func (a Box) Union(b Box) Box

Union returns a box enclosing both the receiver and argument Boxes.

func (Box) Vertices added in v0.12.0

func (a Box) Vertices() []Vec

Vertices returns a slice of the 4 vertices corresponding to each of the Box's corners.

The order of the vertices is CCW in the XY plane starting at the box minimum. If viewing box from +Z position the ordering is as follows:

  1. Bottom left.
  2. Bottom right.
  3. Top right.
  4. Top left.

type Rotation added in v0.9.0

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

Rotation describes a rotation in 2D.

func NewRotation added in v0.9.0

func NewRotation(alpha float64, p Vec) Rotation

NewRotation creates a rotation by alpha, around p.

func (Rotation) Rotate added in v0.9.0

func (r Rotation) Rotate(p Vec) Vec

Rotate returns p rotated according to the parameters used to construct the receiver.

type Triangle added in v0.12.0

type Triangle [3]Vec

Triangle represents a triangle in 2D space and is composed by 3 vectors corresponding to the position of each of the vertices.

func (Triangle) Area added in v0.12.0

func (t Triangle) Area() float64

Area returns the surface area of the triangle.

func (Triangle) Centroid added in v0.12.0

func (t Triangle) Centroid() Vec

Centroid returns the intersection of the three medians of the triangle as a point in space.

func (Triangle) IsDegenerate added in v0.12.0

func (t Triangle) IsDegenerate(tol float64) bool

IsDegenerate returns true if all of triangle's vertices are within tol distance of its longest side.

type Vec

type Vec struct {
	X, Y float64
}

Vec is a 2D vector.

func Add added in v0.9.0

func Add(p, q Vec) Vec

Add returns the vector sum of p and q.

func Rotate added in v0.9.0

func Rotate(p Vec, alpha float64, q Vec) Vec

Rotate returns a new vector, rotated by alpha around the provided point, q.

func Scale added in v0.9.0

func Scale(f float64, p Vec) Vec

Scale returns the vector p scaled by f.

func Sub added in v0.9.0

func Sub(p, q Vec) Vec

Sub returns the vector sum of p and -q.

func Unit added in v0.8.0

func Unit(p Vec) Vec

Unit returns the unit vector colinear to p. Unit returns {NaN,NaN} for the zero vector.

Jump to

Keyboard shortcuts

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