geom

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	East = iota
	North
	West
	South
)

Cardinal directions, as used in the return from PolygonExtrema.

Variables

View Source
var IdentityMatrix3x4 = Matrix3x4{
	0: [4]float64{0: 1},
	1: [4]float64{1: 1},
	2: [4]float64{2: 1},
}

IdentityMatrix3x4 is the identity matrix for Matrix3x4.

View Source
var IdentityRatMatrix3 = RatMatrix3{
	0: [3]Rat{0: {1, 1}},
	1: [3]Rat{1: {1, 1}},
	2: [3]Rat{2: {1, 1}},
}

IdentityRatMatrix3 is the identity matrix for RatMatrix3.

Functions

func CDiv

func CDiv(p, q image.Point) image.Point

CDiv performs componentwise division of two image.Points.

func CFloat

func CFloat(p image.Point) (x, y float64)

CFloat returns the components of an image.Point as two floats.

func CMul

func CMul(p, q image.Point) image.Point

CMul performs componentwise multiplication of two image.Points.

func CSign

func CSign(p image.Point) image.Point

CSign applies Sign componentwise to the Point.

func Dot

func Dot(p, q image.Point) int

Dot returns the Dot product of two image.Points.

func FSign

func FSign(m float64) float64

FSign returns the sign of the float64 (-1, 0, or 1).

func PolygonContains

func PolygonContains(convex []image.Point, p image.Point) bool

PolygonContains reports if a convex polygon contains a point. The polygon must be in clockwise order if +Y is pointing upwards, or anticlockwise if +Y is pointing downwards.

func PolygonExtrema

func PolygonExtrema(polygon []image.Point) [4]image.Point

PolygonExtrema returns the most easterly, northerly, westerly, and southerly points (north is in the -Y direction, east is in the +X direction, etc). If there are multiple points furthest in any direction, the first one is used.

func PolygonRectOverlap

func PolygonRectOverlap(convex []image.Point, rect image.Rectangle) bool

PolygonRectOverlap reports if a convex polygon overlaps a rectangle.

func Project

func Project(π Projector, p Int3) image.Point

Project is shorthand for π.Project(p.Z).Add(p.XY()).

func Sign

func Sign(m int) int

Sign returns the sign of the int (-1, 0, or 1).

Types

type Box

type Box struct {
	Min, Max Int3
}

Box describes an axis-aligned rectangular prism.

func (Box) Add

func (b Box) Add(p Int3) Box

Add offsets the box by vector p.

func (Box) Back

func (b Box) Back(π Projector) image.Rectangle

Back returns an image.Rectangle representing the back of the box, using the given projection π.

func (Box) BoundingRect

func (b Box) BoundingRect(π Projector) image.Rectangle

BoundingRect returns an image.Rectangle that bounds the box if it were projected.

func (Box) Canon

func (b Box) Canon() Box

Canon returns a copy of b that is well-formed.

func (Box) Centre

func (b Box) Centre() Int3

Centre returns the centre point of the box.

func (Box) Empty

func (b Box) Empty() bool

Empty reports whether the box contains no points.

func (Box) Eq

func (b Box) Eq(c Box) bool

Eq reports whether b and c contain the same set of points. All empty boxes are considered equal.

func (Box) Front

func (b Box) Front(π Projector) image.Rectangle

Front returns an image.Rectangle representing the front of the box, using the given projection π.

func (Box) Overlaps

func (b Box) Overlaps(c Box) bool

Overlaps reports whether b and c have non-empty intersection.

func (Box) Size

func (b Box) Size() Int3

Size returns b's width, height, and depth.

func (Box) String

func (b Box) String() string

String returns a string representation of b like "(3,4,5)-(6,5,8)".

func (Box) Sub

func (b Box) Sub(p Int3) Box

Sub offsets the box by (-p).

func (Box) XY

func (b Box) XY() image.Rectangle

XY returns the image.Rectangle representing the box if we forgot about Z (a vertical cross-section).

func (Box) XZ

func (b Box) XZ() image.Rectangle

XZ returns the image.Rectangle representing the box if we forgot about Y (the horizontal cross-section).

type CubicSpline

type CubicSpline struct {
	Points []Float2

	// If false, CubicSpline defines a natural cubic spline (the slopes at the
	// endpoints are "free" and the moments at the ends are zero.)
	// If true, Preslope (or Postslope, or both) is used to set the slope.
	FixedPreslope, FixedPostslope bool

	// Slope of line before and after spline, for extrapolation.
	// If a natural cubic spline is being used, these are set by Prepare.
	// If instead FixedPreslope or FixedPostslope are true, these are read by
	// Prepare to determine the moments.
	Preslope, Postslope float64
	// contains filtered or unexported fields
}

