model2d

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2020 License: BSD-2-Clause Imports: 16 Imported by: 10

Documentation

Overview

Package model2d provides various tools for loading, manipulating, saving, and using 2D shapes. It is also intended to aid in creating 3D models which incorporate 2D shapes in some way.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColliderContains

func ColliderContains(c Collider, coord Coord, margin float64) bool

ColliderContains checks if a point is within a Collider and at least margin away from the border.

If the margin is negative, points are also conatined if the point is less than -margin away from the surface.

func EncodeSVG

func EncodeSVG(m *Mesh) []byte

func GroupSegments

func GroupSegments(segs []*Segment)

GroupSegments sorts the segments recursively by their x and y values. This can be used to prepare segments for GroupedSegmentsToCollider.

func InBounds

func InBounds(b Bounder, c Coord) bool

InBounds returns true if c is contained within the bounding rectangle of b.

func Triangulate

func Triangulate(polygon []Coord) [][3]Coord

Triangulate turns any simple polygon into a set of equivalent triangles.

The polygon is passed as a series of points, in order. The first point is re-used as the ending point, so no ending should be explicitly specified.

Types

type BezierCurve

type BezierCurve []Coord

BezierCurve implements an arbitrarily high-dimensional Bezier curve.

func (BezierCurve) Eval

func (b BezierCurve) Eval(t float64) Coord

Eval evaluates the curve at time t, where 0 <= t <= 1.

func (BezierCurve) EvalX

func (b BezierCurve) EvalX(x float64) float64

EvalX finds the y value that occurs at the given x value, assuming that the curve is monotonic in x.

If the y value cannot be found, NaN is returned.

func (BezierCurve) InverseX

func (b BezierCurve) InverseX(x float64) float64

InverseX gets the t value between 0 and 1 where the x value is equal to some x, assuming the curve is monotonic in x.

If the t cannot be found, NaN is returned.

func (BezierCurve) Transpose

func (b BezierCurve) Transpose() BezierCurve

Transpose generates a BezierCurve where x and y are swapped.

type Bitmap

type Bitmap struct {
	Data   []bool
	Width  int
	Height int
}

A Bitmap is a two-dimensional image with boolean values. The data is stored in row-major order.

func MustReadBitmap

func MustReadBitmap(path string, c ColorBitFunc) *Bitmap

MustReadBitmap is like ReadBitmap, except that it panics if the bitmap cannot be read.

func NewBitmap

func NewBitmap(width, height int) *Bitmap

NewBitmap creates an empty bitmap.

func NewBitmapImage

func NewBitmapImage(img image.Image, c ColorBitFunc) *Bitmap

NewBitmapImage creates a Bitmap from an image, by calling c for each pixel and using the result as the bit.

If c is nil, then the mean RGBA is computed, and pixels are considered true if they are closer to the mean in L2 distance than they are to the top-left pixel. For images with two dominant colors, this is equivalent to making the background false, and the foreground true, assuming that the first pixel is background.

func ReadBitmap

func ReadBitmap(path string, c ColorBitFunc) (*Bitmap, error)

ReadBitmap is like NewBitmapImage, except that it reads the image from a file.

func (*Bitmap) FlipX

func (b *Bitmap) FlipX() *Bitmap

FlipX reverses the x-axis.

func (*Bitmap) FlipY

func (b *Bitmap) FlipY() *Bitmap

FlipY reverses the y-axis.

func (*Bitmap) Get

func (b *Bitmap) Get(x, y int) bool

Get gets the bit at the coordinate.

If the coordinate is out of bounds, false is returned.

func (*Bitmap) Invert

func (b *Bitmap) Invert() *Bitmap

Invert creates a new bitmap with the opposite values.

func (*Bitmap) Mesh

func (b *Bitmap) Mesh() *Mesh

Mesh converts the bitmap to a mesh by creating boxes around every true pixel and deleting duplicate segments.

