Documentation ¶
Index ¶
- Constants
- Variables
- func Abs(x float64) float64
- func AngleOfSide(polygon []vectors.Vec2, i int) float64
- func CenterOfPath(points []vectors.Vec2) vectors.Vec2
- func ContainsPoint(polygon []float64, pointX, pointY float64) bool
- func DrawLine(img *image.RGBA, a, b vectors.Vec2, color color.RGBA, scale float64)
- func GetBresenhamLine(pos1, pos2 vectors.IVec2) (points []vectors.IVec2)
- func GetPathSides(path []vectors.Vec2) []vectors.Segment
- func InRectangle(a, b, c Point) bool
- func IsConvex(polygon []float64) bool
- func IsSimple(polygon []float64) bool
- func PointInTriangle(px, py, ax, ay, bx, by, cx, cy float64) bool
- func PolygonArrayToPolygon(polygon []float64) []vectors.Vec2
- func PolygonToPolygonArray(polygon []vectors.Vec2) []float64
- func Reverse(polygon []float64) []float64
- func RotatePolygonAroundPoint(polygon []vectors.Vec2, point vectors.Vec2, angle float64) []vectors.Vec2
- func SavePathAsPNG(path []vectors.Vec2, filename string, scale float64) error
- func ShrinkPath(points []vectors.Vec2, shrink float64) []vectors.Vec2
- func SimplifyPolyline(points []vectors.Vec2, tolerance float64, highestQuality bool) []vectors.Vec2
- func Slice(polygon []vectors.Vec2, startX, startY, endX, endY float64) ([][]vectors.Vec2, bool)
- func StraightSkeleton(points []vectors.Vec2, shrink, spacing float64) []vectors.Vec2
- func TranslatePath(path []vectors.Vec2, offset vectors.Vec2) []vectors.Vec2
- func Triangulate(polygon []vectors.Vec2) ([]int, error)
- func TriangulatePolyk(polygon []float64) []int
- type AABB
- type BlockyShape
- func (g BlockyShape) ConnectionPoints() []int
- func (g BlockyShape) GetConnectionDirection(idx int) int
- func (g *BlockyShape) GetIsNeighborset(idx int) [4]bool
- func (g BlockyShape) GetNeighbors(idx int) []int
- func (g BlockyShape) GetPaths() [][]vectors.Vec2
- func (g BlockyShape) GetRotated(rotateLeft bool, steps int) BlockyShape
- func (g BlockyShape) Print()
- func (g BlockyShape) RenderToImage(img *image.RGBA, color color.RGBA)
- type CircleShape
- type Edge
- type HShape
- type ISC
- type JShape
- type LShape
- type Mesh
- type PlusShape
- type Point
- type Polygon
- type RectangleShape
- type Shape
- type SquircleShape
- type TrapezoidShape
- type TriangleShape
- type UShape
Constants ¶
const ( RotateRight = iota RotateLeft )
Variables ¶
var ( BlockyOShape = BlockyShape{ Width: 3, Length: 3, Set: []bool{ true, true, true, true, false, true, true, true, true, }, } BlockyUShape = BlockyShape{ Width: 3, Length: 3, Set: []bool{ true, false, true, true, false, true, true, true, true, }, } BlockyLShape = BlockyShape{ Width: 3, Length: 3, Set: []bool{ true, false, false, true, false, false, true, true, true, }, } BlockyJShape = BlockyShape{ Width: 3, Length: 3, Set: []bool{ false, true, true, false, false, true, true, true, true, }, } BlockyTShape = BlockyShape{ Width: 3, Length: 3, Set: []bool{ true, true, true, false, true, false, false, true, false, }, } BlockyPlusShape = BlockyShape{ Width: 3, Length: 3, Set: []bool{ false, true, false, true, true, true, false, true, false, }, } )
var BlockyShapes = []BlockyShape{ BlockyOShape, BlockyUShape, BlockyLShape, BlockyJShape, BlockyTShape, BlockyPlusShape, }
Functions ¶
func AngleOfSide ¶
AngleOfSide returns the absolute angle of the normal of the side of the polygon with the given index.
func CenterOfPath ¶
CenterOfPath calculates the center of a polygon (by averaging all points).
func ContainsPoint ¶
*
- Checks, if polygon contains [x, y]. *
- Works with simple polygons only. *
- @param {number[]} polygon [x1, y1, x2, y2...]
- @param {number} pointX Coordinate [x]
- @param {number} pointY Coordinate [y]
- @returns {boolean} depth
func GetBresenhamLine ¶
GetBresenhamLine returns a list of points that are on the line between pos1 and pos2. The line is drawn using the Bresenham algorithm. See: http://www.roguebasin.com/index.php/Bresenham%27s_Line_Algorithm
func InRectangle ¶
*
- In Rectangle *
- @private
- @param {Point} a
- @param {Point} b
- @param {Point} c
- @return {boolean}
func IsConvex ¶
*
- Checks, if polygon is convex. Polygon is convex, when each inner angle is <= 180°. *
- @param {number[]} polygon [x1, y1, x2, y2...]
- @returns {boolean}
func IsSimple ¶
*
- Checks, if polygon is simple. Polygon is simple, when its edges don't cross each other. *
- @param {number[]} polygon [x1, y1, x2, y2...]
- @returns {boolean} true if Polygon is simple
func PointInTriangle ¶
*
- Point in Triangle *
- @private
- @param {number} px
- @param {number} py
- @param {number} ax
- @param {number} ay
- @param {number} bx
- @param {number} by
- @param {number} cx
- @param {number} cy
- @returns {boolean}
func Reverse ¶
*
- Finds the point on polygon edges, which is closest to [x,y]. Returns an object in this format *
- "dist" is the distance of the polygon point, "edge" is the number of the closest edge, "point" is the closest point on that edge, "norm" is the normal from "point" to [x,y]. *
- @param {number[]} polygon [x1, y1, x2, y2...]
- @param {number} x Coordinate [x]
- @param {number} y Coordinate [y]
- @returns {ClosestEdge}
- @example
- //={dist:0, edge:0, point:{x:0, y:0}, norm:{x:0, y:0}}
func ClosestEdge(polygon []float64, x, y float64) (isc ClosestEdge) { var p = polygon var l = len(p) - 2 var empty = emptyPoints() var a1 = empty[0] var b1 = empty[2] var b2 = empty[3] // var c = tp[4] // is assigned a value but never used. a1.x = x a1.y = y isc.dist = math.Inf(1) for i := 0; i < l; i += 2 { b1.x = p[i] b1.y = p[i+1] b2.x = p[i+2] b2.y = p[i+3] isc = pointLineDist(a1, b1, b2, i>>1, isc) } b1.x = b2.x b1.y = b2.y b2.x = p[0] b2.y = p[1] isc = pointLineDist(a1, b1, b2, l>>1, isc) idst := 1 / isc.dist isc.norm.x = (x - isc.point.x) * idst isc.norm.y = (y - isc.point.y) * idst return isc }
*
- Reverse *
- @param {number[]} polygon [x1, y1, x2, y2...]
func RotatePolygonAroundPoint ¶
func RotatePolygonAroundPoint(polygon []vectors.Vec2, point vectors.Vec2, angle float64) []vectors.Vec2
RotatePolygonAroundPoint rotates a polygon around a point by the given angle.
func SavePathAsPNG ¶
SavePathAsPNG saves the path as a PNG image.
func ShrinkPath ¶
ShrinkPath shrinks a polygon by a given factor around its center.
func SimplifyPolyline ¶
SimplifyPolyline simplifies a polyline. NOTE: This code is based on: https://github.com/mourner/simplify-js
func StraightSkeleton ¶
StraightSkeleton calculates the straight skeleton of a polygon. The straight skeleton is a graph of edges that can be used to generate a roof. See: https://github.com/feldhaus/coding-2d-cookbook/blob/main/js/geometry/polygon-straight-skeleton.js NOTE: This is a very naive implementation that only works for simple polygons. There are some issues wit NaN values and the added epsilon values make it not precise.
func TranslatePath ¶
TranslatePath translates a path by the given offset.
func Triangulate ¶
Triangulate triangulates a polygon using the ear clipping algorithm. It returns the indices of each vertex of each triangle in pairs of 3.
func TriangulatePolyk ¶
*
- Computes the triangulation. Output array is array of triangles (triangle = 3 indices of polygon vertices). *
- Works with simple polygons only. *
- @param {number[]} polygon [x1, y1, x2, y2...]
- @returns {number[]} array of triangles (triangle = 3 indices of polygon vertices)
- @example
- var ids = PolyK.Triangulate([0, 0, 1, 0, 1, 1, 0, 1]);
- //=[0, 1, 2, 0, 2, 3]
Types ¶
type BlockyShape ¶
type BlockyShape struct {
Width, Length int // Width and length of the shape in tiles
Set []bool // Set of tiles that are set
}
BlockyShape represents a shape that is made up of blocks / tiles.
func NewBlockyShape ¶
func NewBlockyShape(width, length int) BlockyShape
NewBlockyShape creates a new BlockyShape with the given width and length.
func NewRandShape ¶
func NewRandShape(width, length int) BlockyShape
func (BlockyShape) ConnectionPoints ¶
func (g BlockyShape) ConnectionPoints() []int
ConnectionPoints returns the indices of all tiles that are connection points. A connection point is a tile that has only one neighbor.
func (BlockyShape) GetConnectionDirection ¶
func (g BlockyShape) GetConnectionDirection(idx int) int
GetConnectionDirection returns the direction of the connection of the given index.
func (*BlockyShape) GetIsNeighborset ¶
func (g *BlockyShape) GetIsNeighborset(idx int) [4]bool
GetIsNeighborset returns a boolean array that indicates if the tile at the given index has a neighbor in the given direction.
The directions are: 0: Top 1: Bottom 2: Left 3: Right
func (BlockyShape) GetNeighbors ¶
func (g BlockyShape) GetNeighbors(idx int) []int
GetNeighbors returns the indices of all set neighbors of the given index.
func (BlockyShape) GetPaths ¶
func (g BlockyShape) GetPaths() [][]vectors.Vec2
GetPaths returns all closed paths that make up the shape. TODO: This is a very naive implementation that just adds each square as a path. It should be improved to merge squares that are next to each other.
func (BlockyShape) GetRotated ¶
func (g BlockyShape) GetRotated(rotateLeft bool, steps int) BlockyShape
func (BlockyShape) RenderToImage ¶
func (g BlockyShape) RenderToImage(img *image.RGBA, color color.RGBA)
RenderToImage renders the shape to a supplied image with the given color.
type CircleShape ¶
type CircleShape struct { Radius float64 // Radius of the circle Steps int // Number of points to use to draw the circle }
CircleShape is a shape that is a circle.
func (CircleShape) ConnectionPoints ¶
func (c CircleShape) ConnectionPoints() []vectors.Vec2
ConnectionPoints returns the connection points of the circle
func (CircleShape) GetPath ¶
func (c CircleShape) GetPath() []vectors.Vec2
GetPath returns the path of the circle
type HShape ¶
HShape is a shape that looks like an H.
func (HShape) ConnectionPoints ¶
ConnectionPoints returns the connection points of the shape.
type JShape ¶
type JShape struct {
Width, Length, WingWidth float64
}
JShape is a shape that looks like a J.
func (JShape) ConnectionPoints ¶
ConnectionPoints returns the connection points of the shape.
type LShape ¶
type LShape struct {
Width, Length, WingWidth float64
}
LShape is a shape that looks like an L.
func (LShape) ConnectionPoints ¶
ConnectionPoints returns the connection points of the shape.
type Mesh ¶
type Mesh struct { Vertices []vectors.Vec3 // Contains the vertices of the mesh. Triangles []int // Contains the indices of the triangle vertices. }
Mesh represents a 3d mesh that can be exported to a .obj file.
func ExtrudePath ¶
ExtrudePath extrudes a path to a 3D shape.
func TaperPath ¶
TaperPath tapers a path to a 3D shape to generate a roof. NOTE: This is WIP since it doesn't get the angle right.
func (*Mesh) ExportToObj ¶
ExportToObj exports the mesh to a .obj file.
type PlusShape ¶
type PlusShape struct {
Width, Length, WingWidth float64
}
PlusShape is a plus shape.
func (PlusShape) ConnectionPoints ¶
ConnectionPoints returns the connection points of the shape.
type Point ¶
type Point struct {
// contains filtered or unexported fields
}
func GetLineIntersection ¶
Get Line Intersection
@private @param {Point} a1 @param {Point} a2 @param {Point} b1 @param {Point} b2 @param {Point} c @returns {Point}
type Polygon ¶
Polygon is a polygon with multiple points.
func (Polygon) DrawToImage ¶
DrawToImage draws the polygon to an image.
type RectangleShape ¶
type RectangleShape struct {
Width, Length float64
}
RectangleShape is a shape that is a rectangle.
func (RectangleShape) ConnectionPoints ¶
func (r RectangleShape) ConnectionPoints() []vectors.Vec2
ConnectionPoints returns the connection points of the shape.
func (RectangleShape) GetPath ¶
func (r RectangleShape) GetPath() []vectors.Vec2
GetPath returns the path of the shape.
type Shape ¶
type Shape interface { ConnectionPoints() []vectors.Vec2 // Returns the connection points of the shape. GetPath() []vectors.Vec2 // Returns the path of the shape. }
Shape is an interface for shapes.
type SquircleShape ¶
SquircleShape is a shape that is a squircle.
func (SquircleShape) ConnectionPoints ¶
func (s SquircleShape) ConnectionPoints() []vectors.Vec2
ConnectionPoints returns the connection points of the squircle
func (SquircleShape) GetPath ¶
func (s SquircleShape) GetPath() []vectors.Vec2
GetPath returns the path of the squircle For a squircle, we just need to draw quarter circles at each corner, which will give us a squricle.
type TrapezoidShape ¶
type TrapezoidShape struct {
Width, Length, WidthTop float64
}
TrapezoidShape is a shape that is a trapezoid.
func (TrapezoidShape) ConnectionPoints ¶
func (t TrapezoidShape) ConnectionPoints() []vectors.Vec2
ConnectionPoints returns the connection points of the trapezoid
func (TrapezoidShape) GetPath ¶
func (t TrapezoidShape) GetPath() []vectors.Vec2
GetPath returns the path of the trapezoid
type TriangleShape ¶
type TriangleShape struct {
Width, Length float64
}
TriangleShape is a shape that is a triangle.
func (TriangleShape) ConnectionPoints ¶
func (t TriangleShape) ConnectionPoints() []vectors.Vec2
ConnectionPoints returns the connection points of the triangle
func (TriangleShape) GetPath ¶
func (t TriangleShape) GetPath() []vectors.Vec2
GetPath returns the path of the triangle
type UShape ¶
type UShape struct {
Width, Length, WingWidth float64
}
UShape is a shape that looks like a U.
func (UShape) ConnectionPoints ¶
ConnectionPoints returns the connection points of the shape.