CubicSpline implements a cubic spline. A cubic spline interpolates the given Points with cubic polynomials, ensuring first and second derivatives along the whole spline are continuous.

func (*CubicSpline) Interpolate

func (s *CubicSpline) Interpolate(x float64) float64

Interpolate returns y where (x,y) is a point on the spline. If x is outside the spline, it extrapolates from the first or last point together with s.Preslope or s.Postslope.

func (*CubicSpline) Prepare

func (s *CubicSpline) Prepare() error

Prepare sorts the points and computes internal information.

type ElevationProjection

type ElevationProjection struct{}

ElevationProjection throws away Z.

func (ElevationProjection) Project

Project returns the zero point.

func (ElevationProjection) Sign

Sign returns the zero point.

type Float2

type Float2 struct {
	X, Y float64
}

Float2 is an element of float64^2

func (Float2) Add

func (p Float2) Add(q Float2) Float2

Add performs vector addition.

func (Float2) CDiv

func (p Float2) CDiv(q Float2) Float2

CDiv performs componentwise division.

func (Float2) CMul

func (p Float2) CMul(q Float2) Float2

CMul performs componentwise multiplication.

func (Float2) Coord

func (p Float2) Coord() (x, y float64)

Coord returns the components of the vector.

func (Float2) Div

func (p Float2) Div(k float64) Float2

Div performs scalar division by k.

func (Float2) Dot

func (p Float2) Dot(q Float2) float64

Dot returns the dot product of the two vectors.

func (Float2) Mul

func (p Float2) Mul(k float64) Float2

Mul performs scalar multiplication.

func (Float2) Neg

func (p Float2) Neg() Float2

Neg returns the vector pointing in the opposite direction.

func (Float2) Sign

func (p Float2) Sign() Float2

Sign returns a sign vector.

func (Float2) String

func (p Float2) String() string

String returns a string representation of p like "(3.0,4.0,5.0)".

func (Float2) Sub

func (p Float2) Sub(q Float2) Float2

Sub performs vector subtraction.

type Float3

type Float3 struct {
	X, Y, Z float64
}

Float3 is an element of float64^3.

func (Float3) Add

func (p Float3) Add(q Float3) Float3

Add performs vector addition.

func (Float3) CDiv

func (p Float3) CDiv(q Float3) Float3

CDiv performs componentwise division.

func (Float3) CMul

func (p Float3) CMul(q Float3) Float3

CMul performs componentwise multiplication.

func (Float3) Coord

func (p Float3) Coord() (x, y, z float64)

Coord returns the components of the vector.

func (Float3) Div

func (p Float3) Div(k float64) Float3

Div performs scalar division by k.

func (Float3) Dot

func (p Float3) Dot(q Float3) float64

Dot returns the dot product of the two vectors.

func (Float3) Mul

func (p Float3) Mul(k float64) Float3

Mul performs scalar multiplication.

func (Float3) Neg

func (p Float3) Neg() Float3

Neg returns the vector pointing in the opposite direction.

func (Float3) Sign

func (p Float3) Sign() Float3

Sign returns a sign vector.

func (Float3) String

func (p Float3) String() string

String returns a string representation of p like "(3.0,4.0,5.0)".

func (Float3) Sub

func (p Float3) Sub(q Float3) Float3

Sub performs vector subtraction.

type Int3

type Int3 struct {
	X, Y, Z int
}

Int3 is a an element of int^3.

func Pt3

func Pt3(x, y, z int) Int3

Pt3 returns Int3{x, y, z}.

func (Int3) Add

func (p Int3) Add(q Int3) Int3

Add performs vector addition.

func (Int3) CDiv

func (p Int3) CDiv(q Int3) Int3

CDiv performs componentwise division.

func (Int3) CMul

func (p Int3) CMul(q Int3) Int3

CMul performs componentwise multiplication.

func (Int3) Coord

func (p Int3) Coord() (x, y, z int)

Coord returns the components of the vector.

func (Int3) Div

func (p Int3) Div(k int) Int3

Div performs scalar division by k.

func (Int3) Dot

func (p Int3) Dot(q Int3) int

Dot returns the dot product of the two vectors.

func (Int3) Mul

func (p Int3) Mul(k int) Int3

Mul performs scalar multiplication.

func (Int3) Neg

func (p Int3) Neg() Int3

Neg returns the vector pointing in the opposite direction.

func (Int3) Sign

func (p Int3) Sign() Int3