func (*Bitmap) Set

func (b *Bitmap) Set(x, y int, v bool)

Set sets the bit at the coordinate.

The coordinate must be in bounds.

type Bounder

type Bounder interface {
	// Get the corners of a bounding rectangle.
	//
	// A point p satisfies p >= Min and p <= Max if it is
	// within the bounds.
	Min() Coord
	Max() Coord
}

A Bounder is an object contained in a rectangle.

type Circle added in v0.2.3

type Circle struct {
	Center Coord
	Radius float64
}

A Circle is a 2D perfect circle.

func (*Circle) Contains added in v0.2.3

func (c *Circle) Contains(coord Coord) bool

func (*Circle) Max added in v0.2.3

func (c *Circle) Max() Coord

func (*Circle) Min added in v0.2.3

func (c *Circle) Min() Coord

type Collider

type Collider interface {
	Bounder

	// RayCollisions enumerates the collisions with a ray.
	// It returns the total number of collisions.
	//
	// f may be nil, in which case this is simply used for
	// counting.
	RayCollisions(r *Ray, f func(RayCollision)) int

	// FirstRayCollision gets the ray collision with the
	// lowest scale.
	//
	// The second return value is false if no collisions
	// were found.
	FirstRayCollision(r *Ray) (collision RayCollision, collides bool)

	// CircleCollision checks if the collider touches a
	// circle with origin c and radius r.
	CircleCollision(c Coord, r float64) bool
}

A Collider is the outline of a 2-dimensional shape. It can count its intersections with a ray, and check if any part of the outline is inside a circle.

All methods of a Collider are safe for concurrency.

func GroupedSegmentsToCollider

func GroupedSegmentsToCollider(segs []*Segment) Collider

GroupedSegmentsToCollider converts pre-grouped segments into an efficient collider. If the segments were not grouped with GroupSegments, then the resulting collider may be highly inefficient.

func MeshToCollider

func MeshToCollider(m *Mesh) Collider

MeshToCollider converts a mesh to an efficient Collider.

type ColliderSolid

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

ColliderSolid is a Solid which uses the even-odd test for a Collider.

func NewColliderSolid

func NewColliderSolid(c Collider) *ColliderSolid

NewColliderSolid creates a basic ColliderSolid.

func NewColliderSolidHollow

func NewColliderSolidHollow(c Collider, r float64) *ColliderSolid

NewColliderSolidHollow creates a ColliderSolid that only reports containment around the edges.

func NewColliderSolidInset

func NewColliderSolidInset(c Collider, inset float64) *ColliderSolid

NewColliderSolidInset creates a ColliderSolid that only reports containment at some distance from the surface.

If inset is negative, then the solid is outset from the collider.

func (*ColliderSolid) Contains

func (c *ColliderSolid) Contains(coord Coord) bool

Contains checks if coord is in the solid.

func (*ColliderSolid) Max

func (c *ColliderSolid) Max() Coord

Max gets the maximum of the bounding box.

func (*ColliderSolid) Min

func (c *ColliderSolid) Min() Coord

Min gets the minimum of the bounding box.

type ColorBitFunc

type ColorBitFunc func(c color.Color) bool

ColorBitFunc turns colors into single bits.

type Coord

type Coord struct {
	X float64
	Y float64
}

A Coord is a coordinate in 2-D Euclidean space.

func NewCoordArray

func NewCoordArray(a [2]float64) Coord

NewCoordArray creates a Coord from an array of x and y.

func NewCoordRandNorm

func NewCoordRandNorm() Coord

NewCoordRandNorm creates a random Coord with normally distributed components.

func NewCoordRandUniform added in v0.2.4

func NewCoordRandUniform() Coord

NewCoordRandUniform creates a random Coord with uniformly random coordinates in [0, 1).

func NewCoordRandUnit

func NewCoordRandUnit() Coord

NewCoordRandUnit creates a random Coord with magnitude 1.

