gengeometry

package
v0.0.0-...-ab92d4e Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

gengeometry

DO NOT USE!

This package will contain useful helper functions for working with polygons, lines, and all that.

Splitting a polygon / path

Simple algorithm for splitting a polygon along a line into different polygons.

alt text

Path and mesh generation

Really shitty path generation... this is mainly for experimenting with procedural generation of building shapes and all that. Not very good, but it works.

alt text

Documentation

Index

Constants

View Source
const (
	RotateRight = iota
	RotateLeft
)

Variables

View Source
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,
		},
	}
)

Functions

func Abs

func Abs(x float64) float64

func AngleOfSide

func AngleOfSide(polygon []vectors.Vec2, i int) float64

AngleOfSide returns the absolute angle of the normal of the side of the polygon with the given index.

func CenterOfPath

func CenterOfPath(points []vectors.Vec2) vectors.Vec2

CenterOfPath calculates the center of a polygon (by averaging all points).

func ContainsPoint

func ContainsPoint(polygon []float64, pointX, pointY float64) bool

*

  • 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 DrawLine

func DrawLine(img *image.RGBA, a, b vectors.Vec2, color color.RGBA, scale float64)

func GetBresenhamLine

func GetBresenhamLine(pos1, pos2 vectors.IVec2) (points []vectors.IVec2)

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 GetPathSides

func GetPathSides(path []vectors.Vec2) []vectors.Segment

func InRectangle

func InRectangle(a, b, c Point) bool

*

  • In Rectangle *
  • @private
  • @param {Point} a
  • @param {Point} b
  • @param {Point} c
  • @return {boolean}

func IsConvex

func IsConvex(polygon []float64) bool

*

  • 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

func IsSimple(polygon []float64) bool

*

  • 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

func PointInTriangle(px, py, ax, ay, bx, by, cx, cy float64) bool

*

  • 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 PolygonArrayToPolygon

func PolygonArrayToPolygon(polygon []float64) []vectors.Vec2

*

  • [ v.x, v.y, v.x, v.y ]...

func PolygonToPolygonArray

func PolygonToPolygonArray(polygon []vectors.Vec2) []float64

*

  • [ v.x, v.y, v.x, v.y ]...

func Reverse

func Reverse(polygon []float64) []float64

*

  • 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

func SavePathAsPNG(path []vectors.Vec2, filename string, scale float64) error

SavePathAsPNG saves the path as a PNG image.

func ShrinkPath

func ShrinkPath(points []vectors.Vec2, shrink float64) []vectors.Vec2

ShrinkPath shrinks a polygon by a given factor around its center.

func SimplifyPolyline

func SimplifyPolyline(points []vectors.Vec2, tolerance float64, highestQuality bool) []vectors.Vec2

SimplifyPolyline simplifies a polyline. NOTE: This code is based on: https://github.com/mourner/simplify-js

func Slice

func Slice(polygon []vectors.Vec2, startX, startY, endX, endY float64) ([][]vectors.Vec2, bool)

func StraightSkeleton

func StraightSkeleton(points []vectors.Vec2, shrink, spacing float64) []vectors.Vec2

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

func TranslatePath(path []vectors.Vec2, offset vectors.Vec2) []vectors.Vec2

TranslatePath translates a path by the given offset.

func Triangulate

func Triangulate(polygon []vectors.Vec2) ([]int, error)

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

func TriangulatePolyk(polygon []float64) []int

*

  • 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 AABB

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

func GetAABB

func GetAABB(polygon []float64) AABB

*

  • Returns the Axis-aligned Bounding Box of polygon *
  • @param {number[]} polygon [x1, y1, x2, y2...]
  • @returns {AABB}
  • @example
  • //={x:0, y:0, width:0, height:0}

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) Print

func (g BlockyShape) Print()

Print prints the shape to the console

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 Edge

type Edge struct {
	Index int
	A     *Point
	B     *Point
}

type HShape

type HShape struct {
	Width, Length float64
	WingWidth     float64
}

HShape is a shape that looks like an H.

func (HShape) ConnectionPoints

func (h HShape) ConnectionPoints() []vectors.Vec2

ConnectionPoints returns the connection points of the shape.

func (HShape) GetPath

func (h HShape) GetPath() []vectors.Vec2

GetPath returns the path of the shape.

type ISC

type ISC struct {
	Edge  *Edge
	Point *Point
	// contains filtered or unexported fields
}

type JShape

type JShape struct {
	Width, Length, WingWidth float64
}

JShape is a shape that looks like a J.

func (JShape) ConnectionPoints

func (j JShape) ConnectionPoints() []vectors.Vec2

ConnectionPoints returns the connection points of the shape.

func (JShape) GetPath

func (j JShape) GetPath() []vectors.Vec2

GetPath returns the path of the shape.

type LShape

type LShape struct {
	Width, Length, WingWidth float64
}

LShape is a shape that looks like an L.

func (LShape) ConnectionPoints

func (l LShape) ConnectionPoints() []vectors.Vec2

ConnectionPoints returns the connection points of the shape.

func (LShape) GetPath

func (l LShape) GetPath() []vectors.Vec2

GetPath returns the path 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

func ExtrudePath(path []vectors.Vec2, height float64) (*Mesh, error)

ExtrudePath extrudes a path to a 3D shape.

func TaperPath

func TaperPath(path []vectors.Vec2, height float64) (*Mesh, error)

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) AddMesh

func (m *Mesh) AddMesh(mesh *Mesh, position vectors.Vec3)

AddMesh adds a mesh to the current mesh (at a given vertical offset).

func (*Mesh) ExportToObj

func (m *Mesh) ExportToObj(filename string)

ExportToObj exports the mesh to a .obj file.

func (*Mesh) Translate

func (m *Mesh) Translate(v vectors.Vec2)

Translate translates the mesh by a given vector in the 2d plane.

type PlusShape

type PlusShape struct {
	Width, Length, WingWidth float64
}

PlusShape is a plus shape.

func (PlusShape) ConnectionPoints

func (p PlusShape) ConnectionPoints() []vectors.Vec2

ConnectionPoints returns the connection points of the shape.

func (PlusShape) GetPath

func (p PlusShape) GetPath() []vectors.Vec2

GetPath returns the path of the shape.

type Point

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

func GetLineIntersection

func GetLineIntersection(a1, a2, b1, b2, c Point) (Point, bool)

Get Line Intersection

@private @param {Point} a1 @param {Point} a2 @param {Point} b1 @param {Point} b2 @param {Point} c @returns {Point}

type Polygon

type Polygon struct {
	Points []vectors.Vec2
}

Polygon is a polygon with multiple points.

func (Polygon) DrawToImage

func (p Polygon) DrawToImage(img *image.RGBA, color color.RGBA, scale float64)

DrawToImage draws the polygon to an image.

func (*Polygon) Split

func (p *Polygon) Split(l vectors.Segment) []*Polygon

Split splits a polygon into multiple polygons by cutting it along a line. NOTE: This is buggy, especially when an intersection point is on a vertex.

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

type SquircleShape struct {
	Width, Length, Radius float64
	Steps                 int
}

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

func (u UShape) ConnectionPoints() []vectors.Vec2

ConnectionPoints returns the connection points of the shape.

func (UShape) GetPath

func (u UShape) GetPath() []vectors.Vec2

GetPath returns the path of the shape.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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