geom

package
v0.0.0-...-2c3a8a6 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package geom defines a two-dimensional coordinate system.

Index

Constants

View Source
const (
	ME_A  TransformMatrixElement = 0
	ME_B                         = 3
	ME_C                         = 1
	ME_D                         = 4
	ME_TX                        = 2
	ME_TY                        = 5
)

Variables

This section is empty.

Functions

func Centroid

func Centroid(points ...[2]float64) (x, y float64)

func Distance

func Distance(x1, y1, x2, y2 float64) float64

Distance calculates the distance between the points [x1 y1] and [x2 y2].

func DistanceSq

func DistanceSq(x1, y1, x2, y2 float64) float64

DistanceSq calculates the squared distance between the points [x1 y1] and [x2 y2].

Types

type Circle

type Circle struct {
	X, Y, Radius float64
}

Circle cannot be transformed.

func (Circle) Area

func (c Circle) Area() float64

func (Circle) GetX

func (c Circle) GetX() float64

func (Circle) GetY

func (c Circle) GetY() float64

func (*Circle) SetX

func (c *Circle) SetX(x float64)

func (*Circle) SetY

func (c *Circle) SetY(y float64)

type Ellipse

type Ellipse struct {
	X, Y             float64
	RadiusX, RadiusY float64
}

Ellipse cannot be transformed. https://g6auc.me.uk/ellipses/index.html

func (Ellipse) Area

func (e Ellipse) Area() float64

func (Ellipse) GetX

func (e Ellipse) GetX() float64

func (Ellipse) GetY

func (e Ellipse) GetY() float64

func (*Ellipse) SetX

func (e *Ellipse) SetX(x float64)

func (*Ellipse) SetY

func (e *Ellipse) SetY(y float64)

type Matrix

type Matrix [9]float64

Matrix is a 3x3 matrix of float64 values.

func IdentityMatrix

func IdentityMatrix() Matrix

IdentityMatrix creates a Matrix with values that cause no transformation to the target.

func RotationMatrix

func RotationMatrix(radians float64) Matrix

RotationMatrix creates a Matrix which rotates the target by an angle, measured in radians. https://en.wikipedia.org/wiki/Rotation_matrix

func ScaleMatrix

func ScaleMatrix(x, y float64) Matrix

func SkewMatrix

func SkewMatrix(x, y float64) Matrix

SkewMatrix creates a Matrix which progressively slides the target in a direction parallel to the x or y axis.

func TransformMatrix

func TransformMatrix(a, b, c, d, tx, ty float64) Matrix

TransformMatrix creates a Matrix with the following elements:

[ a, c, tx,
  b, d, ty,
  u, v, w ]

The elements A and D affect the positioning of pixels along the x and y axis when scaling or rotating. B and C are the elements that affect the positioning of pixels along the x and y axis when rotating or skewing. TX and TY are the distances by which to translate each point along the x and y axis. The Matrix operates in 2D space so it always assumes that the (last three) elements u and v are 0.0, and w is 1.0. https://en.wikipedia.org/wiki/Transformation_matrix https://www.tutorialspoint.com/computer_graphics/2d_transformation.htm

type Point

type Point struct {
	X, Y float64
}

func PointFromInt

func PointFromInt(x, y int32) *Point

PointFromInt creates a new Point from int32 values.

func PointFromXY

func PointFromXY(xy XYGetter) *Point

PointFromXY creates a new Point from a XYGetter.

func (Point) GetX

func (p Point) GetX() float64

func (Point) GetY

func (p Point) GetY() float64

func (Point) InFRect

func (p Point) InFRect(r sdl.FRect) bool

InFRect returns a bool indicating whether the X and Y values of Point are within the sdl.FRect.

func (Point) InRect

func (p Point) InRect(r sdl.Rect) bool

InRect returns a bool indicating whether the X and Y values of Point are within the sdl.Rect.

func (*Point) SetX

func (p *Point) SetX(x float64)

func (*Point) SetY

func (p *Point) SetY(y float64)

func (Point) Vector

func (p Point) Vector() Vector

Vector returns a Vector with the same X and Y values as Point.

type Polygon