func X added in v0.2.4

func X(x float64) Coord

X gets a coordinate in the X direction.

func Y added in v0.2.4

func Y(y float64) Coord

Y gets a coordinate in the Y direction.

func (Coord) Add

func (c Coord) Add(c1 Coord) Coord

Add computes the sum of c and c1.

func (Coord) Array

func (c Coord) Array() [2]float64

Array gets an array of x and y.

func (Coord) Dist

func (c Coord) Dist(c1 Coord) float64

Dist computes the Euclidean distance to c1.

func (Coord) Div added in v0.2.2

func (c Coord) Div(c1 Coord) Coord

Div computes the element-wise quotient of c / c1.

func (Coord) Dot

func (c Coord) Dot(c1 Coord) float64

Dot computes the dot product of c and c1.

func (Coord) Max

func (c Coord) Max(c1 Coord) Coord

Max gets the element-wise maximum of c and c1.

func (Coord) Mid

func (c Coord) Mid(c1 Coord) Coord

Mid computes the midpoint between c and c1.

func (Coord) Min

func (c Coord) Min(c1 Coord) Coord

Min gets the element-wise minimum of c and c1.

func (Coord) Mul

func (c Coord) Mul(c1 Coord) Coord

Mul computes the element-wise product of c and c1.

func (Coord) Norm

func (c Coord) Norm() float64

Norm computes the vector L2 norm.

func (Coord) Normalize

func (c Coord) Normalize() Coord

Normalize gets a unit vector from c.

func (Coord) ProjectOut

func (c Coord) ProjectOut(c1 Coord) Coord

ProjectOut projects the c1 direction out of c.

func (Coord) Reflect

func (c Coord) Reflect(c1 Coord) Coord

Reflect reflects c1 around c.

func (Coord) Scale

func (c Coord) Scale(s float64) Coord

Scale scales all the coordinates by s and returns the new coordinate.

func (Coord) Sub

func (c Coord) Sub(c1 Coord) Coord

Sub computes c - c1.

func (Coord) Sum

func (c Coord) Sum() float64

Sum sums the elements of c.

type InterpBitmap

type InterpBitmap struct {
	Data   []color.RGBA
	Width  int
	Height int
	Model  color.Model
	F      ColorBitFunc

	// Interp is the interpolation function.
	// A zero value is Bicubic.
	Interp Interpolator
}

An InterpBitmap is a dynamic Bitmap backed by an image with a color interpolation scheme.

func MustReadInterpBitmap

func MustReadInterpBitmap(path string, c ColorBitFunc) *InterpBitmap

MustReadInterpBitmap is like ReadInterpBitmap, except that it panics if the InterpBitmap cannot be read.

func NewInterpBitmap

func NewInterpBitmap(img image.Image, c ColorBitFunc) *InterpBitmap

NewInterpBitmap creates a InterpBitmap from an image.

If c is nil, then the mean RGBA is computed, and pixels are considered true if they are closer to the mean in L2 distance than they are to the top-left pixel. For images with two dominant colors, this is equivalent to making the background false, and the foreground true, assuming that the first pixel is background.

func ReadInterpBitmap

func ReadInterpBitmap(path string, c ColorBitFunc) (*InterpBitmap, error)

ReadInterpBitmap is like NewInterpBitmap, except that it reads the image from a file.

func (*InterpBitmap) Bitmap

func (b *InterpBitmap) Bitmap() *Bitmap

Bitmap gets an uninterpolated bitmap from b.

func (*InterpBitmap) Contains

func (b *InterpBitmap) Contains(c Coord) bool

Contains gets the bit at the interpolated coordinate.

If the coordinate is out of bounds, false is returned.

func (*InterpBitmap) FlipX

func (b *InterpBitmap) FlipX() *InterpBitmap

FlipX reverses the x-axis.

func (*InterpBitmap) FlipY

func (b *InterpBitmap) FlipY() *InterpBitmap

