forked from ff6347/Illustrator-Javascript-Voronoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voronoi.jsx
207 lines (152 loc) · 5.72 KB
/
voronoi.jsx
1
// voronoi.jsx// draws voronoi diagrams in illustrator// using the javascript port from// gorhill --> https://github.com/gorhill // you can find the code here:// https://github.com/gorhill/Javascript-Voronoi// under the same license as this one// the illustrator usage can be found here:// https://github.com/fabiantheblind/Javascript-Voronoi// the direct download is here: // https://github.com/fabiantheblind/Javascript-Voronoi/zipball/master//~ Copyright (c) 2012 Fabian "fabiantheblind" Morón Zirfas//~ Permission is hereby granted, free of charge, to any person obtaining a copy of this//~ software and associated documentation files (the "Software"), to deal in the Software //~ without restriction, including without limitation the rights to use, copy, modify, //~ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to //~ permit persons to whom the Software is furnished to do so, subject to the following //~ conditions://~ The above copyright notice and this permission notice shall be included in all copies //~ or substantial portions of the Software.//~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, //~ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A //~ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT //~ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF //~ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE //~ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.//~ see also http://www.opensource.org/licenses/mit-license.php#include "rhill-voronoi-core.js"main();function main(){ // this is the basic example by gorhill // var sites = [{x:300,y:300}, {x:100,y:100}, {x:200,y:500}, {x:250,y:450}, {x:500,y:150}]; // xl, xr means x left, x right // yt, yb means y top, y bottom var bbox = {xl:0, xr:800, yt:0, yb:600}; // pass an object which exhibits xl, xr, yt, yb properties. The bounding // box will be used to connect unbound edges, and to close open cells // $.writeln (util_inspect_properties(result.cells[0].site)); // render, further analyze, etc. var docPreset = new DocumentPreset;docPreset.units = RulerUnits.Pixels;docPreset.width = bbox.xr;docPreset.height = bbox.yb;var doc = app.documents.addDocument("newFile",docPreset);var tf = doc.textFrames.add(); tf.contents = "VORONOI"; tf.paragraphs[0].characterAttributes.size = 72; // try{ tf.paragraphs[0].characterAttributes.textFont = app.textFonts.getByName("Unibody8SmallCaps-Regular"); // }catch(err){ // alert("the desired font is not available"); // } tf.paragraphs[0].paragraphAttributes.justification = Justification.CENTER; var it = tf.createOutline(); it.translate((bbox.xr / 2),(bbox.yb / 2) ); redraw(); release(doc); redraw(); var sites = get_pathPoints(doc); var voronoi = new Voronoi(); result = voronoi.compute(sites, bbox); var lyr = doc.layers.add(); doc.layers[1].locked = true; doc.layers[1].visible = false;//~ //~ var doc = app.documents.add(//~ DocumentColorSpace.RGB,//~ cm2pt (80),//~ cm2pt (60),//~ 1,//~ DocumentArtboardLayout.GridByRow,//~ 20.0,//~ 3);//~ draw_cells (result); draw_edges (doc, result);} function draw_edges(doc, result){ var edges = new Array();for(var i in result.edges){ var pt = new Array(); var va = new Array(result.edges[i].va.x, result.edges[i].va.y); var vb = new Array(result.edges[i].vb.x, result.edges[i].vb.y); pt.push(new Array(va[0],va[1])); pt.push(new Array(vb[0],vb[1])); edges.push({"path":pt}); }for(var k in edges){var path = doc.pathItems.add();redraw(); path.setEntirePath(edges[k].path); }var diam = 3;for(var l in result.cells){ var top = -result.cells[l].site.y; var left = result.cells[l].site.x; redraw(); var ell = doc.pathItems.ellipse( -top + diam/2, left -diam/2, diam, diam, true,true ); }}function cm2pt(val){ var result = val * 28.346; return result; }function get_pathPoints(doc){ // var sites = [{x:300,y:300}, {x:100,y:100}, {x:200,y:500}, {x:250,y:450}, {x:500,y:150}]; var list = new Array(); for(var j = 0; j < doc.pathItems.length;j++){ var path = doc.pathItems[j]; var points = path.pathPoints; for (var i = 0; i < points.length; i++) { var p = points[i]; var a = p.anchor; var px = a[0]; var py = a[1]; list.push({x:px,y:py}); };}return list;}// ------------ release compound Paths ------------// maybee try this// http://forums.adobe.com/message/2140054function release(doc){while(doc.compoundPathItems.length > 0){ try{release_compoundPath(doc);}catch(e){alert(e);} }}function release_compoundPath(doc){ redraw();var cp = doc.compoundPathItems[0];var p = cp.pathItems[0]; p.move(doc, ElementPlacement.PLACEATEND);}// ------------ insept properties and methods ------------// the functions below are// by Peter the Magnificant Kahrel// http://www.kahrel.plus.com/indesign/scriptui.html// look under "Displaying properties and methods"function util_inspect_properties (f) {$.writeln (f.reflect.name);var props = f.reflect.properties;var array = [];for (var i = 0; i < props.length; i++)try {array.push (props[i].name + ": " + f[props[i].name])} catch (_){} array.sort ();$.writeln (array.join ("\r"));}function util_inspect_methods (m) {var props = m.reflect.methods.sort(); $.writeln ("\rMethods");for (var i = 0; i < props.length; i++)$.writeln (props[i].name);}// ------------ ------------