feltron
YaBB Newbies
Offline
Posts: 6
Re: Help with bezier polygons
Reply #8 - Jun 9th , 2010, 8:27am
Thanks so much Cedric! I successfully took your fixes and ported them into a class… now to insert a little data! [code] // GLOBAL VARIABLES int margin = 20; int pointDiameter = 5; //marker point diameter // POLYGON 1 float[] points1 = { 50,50,50,50,50}; float tension1; Polygon myPolygon1 = new Polygon(points1, color(0,360,360)); // first polygon with array as constructor argument // POLYGON 2 float[] points2 = { 70,70,70}; float tension2; Polygon myPolygon2 = new Polygon(points2, color(360,0,360)); // first polygon with array as constructor argument // POLYGON 3 float[] points3 = { 25,25,25,25,25,25,25,25}; float tension3; Polygon myPolygon3 = new Polygon(points3, color(360,360,360)); // first polygon with array as constructor argument // SETUP void setup(){ size(1000,1000); noFill(); stroke(255); smooth(); colorMode(HSB,360); } // DRAW void draw(){ background(0); tension1 = mouseX/5; myPolygon1.drawPoints(); myPolygon1.drawCurve(tension1); tension2 = mouseX/2; myPolygon2.drawPoints(); myPolygon2.drawCurve(tension2); tension3 = mouseX/10; myPolygon3.drawPoints(); myPolygon3.drawCurve(tension3); } // POLYGON CLASS class Polygon { //GLOBAL POLYGON VARIABLES float points[]; float polyAngles; float xVertex; float yVertex; float xVertex2; float yVertex2; float CX1; float CY1; float CX2; float CY2; color c; //CONSTRUCTOR Polygon(float[] inputPoints, color tempC) { points = inputPoints; c = tempC; } void drawCurve(float tension){ polyAngles = 360 / points.length; xVertex = width/2 + cos(radians(0)) * ((points[0]/100)*(width/2-margin)); yVertex = height/2 + sin(radians(0)) * ((points[0]/100)*(height/2-margin)); beginShape(); stroke(c); vertex(xVertex, yVertex); // origin vertex for (int i = 1; i<points.length; i++){ xVertex = width/2 + cos(radians(i * polyAngles)) * ((points[i]/100)*(width/2-margin)); yVertex = height/2 + sin(radians(i * polyAngles)) * ((points[i]/100)*(height/2-margin)); xVertex2 = width/2 + cos(radians((i-1) * polyAngles)) * ((points[i-1]/100)*(width/2-margin)); yVertex2 = height/2 + sin(radians((i-1) * polyAngles)) * ((points[i-1]/100)*(height/2-margin)); CX2 = xVertex + cos(radians(i * polyAngles -90)) * tension; CY2 = yVertex + sin(radians(i * polyAngles -90)) * tension; CX1 = xVertex2 + cos(radians((i-1) * polyAngles +90)) * tension; CY1 = yVertex2 + sin(radians((i-1) * polyAngles +90)) * tension; bezierVertex( CX1, CY1, CX2, CY2, xVertex, yVertex); // angle vertices ellipse(CX1, CY1, pointDiameter, pointDiameter); ellipse(CX2 , CY2 , pointDiameter, pointDiameter); } xVertex = width/2 + cos(radians(0 * polyAngles)) * ((points[0]/100)*(width/2-margin)); yVertex = height/2 + sin(radians(0 * polyAngles)) * ((points[0]/100)*(height/2-margin)); xVertex2 = width/2 + cos(radians(((points.length-1)) * polyAngles)) * ((points[(points.length-1)]/100)*(width/2-margin)); yVertex2 = height/2 + sin(radians(((points.length-1)) * polyAngles)) * ((points[(points.length-1)]/100)*(height/2-margin)); CX2 = xVertex + cos(radians(0 * polyAngles -90)) * tension; CY2 = yVertex + sin(radians(0 * polyAngles -90)) * tension; CX1 = xVertex2 + cos(radians(((points.length-1)) * polyAngles +90)) * tension; CY1 = yVertex2 + sin(radians(((points.length-1)) * polyAngles +90)) * tension; bezierVertex( CX1, CY1, CX2, CY2, xVertex, yVertex); // origin vertex to close endShape(); ellipse(CX1, CY1, pointDiameter, pointDiameter); ellipse(CX2 , CY2 , pointDiameter, pointDiameter); } //POINT DRAWING FUNCTION void drawPoints() { stroke(0,0,360); polyAngles = 360 / points.length; ellipse(width/2, height/2, pointDiameter, pointDiameter); //center point stroke(c); for (int i = 0; i<points.length; i++){ xVertex = width/2 + cos(radians(i * polyAngles)) * ((points[i]/100)*(width/2-margin)); yVertex = height/2 + sin(radians(i * polyAngles)) * ((points[i]/100)*(height/2-margin)); ellipse(xVertex, yVertex, pointDiameter, pointDiameter); // angle vertices } } } [code]