We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Reading drawing attributes
Page Index Toggle Pages: 1
Reading drawing attributes (Read 395 times)
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   );
}
Re: Reading drawing attributes
Reply #1 - Jun 8th, 2006, 11:55am
 
I have found an alternative method of acheiving what I was trying to do using a polygon as shown below. However my question still stands as I am sure it would be useful to be able to read and store drawing attributes.

========================================================

void smoothRect( int x, int y, int w, int h, int r ) {

 beginShape(POLYGON);
   vertex(x, y+r);
   vertex(x, y+h-r);
   bezierVertex(x,y+h-r/4,x+r/4,y+h,x+r,y+h);
   vertex(x+w-r,y+h);
   bezierVertex(x+w-r/4,y+h,x+w,y+h-r/4,x+w,y+h-r);
   vertex(x+w,y+r);
   bezierVertex(x+w,y+r/4,x+w-r/4,y,x+w-r,y);
   vertex(x+r,y);
   bezierVertex(x+r/4,y,x,y+r/4,x,y+r);
   vertex(x,y+r);
 endShape();
}
Re: Reading drawing attributes
Reply #2 - Jun 8th, 2006, 12:29pm
 
see here:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Suggestions;action=display;num=1148363381

F
Re: Reading drawing attributes
Reply #3 - Jun 8th, 2006, 10:43pm
 
Many thanks. This is exactly what I was looking for.
Page Index Toggle Pages: 1