voronoi

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCircleSitesOrder              = errors.New("circle sites wrong order")
	ErrNoCircleFoundConnectionPoints = errors.New("no circle found connecting points")
)

All kind of errors

Functions

func GetParabolaABC

func GetParabolaABC(focus *Site, yOfDirectrix int) (float64, float64, float64)

GetParabolaABC returns the a, b and c coefficients of the standard form of a parabola equation, given only x and y of the focus and y of the directrix. Math behind this is explained at https://math.stackexchange.com/q/2700061/543428.

func GetXOfInternalNode

func GetXOfInternalNode(node *Node, directrix int) (int, error)

GetXOfInternalNode returns the x of the intersection of the two parabola arcs below an internal node.

func GetXOfIntersection

func GetXOfIntersection(left *Node, right *Node, directrix int) (int, error)

GetXOfIntersection returns the x of the intersection of two parabola arcs.

func GetYByX

func GetYByX(focus *Site, x int, directrix int) int

GetYByX calculates the Y value for the parabola with the given focus and directrix (the sweep line)

Types

type CircleEvents

type CircleEvents []*Event

CircleEvents represents a list of pointers to circle events in which the node participates.

func (*CircleEvents) HasEvent

func (ce *CircleEvents) HasEvent(event *Event) bool

HasEvent tests if the node has a pointer to the given event.

func (*CircleEvents) RemoveEvent

func (ce *CircleEvents) RemoveEvent(event *Event)

RemoveEvent removes the given event from the list.

type DCEL

type DCEL struct {
	Vertices  []*Vertex
	Faces     []*Face
	HalfEdges []*HalfEdge
}

DCEL stores the state of the data structure and provides methods for linking of three sets of objects: vertecies, edges and faces.

func NewDCEL

func NewDCEL() *DCEL

NewDCEL creates a new DCEL data structure.

func (*DCEL) NewEdge

func (d *DCEL) NewEdge(face1, face2 *Face, vertex *Vertex) (*HalfEdge, *HalfEdge)

NewEdge creates a pair of half-edges, one of them starting at the given vertex.

func (*DCEL) NewFace

func (d *DCEL) NewFace() *Face

NewFace creates a new face and stores it in the DCEL structure.

func (*DCEL) NewHalfEdge

func (d *DCEL) NewHalfEdge(face *Face, vertex *Vertex, twin *HalfEdge) *HalfEdge

NewHalfEdge creates a new half-edge starting at the given vertex and stores it in the structure.

func (*DCEL) NewVertex

func (d *DCEL) NewVertex(x, y int) *Vertex

NewVertex creates a new vertex with the given coordinates and stores it in the structure.

type Event

type Event struct {
	X, Y int // X and Y of the site, or X and Y of the bottom point of the circle.

	EventType EventType // The type of the event. Site = 0 and Circle = 1.
	Site      *Site     // Pointer to the related site. Only relevant for site events.
	Node      *Node     // The related arc node. Only relevant for circle events.
	Radius    int       // Radius of the circle.
	// contains filtered or unexported fields
}

Event represents a site or circle event.

type EventQueue

type EventQueue []*Event

A EventQueue is a priority queue that implements heap.Interface and holds Events.

func NewEventQueue

func NewEventQueue(sites SiteSlice) EventQueue

NewEventQueue creates a new queue and initializes it with events for the given list of sites.

func (EventQueue) Len

func (pq EventQueue) Len() int

Len returns the number of events in the queue.

func (EventQueue) Less

func (pq EventQueue) Less(i, j int) bool

Less compares two events and is needed as implementation of the Sort interface.

func (*EventQueue) Pop

func (pq *EventQueue) Pop() interface{}

Pop removes the last element from the queue and sets its index to -1.

func (*EventQueue) Push

func (pq *EventQueue) Push(x interface{})

Push appends an item to the queue and reorders items if necessary.

func (*EventQueue) Remove

func (pq *EventQueue) Remove(event *Event)

Remove removes the element with the specified index from the queue.

func (EventQueue) String

func (pq EventQueue) String() string

func (EventQueue) Swap

func (pq EventQueue) Swap(i, j int)

Swap swaps two events, updating their index in the slice.

type EventType

type EventType int

EventType represent the type of the event - either site or circle event.

const (
	EventSite   EventType = 0
	EventCircle EventType = 1
)

All types of events

type Face

type Face struct {
	HalfEdge *HalfEdge
	ID       int64
	Data     interface{}
}

Face represents a subdivision of the plane. Each face has a pointer to one of the half edges at its boundary. Faces can have user specified IDs and annotations.

func (*Face) String

func (f *Face) String() string

String implement string function

type HalfEdge

type HalfEdge struct {
	Target *Vertex
	Face   *Face
	Twin   *HalfEdge
	Next   *HalfEdge
	Prev   *HalfEdge
	Data   interface{}
}

HalfEdge represents one of the half-edges in an edge pair. Each half-edge has a pointer to its target vertex (origin), the face to which it belongs, its twin edge (a reversed half-edge, pointing to a neighbour face) and pointers to the next and previous half-edges at the boundary of its face. Half-edges can also store user data.

func (*HalfEdge) IsClosed

func (he *HalfEdge) IsClosed() bool

IsClosed returns true if both half-edges in the pair have a target vertex.

func (*HalfEdge) String

func (he *HalfEdge) String() string

String implement string function

type Node

type Node struct {
	// Site is the focus of the parabola arc (the site which created the parabola).
	// Not used for internal nodes.
	Site *Site
	// Events hold pointers to circle events, in which this arc is the left most, middle or right-most arc.
	// Not used for internal nodes.
	LeftEvents, MiddleEvents, RightEvents CircleEvents
	// Pointer to the parent node.
	Parent *Node
	// Left stores a subtree of arcs with smaller X values.
	Left *Node
	// Right stores a subtree of arcs with larger X values.
	Right *Node

	LeftEdges  []*HalfEdge
	RightEdges []*HalfEdge
}

