Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ExpShape es;
- void setup() {
- size(600, 600);
- //make a new expanding shape
- es = new ExpShape();
- //for 36 steps around a circle, you have a 60% chance to add a vertex, which can vary between 0 and width/2-100 in radius from the center
- //just a fun way to makes some weird random shapes
- for(int i = 0; i < 36; i++){
- if(random(1) > 0.4){
- float ranRad = random((width-200)/2.0);
- es.vert(sin(TWO_PI/36.0 * i)*ranRad + width/2, cos(TWO_PI/36.0 * i)*ranRad + height/2);
- }
- }
- }
- void draw() {
- background(51);
- stroke(color(255,0,0));
- fill(255);
- es.setThickness((cos(frameCount/32.0 + PI)+1)*50.0);
- es.show();
- }
- class ExpLine {
- float thick; //the thickness of the line
- PVector p1; //point 1 of the expanding line
- PVector p2; //point 2 of the expanding line
- PShape s; //the over-all shape of the expanded line, including the perimeter.
- ExpLine(float x1, float y1, float x2, float y2) {
- p1 = new PVector(x1, y1);
- p2 = new PVector(x2, y2);
- thick = 50.0;
- }
- void show() {
- s = createShape();
- s.beginShape();
- //make a half-circle of vertices around p1, distance "thick" appart
- for (int i = 10; i < 28; i++) {
- PVector rotA = new PVector(p2.x-p1.x, p2.y-p1.y).normalize(); //a rotator PVector pointing from p1 to p2.
- rotA.rotate(TWO_PI / 36.0 * i); //rotate the vector between half-PI and three-halves-PI (i ranges from 10 to 28, so it's 10/36 to 28/36, multiplied by TWO_PI)
- rotA.x *= abs(thick);
- rotA.y *= abs(thick);
- s.vertex(p1.x + rotA.x, p1.y + rotA.y);
- }
- //make a half-circle of vertices around p2, distance "thick" appart
- for (int i = 9; i < 28; i++) {
- PVector rotB = new PVector(p1.x-p2.x, p1.y-p2.y).normalize(); //a rotator PVector pointing from p2 to p1.
- rotB.rotate(TWO_PI / 36.0 * i); //rotate the vector between half-PI and three-halves-PI (i ranges from 10 to 28, so it's 10/36 to 28/36, multiplied by TWO_PI)
- rotB.x *= abs(thick);
- rotB.y *= abs(thick);
- s.vertex(p2.x + rotB.x, p2.y + rotB.y);
- }
- s.endShape(CLOSE);
- shape(s, 0, 0);
- }
- }
- class ExpShape {
- ArrayList<PVector> verts; //array of vertices, so we can draw the under-lining shape
- ArrayList<ExpLine> lines; //array of expanding lines, so that we can expand the perimeter of the shape
- PShape s;
- ExpShape() {
- //both array lists begin empty, and will only change once you add vertices manually, much like PShape.
- verts = new ArrayList<PVector>();
- lines = new ArrayList<ExpLine>();
- }
- //manually add vertex, and recalculate shape
- void vert(float x, float y) {
- verts.add(new PVector(x, y));
- updateShape();
- }
- void show() {
- shape(s, 0, 0);
- for (ExpLine l : lines) {
- l.show();
- }
- }
- void updateShape() {
- lines.clear(); //clear all lines
- s = createShape();
- s.beginShape();
- for (int i = 0; i < verts.size(); i++) {
- //if we're on the last vertex of the shape, add an expanding line from the last point to the first
- //otherwise, add a line from the current point to the next
- if (i == verts.size()-1) {
- lines.add(new ExpLine(verts.get(i).x, verts.get(i).y, verts.get(0).x, verts.get(0).y));
- } else {
- lines.add(new ExpLine(verts.get(i).x, verts.get(i).y, verts.get(i+1).x, verts.get(i+1).y));
- }
- //add a vertex for the underlying shape
- s.vertex(verts.get(i).x, verts.get(i).y);
- }
- s.endShape(CLOSE);
- }
- //adjust the thickness for all ExpandingLines, in effect increasing the perimeter of the overall shape
- void setThickness(float t) {
- for (ExpLine l : lines) {
- l.thick = t;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement