r2

package
v0.0.0-...-d5a0809 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package r2 implements operations relating to objects in R2.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddAngles

func AddAngles(a1 float64, a2 float64) float64

AddAngles adds two angles in radians. The inputs are assumed to be in the range of +Pi to -Pi radians. The range of the result is +Pi to -Pi radians

Types

type Box

type Box struct {

	// A defines the top-left corner of the box
	A Vec2

	// S defines the size of the box
	S Vec2
}

Box defines a box in R2

            A
            |
            |
            |
            v
(1) A.X,A.Y +------+ A.X+S.X,A.Y (2)
            |\     |
            | \    |
            |  \   |
            |   \S |
            |    \ |
            |     \|

(3)A.X,A.Y+S.Y +------+ A.X+S.X,A.Y+S.Y (4)

func BoundingBox

func BoundingBox(points []Vec2) Box

BoundingBox creates a minimum axis-aligned bounding box for the given list of points.

func MakeBox

func MakeBox(a, s Vec2) Box

MakeBox creates an r2 Box

func (Box) Area

func (b Box) Area() float64

Area returns the area of the Box

func (Box) Bottom

func (b Box) Bottom() Line

Bottom returns the bottom face of the box.

func (Box) Center

func (b Box) Center() Vec2

Center returns the center of the Box as an r2 vector

func (Box) Contains

func (b Box) Contains(v Vec2) bool

Contains returns true if the point v is within the box b.

func (Box) FindPerimeterPointNearestContainedPoint

func (b Box) FindPerimeterPointNearestContainedPoint(containedPoint Vec2) Vec2

FindPerimeterPointNearestContainedPoint returns the perimiter point closest to the contained point. If the point is not actually within the Box, it returns a (0,0) vector

func (Box) GetCorner1

func (b Box) GetCorner1() Vec2

GetCorner1 returns the top left corner of the box

func (Box) GetCorner2

func (b Box) GetCorner2() Vec2

GetCorner2 returns the top right corner of the box

func (Box) GetCorner3

func (b Box) GetCorner3() Vec2

GetCorner3 returns the bottom left corner of the box.

func (Box) GetCorner4

func (b Box) GetCorner4() Vec2

GetCorner4 returns the bottom right corner of the box.

func (Box) Height

func (b Box) Height() float64

Height returns the height of the box

func (Box) Intersect

func (b Box) Intersect(l Line) (Vec2, bool)

Intersect returns the intersection of the box and the line, and a Boolean indicating if the box and vector intersect. If they do not collide, the zero vector is returned.

func (Box) Left

func (b Box) Left() Line

Left returns the left face of the box.

func (Box) Right

func (b Box) Right() Line

Right returns the right face of the box.

func (Box) Top

func (b Box) Top() Line

Top returns the top face of the box.

func (Box) Width

func (b Box) Width() float64

Width returns the width of the Box

type Line

type Line struct {
	// A defines the basis point of the line
	A Vec2

	// S defines the direction and length of the line
	S Vec2
}

Line describes a line in R2

(1) A.X,A.Y  +
              \
               \
                \
                 \
                  + A.X+S.X,A.Y+S.Y (2)

func MakeLine

func MakeLine(a, s Vec2) Line

MakeLine crates an r2 Line

func MakeLineFromEndpoints

func MakeLineFromEndpoints(a, b Vec2) Line

MakeLineFromEndpoints returns a line which has endpoints a, b

func (Line) Endpoint1

func (l Line) Endpoint1() Vec2

Endpoint1 returns the first endpoint of the line

func (Line) Endpoint2

func (l Line) Endpoint2() Vec2

Endpoint2 returns the second endpoint of the line

func (Line) Length

func (l Line) Length() float64

Length returns the length of the line

type Vec2

type Vec2 struct {
	// X magnitude of the vector
	X float64

	// Y magnitude of the vector
	Y float64
}

Vec2 implements a vector in R2

func IntersectLines

func IntersectLines(l1, l2 Line) (Vec2, bool)

IntersectLines This code is transliterated from here:

https://github.com/JulNadeauCA/libagar/blob/master/gui/primitive.co

Which is in turn based on Gem I.2 in Graphics Gems II by James Arvo.

func MakeVec2

func MakeVec2(x, y float64) Vec2

MakeVec2 creates a new vector inline

func V2

func V2(x, y float64) Vec2

V2 is a shortcut for MakeVec2

func (Vec2) Add

func (v Vec2) Add(u Vec2) Vec2

Add returns the sum of vector v and u

func (Vec2) Angle

func (v Vec2) Angle() float64

Angle computes the angle of the vector respect to the origin. The result is in radians.

func (Vec2) Dot

func (v Vec2) Dot(u Vec2) float64

Dot returns the dot product of vector v and u

func (Vec2) Length

func (v Vec2) Length() float64

Length return the vector length

func (Vec2) Project

func (v Vec2) Project(u Vec2) Vec2

Project returns the vector projection of v onto u

func (Vec2) Scale

func (v Vec2) Scale(s float64) Vec2

Scale returns the vector v scaled by the scalar s

func (Vec2) ScaleToLength

func (v Vec2) ScaleToLength(l float64) Vec2

ScaleToLength keeps the vector direction, but updates the length

func (Vec2) Unit

func (v Vec2) Unit() Vec2

Unit returns the vector scaled to length 1

Jump to

Keyboard shortcuts

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