|
Author |
Topic: Averaging line buffers (Read 1823 times) |
|
Mark Hill
|
Averaging line buffers
« on: Apr 7th, 2005, 10:20am » |
|
I've been looking at TomC's elegant use of the vector class and am trying to smooth out the contents of a line buffer to the overall average shape (which kind of works). The method I'm using is quite crude and force closes an object - relying upon a feedback loop. Can anyone see a way of dispensing with the need to rely on the enclosure method I'm currently using The ideal situation would be averaging out to a (pre-stored) geometric figure - triangle, circle, rectangle, again any ideas on an effective way of doing this I've dropped out some of Tom's code just to simplify this post. I'll modify in a sec with original link... Here it is: REPLY 12 http://processing.org/discourse/yabb/board_Syntax_action_displa_y_num_1091820063.html The penultimate line of Tom's melt() method should be changed to read: Code: else if (finished && points.size() <= 2) { |
| otherwise the line buffer never drops an element. Here's my bastardized version I'm enquiring about: Code: Vector lines = new Vector(); // for recording the mouse Line newLine; void setup() { size(320,240); strokeWeight(2); ellipseMode(CENTER_DIAMETER); smooth(); } void loop() { background(255); if (mousePressed) { if (newLine == null) { // start a new line newLine = new Line(); lines.addElement(newLine); } // record the current mouse position newLine.points.addElement(new Point2f(mouseX,mouseY)); } else if (newLine != null) { // set finished newLine.finished = true; // clear the line newLine = null; } for (int i = 0; i < lines.size(); i++) { Line line = (Line)lines.elementAt(i); line.draw(); if(keyPressed) { line.melt(); } } } class Point2f { float x,y; Point2f() { x = y = 0; } Point2f(int x, int y) { this.x = x; this.y = y; } } class Line { Vector points = new Vector(); boolean finished = false; boolean fade = false; void draw() { if (points.size() >= 2) { for (int j = 0; j < points.size()-1; j++) { Point2f a = (Point2f)points.elementAt(j); Point2f b = (Point2f)points.elementAt(j+1); stroke(0); fill(0); line(a.x,a.y,b.x,b.y); noStroke(); ellipse(a.x,a.y, 4,4); } } } void melt() { for (int j = 0; j < points.size()-1; j++) { Point2f a = (Point2f)points.elementAt(j); Point2f b = (Point2f)points.elementAt(j+1); a.x = (a.x + b.x) / 2; a.y = (a.y + b.y) / 2; } // force close the object Point2f a = (Point2f)points.elementAt(0); Point2f b = (Point2f)points.elementAt(points.size()-1); b.x = (a.x); b.y = (a.y); } } |
|
|
« Last Edit: Apr 7th, 2005, 1:42pm by Mark Hill » |
|
|
|
|
|