Response title
This is preview!




Click on Join Now to Sign Up
BrushStroke bs; PVector follow, pfollow; void setup() { size(400,400,P3D); noStroke(); bs = new BrushStroke(); bs.count = 200; follow = new PVector(); pfollow = new PVector(); } void mousePressed() { bs = new BrushStroke(); bs.count = 200; } void draw() { background(0); if(keyPressed) { noFill(); stroke(255); } else { fill(255); noStroke(); } follow.x = alerp(follow.x, mouseX, .05, .01); follow.y = alerp(follow.y, mouseY, .05, .01); ellipse(follow.x, follow.y, 5, 5); boolean following = pfollow.x!=follow.x || pfollow.y!=follow.y; if (mousePressed && following) bs.addSegment( new PVector(follow.x, follow.y), PVector.dist(pfollow, follow)); beginShape(QUADS); BrushStroke.Segment prev = null; for(BrushStroke.Segment cur : bs.segments) { if(cur.first) continue; if(prev == null) { prev = cur; continue; } vertex(prev.right.x, prev.right.y); vertex(prev.left.x, prev.left.y); vertex(cur.left.x, cur.left.y); vertex(cur.right.x, cur.right.y); prev = cur; // line(cur.left.x, cur.left.y, cur.right.x, cur.right.y); } endShape(); pfollow.x = follow.x; pfollow.y = follow.y; } float alerp(float in, float tar, float rate, float acc) { return abs(tar-in)<=acc?tar:in+(tar-in)*rate; } class BrushStroke { int count; LinkedList<Segment> segments; BrushStroke() { segments = new LinkedList<Segment>(); } void addSegment(PVector center, float ww) { Segment end = null; try { end = segments.getLast(); } catch(Throwable ex) {} if(end != null) end.last = false; segments.add(new Segment(end, center, ww)); // if(end != null && end.first) // end.angle = segments.getLast().angle; if(segments.size() > count) segments.removeFirst(); } class Segment { Segment previous; float angle, ww; PVector center, left, right; boolean first, last; Segment(Segment previous, PVector center, float ww) { this.previous = previous; this.center = center; this.ww = ww; if (previous != null) angle = PVector.sub(previous.center, center).heading2D(); else first = true; last = true; updatePts(); } void updatePts() { left = new PVector(); left.x = center.x + cos(angle-HALF_PI)*ww*.5; left.y = center.y + sin(angle-HALF_PI)*ww*.5; right = new PVector(); right.x = center.x + cos(angle+HALF_PI)*ww*.5; right.y = center.y + sin(angle+HALF_PI)*ww*.5; } } }