FlipY reverses the y-axis.

func (*InterpBitmap) Get

func (b *InterpBitmap) Get(x, y int) color.RGBA

Get gets the color at the coordinate.

If the coordinate is out of bounds, a the edge of the image is extended.

func (*InterpBitmap) Invert

func (b *InterpBitmap) Invert() *InterpBitmap

Invert creates a new InterpBitmap with the opposite color bitmap values.

func (*InterpBitmap) Max

func (b *InterpBitmap) Max() Coord

Max gets the maximum of the pixel bounding box.

func (*InterpBitmap) Min

func (b *InterpBitmap) Min() Coord

Min gets the minimum of the pixel bounding box.

type Interpolator

type Interpolator int

Interpolator is a 1-dimensional interpolation kernel.

const (
	Bicubic Interpolator = iota
	Bilinear
)

func (Interpolator) Kernel

func (i Interpolator) Kernel(t float64) []float64

type IntersectedSolid added in v0.2.3

type IntersectedSolid []Solid

IntersectedSolid is a Solid containing the intersection of one or more Solids.

func (IntersectedSolid) Contains added in v0.2.3

func (i IntersectedSolid) Contains(c Coord) bool

func (IntersectedSolid) Max added in v0.2.3

func (i IntersectedSolid) Max() Coord

func (IntersectedSolid) Min added in v0.2.3

func (i IntersectedSolid) Min() Coord

type JoinedCollider

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

A JoinedCollider wraps multiple other Colliders and only passes along rays and circles that enter their combined bounding box.

func NewJoinedCollider

func NewJoinedCollider(other []Collider) *JoinedCollider

NewJoinedCollider creates a JoinedCollider which combines zero or more other colliders.

func (*JoinedCollider) CircleCollision

func (j *JoinedCollider) CircleCollision(center Coord, r float64) bool

func (*JoinedCollider) FirstRayCollision

func (j *JoinedCollider) FirstRayCollision(r *Ray) (RayCollision, bool)

func (*JoinedCollider) Max

func (j *JoinedCollider) Max() Coord

func (*JoinedCollider) Min

func (j *JoinedCollider) Min() Coord

func (*JoinedCollider) RayCollisions

func (j *JoinedCollider) RayCollisions(r *Ray, f func(RayCollision)) int

type JoinedSolid

type JoinedSolid []Solid

JoinedSolid combines one or more other solids into a single union.

func (JoinedSolid) Contains

func (j JoinedSolid) Contains(c Coord) bool

func (JoinedSolid) Max

func (j JoinedSolid) Max() Coord

func (JoinedSolid) Min

func (j JoinedSolid) Min() Coord

type Matrix2

type Matrix2 [4]float64

Matrix2 is a 2x2 matrix, stored in row-major order.

func NewMatrix2Columns

func NewMatrix2Columns(c1, c2 Coord) *Matrix2

NewMatrix2Columns creates a Matrix2 with the given coordinates as column entries.

func NewMatrix2Rotation

func NewMatrix2Rotation(theta float64) *Matrix2

NewMatrix2Rotation creates a rotation matrix that rotates column vectors by theta.

func (*Matrix2) Add added in v0.2.2

func (m *Matrix2) Add(m1 *Matrix2) *Matrix2

Add computes m+m1 and returns the sum.

func (*Matrix2) Det

func (m *Matrix2) Det() float64

Det computes the determinant of the matrix.

func (*Matrix2) Eigenvalues added in v0.2.2

func (m *Matrix2) Eigenvalues() [2]complex128

Eigenvalues computes the eigenvalues of the matrix.

There may be a repeated eigenvalue, but for numerical reasons two are always returned.

func (*Matrix2) Inverse

func (m *Matrix2) Inverse() *Matrix2

Inverse computes the inverse matrix.

func (*Matrix2) InvertInPlace

func (m *Matrix2) InvertInPlace()

