├── CHANGELOG.md ├── CREDITS.md ├── LICENSE.md ├── README.md ├── excanvas ├── README ├── excanvas.compiled.js └── excanvas.js ├── mootools ├── mootools-core-1.3.2.js └── mootools-more-1.3.2.1.js ├── package.json ├── rhill-voronoi-core.js ├── rhill-voronoi-core.min.js ├── rhill-voronoi-demo1.html ├── rhill-voronoi-demo2.html ├── rhill-voronoi-demo3.php ├── rhill-voronoi-demo4.html ├── rhill-voronoi-demo5.html └── rhill-voronoi.html /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG.md 2 | 3 | ## 0.99 (22 Apr 2013): 4 | 5 | Now returning all the unique vertices linking all the edges. This addresses 6 | issue #4: https://github.com/gorhill/Javascript-Voronoi/issues/4 7 | 8 | ## 0.98 (25 Jan 2013): 9 | 10 | Added Cell.getBbox() and Cell.pointIntersection() for convenience when using 11 | an external treemap. 12 | 13 | ## 0.97 (21 Jan 2013): 14 | 15 | Merged contribution by Jesse Morgan (https://github.com/morgajel): 16 | Cell.getNeighbourIds() 17 | https://github.com/gorhill/Javascript-Voronoi/commit/4c50f691a301cd6a286359fefba1fab30c8e3b89 18 | 19 | ## 0.96 (26 May 2011): 20 | 21 | Returned diagram.cells is now an array, whereas the index of a cell 22 | matches the index of its associated site in the array of sites passed 23 | to Voronoi.compute(). This allowed some gain in performance. The 24 | 'voronoiId' member is still used internally by the Voronoi object. 25 | The Voronoi.Cells object is no longer necessary and has been removed. 26 | 27 | ## 0.95 (19 May 2011): 28 | 29 | No longer using Javascript array to keep track of the beach sections of 30 | the beachline, now using Red-Black tree. 31 | 32 | The move to a binary tree was unavoidable, as I ran into finite precision 33 | arithmetic problems when I started to use sites with fractional values. 34 | 35 | The problem arose when the code had to find the arc associated with a 36 | triggered Fortune circle event: the collapsing arc was not always properly 37 | found due to finite precision arithmetic-related errors. Using a tree structure 38 | eliminate the need to look-up a beachsection in the array structure 39 | (findDeletionPoint()), and allowed to bring back epsilon down to 1e-9. 40 | 41 | ## 0.91(21 September 2010): 42 | 43 | Lower epsilon from 1e-5 to 1e-4, to fix problem reported at 44 | http://www.raymondhill.net/blog/?p=9#comment-1414 45 | 46 | ## 0.90 (21 September 2010): 47 | 48 | First version. 49 | 50 | -------------------------------------------------------------------------------- /CREDITS.md: -------------------------------------------------------------------------------- 1 | # CREDITS.md 2 | 3 | ## Portions of this software use, depend, or was inspired by the work of: 4 | 5 | ## "Fortune's algorithm" by Steven J. Fortune 6 | 7 | For his clever algorithm to compute Voronoi diagrams. 8 | 9 | http://ect.bell-labs.com/who/sjf/ 10 | 11 | ## "The Liang-Barsky line clipping algorithm in a nutshell!" by Daniel White 12 | 13 | To efficiently clip a line within a rectangle. 14 | 15 | http://www.skytopia.com/project/articles/compsci/clipping.html 16 | 17 | ## "rbtree" by Franck Bui-Huu 18 | 19 | For his RB-tree C implmentation. 20 | 21 | I ported to Javascript the C code of a Red-Black tree implementation by 22 | Franck Bui-Huu, and further altered the code for Javascript efficiency 23 | and to very specifically fit the purpose of holding the beachline (the key 24 | is a variable range rather than an unmutable data point), and unused 25 | code paths have been removed. 26 | 27 | Each node in the tree is actually a beach section on the beachline. Using a 28 | tree structure for the beachline remove the need to lookup the beach section 29 | in the array at removal time, as now a circle event can safely hold a 30 | reference to its associated beach section (thus findDeletionPoint() is no 31 | longer needed). 32 | 33 | This finally take care of nagging finite arithmetic precision issues arising 34 | at lookup time, such that epsilon could be brought down to 1e-9 (from 1e-4). 35 | rhill 2011-05-27: added a 'previous' and 'next' members which keeps track 36 | of previous and next nodes, and remove the need for Beachsection.getPrevious() 37 | and Beachsection.getNext(). 38 | 39 | https://github.com/fbuihuu/libtree/blob/master/rb.c 40 | 41 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (C) 2010-2013 Raymond Hill 2 | https://github.com/gorhill/Javascript-Voronoi 3 | 4 | Licensed under The MIT License 5 | http://en.wikipedia.org/wiki/MIT_License 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Javascript-Voronoi 2 | 3 | A Javascript implementation of Steven J. Fortune's algorithm to 4 | efficiently compute Voronoi diagrams. The Voronoi object's purpose is 5 | to solely compute a Voronoi diagram, it is completely standalone, with 6 | no dependency on external code: it contains no rendering code: that is 7 | left to the user of the library. 8 | 9 | ## Core files 10 | 11 | * rhill-voronoi-core.js 12 | 13 | Where the Voronoi object is implemented. This is a standalone library, there 14 | is no dependency. 15 | 16 | * rhill-voronoi-core.min.js 17 | 18 | The minimized version (using YUI compressor) 19 | 20 | ## Demo files 21 | 22 | * rhill-voronoi-demo1.html 23 | * rhill-voronoi-demo2.html 24 | * rhill-voronoi-demo3.php 25 | * rhill-voronoi-demo4.html 26 | * rhill-voronoi-demo5.html 27 | 28 | Demo pages to demonstrate usage of the Voronoi object. 29 | 30 | * excanvas/* 31 | 32 | Used by demo pages. 33 | 34 | ExplorerCanvas, giving pre-HTML5 Internet Explorer the ability to make sense 35 | of HTML5's canvas element. Pulled from http://code.google.com/p/explorercanvas/ 36 | 37 | * mootools/* 38 | 39 | Used by rhill-voronoi-demo3.php 40 | 41 | * Above pages available at http://www.raymondhill.net/voronoi/ 42 | 43 | 44 | ## Main object: Voronoi 45 | 46 | A Javascript object which allows to compute a Voronoi diagram. 47 | The Voronoi object doesn't render the resulting Voronoi diagram, 48 | the user is responsible for rendering the diagram. 49 | 50 | ## Usage 51 | 52 | Roughly: 53 | 54 | ``` javascript 55 | var voronoi = new Voronoi(); 56 | var bbox = {xl: 0, xr: 800, yt: 0, yb: 600}; // xl is x-left, xr is x-right, yt is y-top, and yb is y-bottom 57 | var sites = [ {x: 200, y: 200}, {x: 50, y: 250}, {x: 400, y: 100} /* , ... */ ]; 58 | 59 | // a 'vertex' is an object exhibiting 'x' and 'y' properties. The 60 | // Voronoi object will add a unique 'voronoiId' property to all 61 | // sites. The 'voronoiId' can be used as a key to lookup the associated cell 62 | // in diagram.cells. 63 | 64 | var diagram = voronoi.compute(sites, bbox); 65 | ``` 66 | 67 | The returned 'diagram' variable is a Javascript object with the 68 | following properties: 69 | 70 | ``` 71 | diagram.vertices 72 | ``` 73 | 74 | An array of unordered, unique ```Voronoi.Vertex``` objects making up the 75 | Voronoi diagram. Each ```Voronoi.Vertex``` object in the list is shared by 76 | many ```Voronoi.Edge``` objects. 77 | 78 | ``` 79 | diagram.edges 80 | ``` 81 | 82 | An array of unordered, unique ```Voronoi.Edge``` objects making up the 83 | Voronoi diagram. ```Voronoi.Edges``` are defined by two vertices, 84 | ```va``` and ```vb```, which vertices are shared by connected edges. This mean 85 | that if you change one vertex belonging to an edge, other connected edges 86 | will also be changed. 87 | 88 | ``` 89 | diagram.cells 90 | ``` 91 | 92 | An array of ```Voronoi.Cell``` objects making up the Voronoi diagram. A 93 | ```Voronoi.Cell``` object might have an empty array of ```halfedges```, 94 | meaning no Voronoi cell could be computed for a particular cell. 95 | 96 | ``` 97 | diagram.execTime 98 | ``` 99 | 100 | The time it took to compute the Voronoi diagram, in milliseconds. 101 | 102 | Added on October 12, 2013: In order to help improve performance, 103 | `Voronoi.recycle()` has been added to allow the recycling of a returned Voronoi 104 | diagram. Usage: 105 | 106 | ``` javascript 107 | var diagram; 108 | ... 109 | 110 | // some kind of loop starting here (whether outright or through a timer) 111 | ... 112 | 113 | voronoi.recycle(diagram); 114 | // diagram.vertices, diagram.edges and diagram.cells can no longer be used! 115 | diagram = voronoi.compute(sites, bbox); 116 | 117 | // do stuff with content of `diagram` 118 | ... 119 | ``` 120 | 121 | This new method helps performance significantly when re-computing a Voronoi 122 | diagram, as it saves on memory allocation, and associated garbage collection. 123 | 124 | ## Public objects 125 | 126 | ``` 127 | Voronoi 128 | ``` 129 | 130 | The ```Voronoi``` object which computes a Voronoi diagram. 131 | 132 | ``` 133 | Voronoi.Vertex 134 | ``` 135 | 136 | * ```x```: no explanation required. 137 | 138 | * ```y```: no explanation required. 139 | 140 | ``` 141 | Voronoi.Edge 142 | ``` 143 | 144 | * ```lSite```: the Voronoi site object at the left of this ```Voronoi.Edge``` 145 | object. The site object is just a reference to a site in the array of sites 146 | supplied by the user when ```Voronoi.compute()``` was called. 147 | 148 | * ```rSite```: the Voronoi site object at the right of this ```Voronoi.Edge``` 149 | object (can be null, when this is a border edge). The site object is just a 150 | reference to a site in the array of sites supplied by the user when 151 | ```Voronoi.compute()``` was called. 152 | 153 | * ```va```: a ```Voronoi.Vertex``` object with an ```x``` and a ```y``` 154 | property defining the start point (relative to the Voronoi site on 155 | the left) of this ```Voronoi.Edge``` object. 156 | 157 | * ```vb```: a ```Voronoi.Vertex``` object with an ```x``` and a ```y``` 158 | property defining the end point (relative to Voronoi site on the left) 159 | of this ```Voronoi.Edge``` object. 160 | 161 | ``` 162 | Voronoi.Cell 163 | ``` 164 | 165 | * ```site```: the Voronoi site object associated with the Voronoi cell. 166 | 167 | * ```halfedges```: an array of ```Voronoi.Halfedge``` objects, ordered 168 | counterclockwise, defining the polygon for this Voronoi cell. 169 | 170 | ``` 171 | Voronoi.Halfedge 172 | ``` 173 | 174 | * ```site```: the Voronoi site object owning this ```Voronoi.Halfedge``` 175 | object. 176 | 177 | * ```edge```: a reference to the unique ```Voronoi.Edge``` object underlying 178 | this ```Voronoi.Halfedge``` object. 179 | 180 | * ```getStartpoint()```: a method returning a ```Voronoi.Vertex``` of the start 181 | point of this halfedge. Keep in mind halfedges are always counterclockwise. 182 | 183 | * ```getEndpoint()```: a method returning a ```Voronoi.Vertex``` object with 184 | an ```x``` and a ```y``` property for the end point of this halfedge. Keep in 185 | mind halfedges are always counterclockwise. 186 | 187 | ## License 188 | 189 | Copyright (c) 2010-2013 Raymond Hill 190 | https://github.com/gorhill/Javascript-Voronoi 191 | 192 | Licensed under The MIT License 193 | http://en.wikipedia.org/wiki/MIT_License 194 | 195 | Permission is hereby granted, free of charge, to any person obtaining a copy 196 | of this software and associated documentation files (the "Software"), to deal 197 | in the Software without restriction, including without limitation the rights 198 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 199 | copies of the Software, and to permit persons to whom the Software is 200 | furnished to do so, subject to the following conditions: 201 | 202 | The above copyright notice and this permission notice shall be included in 203 | all copies or substantial portions of the Software. 204 | 205 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 206 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 207 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 208 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 209 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 210 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 211 | THE SOFTWARE. 212 | -------------------------------------------------------------------------------- /excanvas/README: -------------------------------------------------------------------------------- 1 | ExplorerCanvas 2 | Copyright 2006 Google Inc. 3 | 4 | ------------------------------------------------------------------------------- 5 | DESCRIPTION 6 | 7 | Firefox, Safari and Opera 9 support the canvas tag to allow 2D command-based 8 | drawing operations. ExplorerCanvas brings the same functionality to Internet 9 | Explorer; web developers only need to include a single script tag in their 10 | existing canvas webpages to enable this support. 11 | 12 | 13 | ------------------------------------------------------------------------------- 14 | INSTALLATION 15 | 16 | Include the ExplorerCanvas tag in the same directory as your HTML files, and 17 | add the following code to your page, preferably in the
tag. 18 | 19 | 20 | 21 | If you run into trouble, please look at the included example code to see how 22 | to best implement this -------------------------------------------------------------------------------- /excanvas/excanvas.compiled.js: -------------------------------------------------------------------------------- 1 | // Copyright 2006 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | document.createElement("canvas").getContext||(function(){var s=Math,j=s.round,F=s.sin,G=s.cos,V=s.abs,W=s.sqrt,k=10,v=k/2;function X(){return this.context_||(this.context_=new H(this))}var L=Array.prototype.slice;function Y(b,a){var c=L.call(arguments,2);return function(){return b.apply(a,c.concat(L.call(arguments)))}}var M={init:function(b){if(/MSIE/.test(navigator.userAgent)&&!window.opera){var a=b||document;a.createElement("canvas");a.attachEvent("onreadystatechange",Y(this.init_,this,a))}},init_:function(b){b.namespaces.g_vml_|| 15 | b.namespaces.add("g_vml_","urn:schemas-microsoft-com:vml","#default#VML");b.namespaces.g_o_||b.namespaces.add("g_o_","urn:schemas-microsoft-com:office:office","#default#VML");if(!b.styleSheets.ex_canvas_){var a=b.createStyleSheet();a.owningElement.id="ex_canvas_";a.cssText="canvas{display:inline-block;overflow:hidden;text-align:left;width:300px;height:150px}g_vml_\\:*{behavior:url(#default#VML)}g_o_\\:*{behavior:url(#default#VML)}"}var c=b.getElementsByTagName("canvas"),d=0;for(;d