// © Michiel Kamermans 2010 - CC 3.0 license (AT-NC) /** * This models a mixed polygon/polybezier shape. * Visualisation can be done by transforming it into * an SVG element, of the form: * * * * * */ class PolyShape { // if true, also shows all the bounding boxes boolean debug = false; void setDebug(boolean d) { debug = d; } boolean toggleBBox() { setDebug(!debug); return debug; } ArrayList bounds = new ArrayList(); // administrative int width = 0; int height = 0; // reference from pathshape ArrayList points; // SVG data, initialised in getSVG() String svgheader; String path_d = "M 0 0 Z"; String path; String svgfooter; // SVG document XMLElement svgxml; // PShape based on SVG document PShape svg; /** * Constructor sets up a left instruction list, buttress list, right instruction list, and * a template SVG object */ PolyShape(ArrayList points) { this.points = points; getSVG(); svgxml = new XMLElement(svgheader + path + svgfooter); svg = new PShapeSVG(svgxml); } /** * Add a normal point to the polyshape. */ void addPoint(int lx, int ly, int rx, int ry) { updateSVG(max(lx,rx),max(ly,ry)); } /** * Update the SVG data (header width/height and path) */ void updateSVG(int x, int y) { if(x>width) { width = x; } if(y>height) { height = y; } svgxml.setAttribute("width", ""+width); svgxml.setAttribute("height", ""+height); svgxml.setAttribute("viewBox","0 0 "+width+" "+height); updatePath(); } /** * Update the string for the "d" parameter for the SVG path */ void updatePath() { bounds = new ArrayList(); path_d = ""; for(int d=0, end=points.size(); d=0; d--) { Point pt = (Point) points.get(d); ArrayList instructions = pt.getRightSVG(); for(int a=0, end=instructions.size(); a\n"; path = "\n"; svgfooter = "\n"; return svgheader + path + svgfooter; } /** * Draw this polyshape */ void draw() { shape(svg); if(debug) { noFill(); stroke(255,0,0,50); for(int v=0, end=bounds.size(); v