package r2

import ""

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



func Cos

func Cos(p, q Vec) float64

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

func Cross

func Cross(p, q Vec) float64

Cross returns the cross product p×q.

func Dot

func Dot(p, q Vec) float64

Dot returns the dot product p·q.

func Norm

func Norm(p Vec) float64

Norm returns the Euclidean norm of p

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

func Norm2

func Norm2(p Vec) float64

Norm2 returns the Euclidean squared norm of p

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


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

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

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

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

func (a Box) Center() Vec

Center returns the center of the Box.

func (Box) Contains

func (a Box) Contains(v Vec) bool

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

func (Box) Empty

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

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

func (a Box) Size() Vec

Size returns the size of the Box.

func (Box) Union

func (a Box) Union(b Box) Box

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

func (Box) Vertices

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

type Rotation struct {
	// contains filtered or unexported fields

Rotation describes a rotation in 2D.

func NewRotation

func NewRotation(alpha float64, p Vec) Rotation

NewRotation creates a rotation by alpha, around p.

func (Rotation) Rotate

func (r Rotation) Rotate(p Vec) Vec

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

type Triangle

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

func (t Triangle) Area() float64

Area returns the surface area of the triangle.

func (Triangle) Centroid

func (t Triangle) Centroid() Vec

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

func (Triangle) IsDegenerate

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

func Add(p, q Vec) Vec

Add returns the vector sum of p and q.

func Rotate

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

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

func Scale

func Scale(f float64, p Vec) Vec

Scale returns the vector p scaled by f.

func Sub

func Sub(p, q Vec) Vec

Sub returns the vector sum of p and -q.

func Unit

func Unit(p Vec) Vec

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

Source Files

box.go doc.go triangle.go vector.go

v0.14.0 (latest)
Aug 19, 2023
1 packages
Last checked
6 hours ago

Tools for package owners.