Sign returns a sign vector.

func (Int3) String

func (p Int3) String() string

String returns a string representation of p like "(3,4,5)".

func (Int3) Sub

func (p Int3) Sub(q Int3) Int3

Sub performs vector subtraction.

func (Int3) XY

func (p Int3) XY() image.Point

XY applies the Z-forgetting projection. (It returns just X and Y.)

func (Int3) XZ

func (p Int3) XZ() image.Point

XZ applies the Y-forgetting projection. (It returns just X and Z (as Y).)

type IntFloat

type IntFloat struct {
	I int
	F float64
}

IntFloat represents a number as an integer part plus a fractional part. This can represent reals in the int range with decent precision.

func ToIntFloat

func ToIntFloat(f float64) IntFloat

ToIntFloat converts a float64 directly into an IntFloat.

func (IntFloat) Add

func (x IntFloat) Add(y IntFloat) IntFloat

Add returns x+y (not canonicalised).

func (IntFloat) Canon

func (x IntFloat) Canon() IntFloat

Canon returns a value equal to x, in canonical form (0 ≤ F < 1). Each possible value only has one canonical form.

func (IntFloat) Div

func (x IntFloat) Div(y IntFloat) IntFloat

Div returns x/y, canonicalised.

func (IntFloat) Float

func (x IntFloat) Float() float64

Float converts the value into a float64.

func (IntFloat) Gt

func (x IntFloat) Gt(y IntFloat) bool

Gt reports x > y. x and y must be in canonical form for the comparison to be meaningful.

func (IntFloat) Inv

func (x IntFloat) Inv() IntFloat

Inv returns 1/x, canonicalised.

func (IntFloat) Lt

func (x IntFloat) Lt(y IntFloat) bool

Lt reports x < y. x and y must be in canonical form for the comparison to be meaningful.

func (IntFloat) Mul

func (x IntFloat) Mul(y IntFloat) IntFloat

Mul returns x*y, canonicalised.

func (IntFloat) Neg

func (x IntFloat) Neg() IntFloat

Neg returns -x (not canonicalised).

func (IntFloat) String

func (x IntFloat) String() string

func (IntFloat) Sub

func (x IntFloat) Sub(y IntFloat) IntFloat

Sub returns x-y (not canonicalised).

type IntMatrix2x3

type IntMatrix2x3 struct{ X, Y Int3 }

IntMatrix2x3 implements a 2 row, 3 column matrix (as two row vectors).

func (IntMatrix2x3) Apply

func (a IntMatrix2x3) Apply(v Int3) image.Point

Apply applies the matrix to a vector to obtain a transformed vector.

type IntMatrix3

type IntMatrix3 [3][3]int

IntMatrix3 implements a 3x3 integer matrix.

func (IntMatrix3) Apply

func (a IntMatrix3) Apply(v Int3) Int3

Apply applies the matrix to a vector to obtain a transformed vector.

func (IntMatrix3) Concat

func (a IntMatrix3) Concat(b IntMatrix3) IntMatrix3

Concat returns the matrix equivalent to applying matrix a and then b.

type IntMatrix3x4

type IntMatrix3x4 [3][4]int

IntMatrix3x4 implements a 3 row, 4 column integer matrix, capable of describing any integer affine transformation.

func (IntMatrix3x4) Apply

func (a IntMatrix3x4) Apply(v Int3) Int3

Apply applies the matrix to a vector to obtain a transformed vector.

func (IntMatrix3x4) ToRatMatrix3

func (a IntMatrix3x4) ToRatMatrix3() RatMatrix3

ToRatMatrix3 returns the 3x3 submatrix as a rational matrix equivalent.

func (IntMatrix3x4) Translation

func (a IntMatrix3x4) Translation() Int3

Translation returns the translation component of the matrix (last column) i.e. what you get if you Apply the matrix to the zero vector.

type IntProjection

type IntProjection image.Point

IntProjection uses two integers to define a custom projection. It is designed for projecting Z onto X and Y with integer fractions as would be used in e.g. a diametric projection (IntProjection{X:0, Y:2}).

func (IntProjection) Project

func (π IntProjection) Project(z int) image.Point

Project returns (z/π.X, z/π.Y), unless π.X or π.Y are 0, in which case that component is zero

func (IntProjection) Sign

func (π IntProjection) Sign() image.Point

Sign returns CSign(π).

type LinearSpline

type LinearSpline struct {
	Points []Float2
	// contains filtered or unexported fields
}

LinearSpline implements a linear spline.

func (*LinearSpline) Interpolate