InvertInPlace moves the inverse of m into m without causing any new allocations.

func (*Matrix2) Mul

func (m *Matrix2) Mul(m1 *Matrix2) *Matrix2

Mul computes m*m1 and returns the product.

func (*Matrix2) MulColumn

func (m *Matrix2) MulColumn(c Coord) Coord

MulColumn multiplies the matrix m by a column vector represented by c.

func (*Matrix2) SVD added in v0.2.2

func (m *Matrix2) SVD(u, s, v *Matrix2)

SVD computes the singular value decomposition of the matrix.

It populates matrices u, s, and v, such that

m = u*s*v.Transpose()

The singular values in s are sorted largest to smallest.

func (*Matrix2) Scale

func (m *Matrix2) Scale(s float64)

Scale scales m by a factor s.

func (*Matrix2) Transpose

func (m *Matrix2) Transpose() *Matrix2

Transpose computes the matrix transpose.

type Mesh

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

A Mesh is a collection of segments.

The segments are uniquely identified as pointers, not as values. This is important for methods which reference existing segments, such as Remove and Neighbors.

Segments in a mesh are "connected" when they contain exactly identical points. Thus, small rounding errors can cause segments to incorrectly be disassociated with each other.

A Mesh can be read safely from concurrent Goroutines, but modifications must not be performed concurrently with any mesh operations.

func MarchingSquares

func MarchingSquares(s Solid, delta float64) *Mesh

MarchingSquares turns a Solid into a mesh using a 2D version of the marching cubes algorithm.

func MarchingSquaresSearch

func MarchingSquaresSearch(s Solid, delta float64, iters int) *Mesh

MarchingSquaresSearch is like MarchingSquares, but applies an additional search step to move the vertices along the edges of each square.

The tightness of the triangulation will double for every iteration.

func NewMesh

func NewMesh() *Mesh

NewMesh creates an empty mesh.

func NewMeshSegments

func NewMeshSegments(segs []*Segment) *Mesh

NewMeshSegments creates a mesh with the given collection of Segments.

func (*Mesh) Add

func (m *Mesh) Add(s *Segment)

Add adds the Segment s to the mesh.

func (*Mesh) AddMesh

func (m *Mesh) AddMesh(m1 *Mesh)

AddMesh adds all the Segments from m1 to m.

func (*Mesh) Blur

func (m *Mesh) Blur(rate float64) *Mesh

Blur moves each vertex closer to the average of its neighbors.

The rate argument controls how much the vertices move. If it is 1, then the vertices become the average of their neighbors. If it is 0, then the vertices remain where they are.

func (*Mesh) Contains

func (m *Mesh) Contains(s *Segment) bool

Contains checks if s has been added to the mesh.

func (*Mesh) Find

func (m *Mesh) Find(ps ...Coord) []*Segment

Find gets all the Segments that contain all of the passed points.

This is only useful with one or two coordinates.

func (*Mesh) Iterate

func (m *Mesh) Iterate(f func(s *Segment))

Iterate calls f for every Segment in m in an arbitrary order.

If f adds or removes Segments, they will not be visited.

func (*Mesh) IterateSorted

func (m *Mesh) IterateSorted(f func(s *Segment), cmp func(s1, s2 *Segment) bool)

IterateSorted is like Iterate, but it first sorts all the Segments according to a less than function, cmp.

func (*Mesh) IterateVertices added in v0.1.1

func (m *Mesh) IterateVertices(f func(c Coord))

IterateVertices calls f for every vertex in m in an arbitrary order.

If f adds or removes vertices, they will not be visited.

func (*Mesh) Manifold

func (m *Mesh) Manifold() bool

Manifold checks if the mesh is manifold, i.e. if every vertex has two segments.

func (*Mesh) MapCoords

func (m *Mesh) MapCoords(f func(Coord) Coord) *Mesh

MapCoords creates a new mesh by transforming all of the coordinates according to the function f.