Node represent an element in a binary tree. Each Leaf in the tree represents an arc of a parabola (part of parabola), that lies on the beach line. Leaf nodes store the site that created the arc and pointers to the circle events associated with it. Internal nodes represent intersections (breakpoints) between the arcs and store no values.

func (*Node) AddLeftEvent

func (n *Node) AddLeftEvent(event *Event)

AddLeftEvent pushes a pointer to an event for which this is the left-most node.

func (*Node) AddMiddleEvent

func (n *Node) AddMiddleEvent(event *Event)

AddMiddleEvent pushes a pointer to an event for which this is the left-most node.

func (*Node) AddRightEvent

func (n *Node) AddRightEvent(event *Event)

AddRightEvent pushes a pointer to an event for which this is the left-most node.

func (*Node) FirstArc

func (n *Node) FirstArc() *Node

FirstArc returns the left-most arc (leaf) in the tree.

func (*Node) HasEvent

func (n *Node) HasEvent(event *Event) bool

HasEvent tests if the node has a pointer to the given event.

func (*Node) IsLeaf

func (n *Node) IsLeaf() bool

IsLeaf returns true if the TreeNode has no left or right children. A single root node is also considered a leaf.

func (*Node) LastArc

func (n *Node) LastArc() *Node

LastArc returns the right-most arc (leaf) in the tree.

func (*Node) NextArc

func (n *Node) NextArc() *Node

NextArc returns the node for the next arc.

func (*Node) NextChildArc

func (n *Node) NextChildArc() *Node

NextChildArc returns the node for the next arc.

func (*Node) PrevArc

func (n *Node) PrevArc() *Node

PrevArc returns the node for the previous arc.

func (*Node) PrevChildArc

func (n *Node) PrevChildArc() *Node

PrevChildArc returns the node for the previous arc.

func (*Node) RemoveEvent

func (n *Node) RemoveEvent(event *Event)

RemoveEvent removes the given event from the lists of the node.

func (*Node) String

func (n *Node) String() string

String method from https://github.com/golang/tour/blob/master/tree/tree.go

type Site

type Site struct {
	X, Y int
	ID   int64
	Face *Face // Pointer to the DCEL face corresponding to this site
	Data interface{}
}

Site is a prerequisute for computing a voronoi diagram. Site is the point (also called seed or generator) in a voronoi diagram, around which a cell (subset of the plane) is formed, with such a property that every point in the cell is closer to this site than any other site.

func (Site) String

func (s Site) String() string

type SiteSlice

type SiteSlice []Site

SiteSlice is a slice of Site values, sortable by Y

func (SiteSlice) Len

func (s SiteSlice) Len() int

Len sort implementation

func (SiteSlice) Less

func (s SiteSlice) Less(i, j int) bool

Less sort implementation

func (SiteSlice) Swap

func (s SiteSlice) Swap(i, j int)

Swap sort implementation

type Vertex

type Vertex struct {
	X, Y     int
	HalfEdge *HalfEdge
	Data     interface{}
}

Vertex represents a node in the DCEL structure. Each vertex has 2D coordinates and a pointer to an arbitrary half edge that has this vertex as its target (origin). Annotations (user data) can be stored in the Data field.

func (*Vertex) String

func (v *Vertex) String() string

String implement string function

type Voronoi

type Voronoi struct {
	Bounds       shapes.Box
	Sites        SiteSlice
	EventQueue   EventQueue
	ParabolaTree *Node
	SweepLine    int // tracks the current position of the sweep line; updated when a new site is added.
	DCEL         *DCEL
}

Voronoi implements Fortune's algorithm for voronoi diagram generation.

func New

func New(sites SiteSlice, bounds shapes.Box) *Voronoi

New creates a voronoi diagram generator for a list of sites and within the specified bounds.

func NewFromPoints

func NewFromPoints(points []shapes.Point, bounds shapes.Box) *Voronoi

NewFromPoints creates a voronoi diagram generator for a list of points within the specified bounds.

func (*Voronoi) CloseTwins

func (v *Voronoi) CloseTwins(list []*HalfEdge, vertex *Vertex)

CloseTwins adds a vertex to the specified edges.

func (*Voronoi) Generate

func (v *Voronoi) Generate()

Generate runs the algorithm for the given sites and bounds, creating a voronoi diagram.

func (*Voronoi) GetFaceHalfEdges

func (v *Voronoi) GetFaceHalfEdges(face *Face) []*HalfEdge

GetFaceHalfEdges returns the half-edges that form the boundary of a face (cell).

func (*Voronoi) GetFaceVertices

func (v *Voronoi) GetFaceVertices(face *Face) []*Vertex

GetFaceVertices returns the vertices that form the boundary of a face (cell), sorted in counter-clockwise order.

func (*Voronoi) HandleNextEvent

func (v *Voronoi) HandleNextEvent()

HandleNextEvent processes the next event from the internal event queue. Used from the player application while developing the algorithm.

func (*Voronoi) ReorderFaceEdges

func (v *Voronoi) ReorderFaceEdges(face *Face)

ReorderFaceEdges reorders face half-edges in a clockwise way, while also removing duplicates.

func (*Voronoi) Reset

func (v *Voronoi) Reset()

Reset clears the state of the voronoi generator.

func (*Voronoi) ToPolyhedrons

func (v *Voronoi) ToPolyhedrons() []shapes.Polyhedron

ToPolyhedrons return polyhedrons from given voronoi algorithm

Jump to

Keyboard shortcuts

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