brik
YaBB Newbies
Offline
Posts: 23
Improving the efficiency of drawing many lines?
May 26th , 2009, 7:32am
Hello, i'm attempting to make a simple drawing app similar to the flash one on ratemydrawing.com, but before my app reaches anything near the number of vertices required for a semi-decent drawing, it begins to seriously chug. Is there any way of improving the efficiency of drawing thousands of lines, perhaps by combining each section into a shape? I've attempted to use drawing onto a canvas instead (which meant that the number of lines draw didn't affect the framerate) but to get the canvas size I wanted it had to be huge...and the memory usage was off the charts! Thanks, Rob /* Buttons: 1 - draw (drag mouse to draw) 2 - move (drag mouse to move) */ PFont font; int fps=60; int dX=800; int dY=800; ArrayList userPoints=new ArrayList(); boolean mouse=false; int viewX=0; int viewY=0; int dragStartX=0; int dragStartY=0; int tool=0; void setup(){ size(dX,dY); frameRate(fps); smooth(); background(255); font=createFont("Arial",12); textFont(font,12); } void draw(){ background(255); if(mousePressed){ //button held if(!mouse){ if(tool==0){ userPoints.add(new Point(mouseX-viewX,mouseY-viewY)); } if(tool==1){ dragStartX=mouseX-viewX; dragStartY=mouseY-viewY; } } //add point if(tool==0) if(!(mouseX==pmouseX && mouseY==pmouseY)|| !mouse){ userPoints.add(new Point(mouseX-viewX,mouseY-viewY)); } //set drag start pos if(tool==1){ viewX=(mouseX-dragStartX); viewY=(mouseY-dragStartY); } mouse=true; } //released else if(mouse){ if(tool==0){ userPoints.add(new Point()); } mouse=false; } //draw lines if(userPoints.size()>0){ for(int i=0;i<userPoints.size()-1;i++){ Point p2=(Point)userPoints.get(i+1); Point p1=(Point)userPoints.get(i); if(!p2.empty && !p1.empty){ int x2=p2.x+viewX; int y2=p2.y+viewY; int x1=p1.x+viewX; int y1=p1.y+viewY; stroke(0); strokeWeight(3); line(x1,y1,x2,y2); } noFill(); } } fill(0); //show view position text("("+-viewX+","+-viewY+")",10,20); text("Num vertices: "+userPoints.size(),width/2,20); text("fps: "+frameRate,width/4,20); if(tool==0)text("Drawing",width-50,20); if(tool==1)text("Moving",width-50,20); } void keyPressed(){ if(key=='1'){ tool=0; } if(key=='2'){ tool=1; } } public class Point{ int x,y; boolean empty=false; Point(){ empty=true; x=0; y=0; } Point(int x,int y){ this.x=x; this.y=y; } void setPos(int x,int y){ this.x=x; this.y=y; } String toString(){ if(!empty)return x+" "+y; else return "-"; } }