func (*Mesh) Max

func (m *Mesh) Max() Coord

Max gets the component-wise maximum across all the vertices in the mesh.

func (*Mesh) Min

func (m *Mesh) Min() Coord

Min gets the component-wise minimum across all the vertices in the mesh.

func (*Mesh) Neighbors

func (m *Mesh) Neighbors(s *Segment) []*Segment

Neighbors gets all the Segments with a vertex touching a given Segment s.

The Segment s itself is not included in the results.

The Segment s needn't be in the mesh. However, if it is not in the mesh, but an equivalent Segment is, then said equivalent Segment will be in the results.

func (*Mesh) Remove

func (m *Mesh) Remove(s *Segment)

Remove removes the Segment t from the mesh.

It looks at t as a pointer, so the pointer must be exactly the same as a Segment passed to Add.

func (*Mesh) RepairNormals added in v0.2.4

func (m *Mesh) RepairNormals(epsilon float64) (*Mesh, int)

RepairNormals flips normals when they point within the shape defined by the mesh, as determined by the even-odd rule.

The repaired mesh is returned, along with the number of modified segments.

The check is performed by adding the normal, scaled by epsilon, to the center of the segment, and then counting the number of ray collisions from this point in the direction of the normal.

func (*Mesh) SaveSVG

func (m *Mesh) SaveSVG(path string) error

func (*Mesh) Scale added in v0.2.2

func (m *Mesh) Scale(s float64) *Mesh

Scale creates a new mesh by scaling the coordinates by a factor s.

func (*Mesh) SegmentsSlice

func (m *Mesh) SegmentsSlice() []*Segment

SegmentsSlice gets a snapshot of all the Segments currently in the mesh. The resulting slice is a copy, and will not change as the mesh is updated.

func (*Mesh) Smooth

func (m *Mesh) Smooth(iters int) *Mesh

Smooth is similar to Blur, but it is less sensitive to differences in segment length.

func (*Mesh) SmoothSq

func (m *Mesh) SmoothSq(iters int) *Mesh

SmoothSq is like Smooth, but it minimizes the sum of squared segment lengths rather than the sum of lengths directly. Thus, SmoothSq produces more even segments than Smooth.

func (*Mesh) Subdivide

func (m *Mesh) Subdivide(iters int) *Mesh

Subdivide uses Chaikin subdivision to add segments between every vertex.

This can only be applied to manifold meshes. This can be checked with m.Manifold().

func (*Mesh) VertexSlice added in v0.1.1

func (m *Mesh) VertexSlice() []Coord

VertexSlice gets a snapshot of all the vertices currently in the mesh.

The result is a copy and is in no way connected to the mesh in memory.

type PointSDF added in v0.2.3

type PointSDF interface {
	SDF

	// PointSDF gets the SDF at c and also returns the
	// nearest point to c on the shape.
	PointSDF(c Coord) (Coord, float64)
}

A PointSDF is an SDF that can additionally get the nearest point on a shape.

func GroupedSegmentsToSDF added in v0.2.3

func GroupedSegmentsToSDF(segs []*Segment) PointSDF

GroupedSegmentsToSDF creates a PointSDF from a slice of segments. If the segments are not grouped by GroupSegments(), the resulting PointSDF is inefficient.

func MeshToSDF added in v0.2.3

func MeshToSDF(m *Mesh) PointSDF

MeshToSDF turns a mesh into a PointSDF.

type Ray

type Ray struct {
	Origin    Coord
	Direction Coord
}

A Ray is a line originating at a point and extending infinitely in some (positive) direction.

type RayCollision

type RayCollision struct {
	// The amount of the ray direction to add to the ray
	// origin to hit the point in question.
	//
	// The scale should be non-negative.
	Scale float64

	// The normal pointing outward from the outline at the
	// point of collision.
	Normal Coord
}

RayCollision is a point where a ray intersects a 2-dimensional outline.