type Polygon struct {
	// X and Y indicate the absolute position of the Polygon.
	X, Y float64
	// contains filtered or unexported fields
}

https://en.wikipedia.org/wiki/Simple_polygon https://en.wikipedia.org/wiki/Polygon

func NewPolygon

func NewPolygon(x, y float64, pts []Point) *Polygon

func NewQuad

func NewQuad(x, y, w, h float64) *Polygon

NewQuad creates a new polygon where all angles are 90 degrees. It differs from a Rect in the fact it can be rotated, scaled and skewed using a Matrix.

func NewRegularPolygon

func NewRegularPolygon(x, y float64, cr float64, n uint8) *Polygon

NewRegularPolygon creates a new regular polygon where all angles are equal in measure and all sides have the same length. Value cr is the circumradius between the center of the Polygon and any vertex (edge point). Value n indicates the amount of vertices (edges) the polygon has, 3 for a triangle, 4 for a quad etc. This value cannot be lower than 3. See https://en.wikipedia.org/wiki/Regular_polygon for additional information about regular polygons.

func NewTrigon

func NewTrigon(x, y, w, h float64) *Polygon

NewTrigon creates a new Polygon with 3 sides.

func (*Polygon) AbsoluteOrigin

func (p *Polygon) AbsoluteOrigin() Point

func (*Polygon) GetX

func (p *Polygon) GetX() float64

func (*Polygon) GetY

func (p *Polygon) GetY() float64

func (*Polygon) Model

func (p *Polygon) Model() []Point

func (*Polygon) Origin

func (p *Polygon) Origin() *Point

func (*Polygon) SetX

func (p *Polygon) SetX(x float64)

func (*Polygon) SetY

func (p *Polygon) SetY(y float64)

func (*Polygon) Transform

func (p *Polygon) Transform(matrix Matrix)

func (*Polygon) Vertices

func (p *Polygon) Vertices() []Point

Vertices returns the edges (vertices or vertexes) of the Polygon.

type PolygonShape

type PolygonShape interface {
	Shape
	Model() []Point
	Vertices() []Point
}

type Rect

type Rect struct {
	X, Y,
	W, H float64
}

A Rect is an axis-aligned rectangle where X Y is its center point. It cannot be transformed using a Transform or Matrix. It can however change position and change its width and height. Use NewQuad instead to create a four-sided polygon if you need a rectangular shape that needs to be rotated, sheared or transformed in any other way.

func (Rect) Area

func (r Rect) Area() float64

func (Rect) GetX

func (r Rect) GetX() float64

func (Rect) GetY

func (r Rect) GetY() float64

func (Rect) Rect

func (r Rect) Rect() *sdl.Rect

func (*Rect) SetX

func (r *Rect) SetX(x float64)

func (*Rect) SetY

func (r *Rect) SetY(y float64)

type Shape

type Shape interface {
	XY
	Area() float64
}

type Transform

type Transform struct {
	Rotation,
	ScaleX, ScaleY,
	SkewX, SkewY,
	TranslateX, TranslateY float64
}

Transform calculates various transformations and merges them to a Matrix which can be applied to a Transformable.

func (Transform) Matrix

func (t Transform) Matrix() Matrix

func (*Transform) Reset

func (t *Transform) Reset()

type TransformMatrixElement

type TransformMatrixElement int

type Transformable

type Transformable interface {
	Origin() *Point
	Transform(matrix Matrix)
}

A Transformable is any shape that can be transformed using a transform Matrix.

type Transformer

type Transformer interface {
	Reset()
	Matrix() Matrix
}

type Vector

type Vector struct {
	X, Y float64
}

func VectorFromInt

func VectorFromInt(x, y int32) *Vector

VectorFromInt creates a new Vector from int32 values.

func VectorFromXY

func VectorFromXY(xy XYGetter) *Vector

VectorFromXY creates a new Vector from an XYGetter.

func (*Vector) Add

func (v *Vector) Add(x, y float64) *Vector

func (*Vector) AddVec

func (v *Vector) AddVec(add Vector) *Vector

AddVec adds the given Vector to this Vector.

func (*Vector) AddXY

