skwirrel
YaBB Newbies
Offline
Posts: 3
Reading drawing attributes
Jun 8th , 2006, 10:35am
I would like to right a function to draw rectangles with rounded edges. It would be nice if this behaved exactly like the built in Rect function by not changing any of the Stroke, Fill, StrokeWeight etc. drawing attributes. The code below demonstrates the problem(s). If strokeWeight() were to return the current strokeWeight and so on for the other functions this would do the job. I would then be able to build functions to support a drawing attribute stack. I could then call pushAttributeStack() at the beginning of my function - this would push a copy of all drawing attributes onto a stack without changing any of them. I could then carry on doing stuff in my function and call popAttributeStack before leaving the function. Any ideas or am I missing something? Thanks ========================================================= void setup() { size(200,200); smooth(); noLoop(); } void draw() { stroke(0); strokeWeight(1); smoothRect( 10,10,100,20,4 ); // this box won't work as expected because the previous call too smoothRect called noStroke smoothRect( 40,40,100,20,4 ); // now reset drawing attributes to demonstrate another problem stroke(0); strokeWeight(5); // this box won't work because the smoothRect function doesn't know the strokeWeight // in fact it assumes it is 1. So when it draws in the infill boxes it overwrites the outline smoothRect( 80,80,100,20,4 ); } void smoothRect( int x, int y, int w, int h, int r ) { line( x, y+r, x, y+h-r ); line( x+r, y, x+w-r, y ); line( x+w, y+r, x+w, y+h-r ); line( x+r, y+h, x+w-r, y+h ); ellipseMode(CENTER); arc( x+r, y+r, 2*r, 2*r, PI*1, PI*1.5 ); arc( x+r, y+h-r, 2*r, 2*r, PI*0.5, PI*1 ); arc( x+w-r, y+r, 2*r, 2*r, PI*1.5, PI*2 ); arc( x+w-r, y+h-r, 2*r, 2*r, 0, PI*0.5 ); noStroke(); rect( x+r, y+1, w-2*r, h-1 ); rect( x+1, y+r, w-1, h-2*r ); }