func (s *LinearSpline) Interpolate(x float64) float64

Interpolate returns y where (x,y) is a point on the spline. If x is outside the spline, it extrapolates from either the first or last segments of the spline.

func (*LinearSpline) Prepare

func (s *LinearSpline) Prepare() error

Prepare sorts the points and computes internal information.

type Matrix3x4

type Matrix3x4 [3][4]float64

Matrix3x4 implements a 3x4 matrix with floating-point values.

func (Matrix3x4) Adjugate

func (a Matrix3x4) Adjugate() Matrix3x4

Adjugate returns the adjugate of the 3x3 submatrix.

func (Matrix3x4) Apply

func (a Matrix3x4) Apply(v Float3) Float3

Apply applies the matrix to the vector v.

func (Matrix3x4) Concat

func (a Matrix3x4) Concat(b Matrix3x4) Matrix3x4

Concat returns the matrix equivalent to applying matrix a and then b.

func (Matrix3x4) Inverse

func (a Matrix3x4) Inverse() (Matrix3x4, error)

Inverse returns the inverse of the matrix.

func (Matrix3x4) Mul

func (a Matrix3x4) Mul(r float64) Matrix3x4

Mul multiplies the whole matrix by a scalar.

func (Matrix3x4) Translation

func (a Matrix3x4) Translation() Float3

Translation returns the translation component of the matrix (last column) i.e. what you get if you Apply the matrix to the zero vector.

type Projection

type Projection struct{ X, Y float64 }

Projection uses two floats to define a custom projection.

func (Projection) Project

func (π Projection) Project(z int) image.Point

Project returns (z*π.X, z*π.Y).

func (Projection) Sign

func (π Projection) Sign() image.Point

Sign returns the componentwise sign of π.

type Projector

type Projector interface {
	// Sign returns a {-1, 0, 1}-valued 2D vector pointing in the direction that
	// positive Z values are projected to.
	Sign() image.Point
	// Project converts a Z coordinate to a 2D offset.
	Project(int) image.Point
}

Projector types can be used to project 3D coordinates into 2D. It only supports projecting Z into a 2D offset (i.e. not a general projection).

type Rat

type Rat struct{ N, D int }

Rat is a (small) rational number implementation. Overflow can happen.

func IntRat

func IntRat(n int) Rat

IntRat returns the rational representation of n.

func (Rat) Add

func (r Rat) Add(q Rat) Rat

Add returns r + q.

func (Rat) Canon

func (r Rat) Canon() Rat

Canon puts the rational number into reduced form.

func (Rat) Div

func (r Rat) Div(q Rat) Rat

Div returns r/q.

func (Rat) Int

func (r Rat) Int() int

Int returns r.N / r.D.

func (Rat) Invert

func (r Rat) Invert() Rat

Invert returns 1/r if it exists, otherwise panics.

func (Rat) Mul

func (r Rat) Mul(q Rat) Rat

Mul returns r * q.

func (Rat) Neg

func (r Rat) Neg() Rat

Neg returns -r.

func (Rat) Rem

func (r Rat) Rem() int

Rem returns r.N % r.D.

func (Rat) String

func (r Rat) String() string

String returns a nice string representation like "-3/5".

func (Rat) Sub

func (r Rat) Sub(q Rat) Rat

Sub returns r - q.

type RatMatrix3

type RatMatrix3 [3][3]Rat

RatMatrix3 implements a 3x3 matrix with rational number entries.

func (RatMatrix3) Adjugate

func (a RatMatrix3) Adjugate() RatMatrix3

Adjugate returns the adjugate of the matrix.

func (RatMatrix3) Concat

func (a RatMatrix3) Concat(b RatMatrix3) RatMatrix3

Concat returns the matrix equivalent to applying matrix a and then b.

func (RatMatrix3) IntApply

func (a RatMatrix3) IntApply(v Int3) Int3

IntApply applies the matrix to the integer vector v. Any remainder is lost.

func (RatMatrix3) Inverse

func (a RatMatrix3) Inverse() (RatMatrix3, error)

Inverse returns the inverse of the matrix.

func (RatMatrix3) Mul

func (a RatMatrix3) Mul(r Rat) RatMatrix3

Mul multiplies the whole matrix by a scalar.

type SimpleProjection

type SimpleProjection struct{}

SimpleProjection projects Z onto Y only.

func (SimpleProjection) Project

func (SimpleProjection) Project(z int) image.Point

Project returns (0, z).

func (SimpleProjection) Sign

func (SimpleProjection) Sign() image.Point

Sign returns (0, 1).

Jump to

Keyboard shortcuts

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