type Rect added in v0.2.4

type Rect struct {
	MinVal Coord
	MaxVal Coord
}

A Rect is a 2D axis-aligned rectangle.

func (*Rect) Contains added in v0.2.4

func (r *Rect) Contains(c Coord) bool

func (*Rect) Max added in v0.2.4

func (r *Rect) Max() Coord

func (*Rect) Min added in v0.2.4

func (r *Rect) Min() Coord

type SDF added in v0.2.3

type SDF interface {
	Bounder

	SDF(c Coord) float64
}

An SDF is a signed distance function.

An SDF returns 0 on the boundary of some shape, positive values inside the shape, and negative values outside the shape. The magnitude is the distance to the shape.

All methods of an SDF are safe for concurrency.

func ColliderToSDF added in v0.2.3

func ColliderToSDF(c Collider, iterations int) SDF

ColliderToSDF generates an SDF that uses bisection search to approximate the SDF for any Collider.

The iterations argument controls the precision. If set to 0, a default of 32 is used.

type Segment

type Segment [2]Coord

A Segment is a 2-dimensional line segment.

The order determines the normal direction.

In particular, if the segments in a polygon go in the clockwise direction (assuming the y-axis faces down), then the normals face outwards from the polygon.

func (*Segment) CircleCollision

func (s *Segment) CircleCollision(c Coord, r float64) bool

CircleCollision checks if the circle intersects the segment s.

func (Segment) Closest added in v0.2.3

func (s Segment) Closest(c Coord) Coord

Closest gets the point on the segment closest to c.

func (Segment) Dist added in v0.2.3

func (s Segment) Dist(c Coord) float64

Dist gets the minimum distance from c to a point on the line segment.

func (*Segment) FirstRayCollision

func (s *Segment) FirstRayCollision(r *Ray) (RayCollision, bool)

FirstRayCollision gets the ray collision if there is one.

func (Segment) Length

func (s Segment) Length() float64

Length gets the length of the segment.

func (Segment) Max

func (s Segment) Max() Coord

Max gets the element-wise maximum of the endpoints.

func (Segment) Mid

func (s Segment) Mid() Coord

Mid gets the midpoint of the segment.

func (Segment) Min

func (s Segment) Min() Coord

Min gets the element-wise minimum of the endpoints.

func (*Segment) Normal

func (s *Segment) Normal() Coord

Normal computes the normal vector to the segment, facing outwards from the surface.

func (*Segment) RayCollisions

func (s *Segment) RayCollisions(r *Ray, f func(RayCollision)) int

RayCollisions calls f (if non-nil) with a collision (if applicable) and returns the collisions count (0 or 1).

type Solid

type Solid interface {
	// Contains must always return false outside of the
	// boundaries of the solid.
	Bounder

	Contains(c Coord) bool
}

A Solid defines a two-dimensional shape.

For any given coordinate, the solid can check if the shape contains that coordinate.

All methods of a Solid are safe for concurrency.

func BitmapToSolid

func BitmapToSolid(b *Bitmap) Solid

BitmapToSolid creates a Solid which is true at pixels where the bitmap is true, and false elsewhere.

func ScaleSolid

func ScaleSolid(solid Solid, s float64) Solid

ScaleSolid creates a new Solid that scales incoming coordinates c by 1/s. Thus, the new solid is s times larger.

type SubtractedSolid added in v0.2.3

type SubtractedSolid struct {
	Positive Solid
	Negative Solid
}

SubtractedSolid is a Solid consisting of all the points in Positive which are not in Negative.

func (*SubtractedSolid) Contains added in v0.2.3

func (s *SubtractedSolid) Contains(c Coord) bool

func (*SubtractedSolid) Max added in v0.2.3

func (s *SubtractedSolid) Max() Coord

func (*SubtractedSolid) Min added in v0.2.3

func (s *SubtractedSolid) Min() Coord

Jump to

Keyboard shortcuts

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