github.com/joshvarga/voronoi@v0.0.0-20180211004454-2fd26fbdfffb/cell.go (about)

     1  // Copyright 2013 Przemyslaw Szczepaniak.
     2  // MIT License: See https://github.com/gorhill/Javascript-Voronoi/LICENSE.md
     3  
     4  // Author: Przemyslaw Szczepaniak (przeszczep@gmail.com)
     5  // Port of Raymond Hill's (rhill@raymondhill.net) javascript implementation 
     6  // of Steven Forune's algorithm to compute Voronoi diagrams
     7  
     8  package voronoi
     9  
    10  import "sort"
    11  
    12  // Cell of voronoi diagram
    13  type Cell struct {
    14  	// Site of the cell
    15  	Site Vertex
    16  	// Array of halfedges sorted counterclockwise
    17  	Halfedges []*Halfedge
    18  }
    19  
    20  func newCell(site Vertex) *Cell {
    21  	return &Cell{Site: site}
    22  }
    23  
    24  func (t *Cell) prepare() int {
    25  	halfedges := t.Halfedges
    26  	iHalfedge := len(halfedges) - 1
    27  
    28  	for ; iHalfedge >= 0; iHalfedge-- {
    29  		edge := halfedges[iHalfedge].Edge
    30  
    31  		if edge.Vb.Vertex == NO_VERTEX || edge.Va.Vertex == NO_VERTEX {
    32  			halfedges[iHalfedge] = halfedges[len(halfedges)-1]
    33  			halfedges = halfedges[:len(halfedges)-1]
    34  		}
    35  	}
    36  
    37  	sort.Sort(halfedgesByAngle{halfedges})
    38  	t.Halfedges = halfedges
    39  	return len(halfedges)
    40  }