func (v *Vector) AddXY(add XYGetter) *Vector

AddXY the given XYGetter values to this Vector.

func (Vector) Angle

func (v Vector) Angle() float64

Angle returns the angle in radians.

func (Vector) Clone

func (v Vector) Clone() *Vector

Clone returns a pointer to a new Vector with the same X and Y values as the source Vector.

func (Vector) Cross

func (v Vector) Cross(target Vector) float64

Cross calculates the cross product of the Vector and the target Vector.

func (Vector) Dist

func (v Vector) Dist(target Vector) float64

Dist calculates the distance between the Vector and the target Vector.

func (Vector) DistSq

func (v Vector) DistSq(target Vector) float64

DistSq calculates the squared distance between the Vector and the target Vector.

func (*Vector) Div

func (v *Vector) Div(div Vector) *Vector

Div divides this Vector by the given Vector.

func (Vector) Dot

func (v Vector) Dot(target Vector) float64

Dot calculates the dot product of the Vector and the target Vector.

func (Vector) Equals

func (v Vector) Equals(target Vector) bool

Equals compares the X and Y values of the Vector and the target Vector, and returns true when they are equal.

func (*Vector) FromPolar

func (v *Vector) FromPolar(angle, length float64) *Vector

FromPolar sets the X and Y values according to the provided angle (in radians) and length.

func (Vector) GetX

func (v Vector) GetX() float64

func (Vector) GetY

func (v Vector) GetY() float64

func (Vector) Length

func (v Vector) Length() float64

Length returns the length (magnitude).

func (Vector) LengthSq

func (v Vector) LengthSq() float64

Length returns the squared length (magnitude).

func (*Vector) Lerp

func (v *Vector) Lerp(target Vector, t float64) *Vector

Lerp linearly interpolates this Vector towards the target Vector. Value t is the interpolation percentage between 0 and 1.

func (*Vector) LerpRound

func (v *Vector) LerpRound(target Vector, t, e float64) *Vector

func (*Vector) Limit

func (v *Vector) Limit(length float64) *Vector

Limit the length of this Vector.

func (*Vector) MulVec

func (v *Vector) MulVec(mul Vector) *Vector

MulVec multiplies this Vector by the given Vector.

func (*Vector) Negate

func (v *Vector) Negate() *Vector

Negate the X and Y values of this Vector, meaning negative numbers becoming positive and positive becoming negative.

func (Vector) Normalize

func (v Vector) Normalize() Vector

Norm normalizes the Vector by making the length a magnitude of 1 in the same direction. It does not alter the source Vector.

func (Vector) Point

func (v Vector) Point() Point

Point returns a Point with the same X and Y values as the Vector.

func (*Vector) Rotate

func (v *Vector) Rotate(angle float64) *Vector

Rotate this Vector by an angle amount (in radians).

func (*Vector) Scale

func (v *Vector) Scale(scale float64) *Vector

Scale this Vector by the given scale value, where 1 is equal to the Vector's current value.

func (*Vector) SetAngle

func (v *Vector) SetAngle(angle float64) *Vector

SetAngle in radians.

func (*Vector) SetLength

func (v *Vector) SetLength(l float64) *Vector

func (*Vector) SetX

func (v *Vector) SetX(x float64)

func (*Vector) SetY

func (v *Vector) SetY(y float64)

func (Vector) String

func (v Vector) String() string

func (*Vector) SubVec

func (v *Vector) SubVec(sub Vector) *Vector

SubVec subtracts the given Vector from this Vector.

func (*Vector) SubXY

func (v *Vector) SubXY(sub XYGetter) *Vector

SubXY subtracts the given XYGetter values from this Vector.

func (*Vector) Zero

func (v *Vector) Zero() *Vector

Zero sets this Vector to 0 values.

type XY

type XY interface {
	XYGetter
	XYSetter
}

type XYGetter

type XYGetter interface {
	GetX() float64
	GetY() float64
}

type XYSetter

type XYSetter interface {
	SetX(x float64)
	SetY(y float64)
}

Directories

Path Synopsis
Package xform supplies additional transform related features.
Package xform supplies additional transform related features.

Jump to

Keyboard shortcuts

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