FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   help - problem with polygon "rainbow" shape
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: help - problem with polygon "rainbow" shape  (Read 353 times)
kevinP

Email
help - problem with polygon "rainbow" shape
« on: Jun 4th, 2004, 3:27pm »

I wrote a simple class for drawing pie shapes; then I modified it to draw "clipped" pie shapes (i.e. rings), with an inner and outer radius.
 
Unfortunately this works terribly. All I am doing (I think) is taking the original arc (polygon sides) and adding a "side" then drawing a smaller arc going back through the angle and then closing the polygon.
 
Here's what I have:
http://www.tiros.net/pfeiffer/processing/misc/arc_problem/
 
MouseX determines the sweep and mouseY the outer radius.
 
Aside from the obvious problem (lines, wierd shapes), one thing that seems strange is that the fixed "end" of the arc  should be stationary, at y == 0 for both points. In my simple pie version this all works perfectly, but here not...  
« Last Edit: Jun 4th, 2004, 3:33pm by kevinP »  

Kevin Pfeiffer
ess

Email
Re: help - problem with polygon "rainbow" shape
« Reply #1 on: Jun 4th, 2004, 3:47pm »

I think I did something similar for my "cyclotron" sketch:
 
http://mywebpages.comcast.net/warptera/p5/cyclotron/
 
I cut out the applicable pieces of source-code for you.  I think it should be easy to adapt to your mouse controls. in fact, if you are only drawing one arc it might be better to just forget the class and object array and write it all in the void loop().  hope this helps:
 
Code:
Slice[] slices;
int num=1;
 
void setup() {
  size(500,500);
  noStroke();
  slices = new Slice[num];
  initialize();
}
 
void loop() {
  background(0);
  translate(width/2,height/2);
  for (int i=0; i<num; i++) {
    slices[i].drawShape();
  }
}
 
void initialize() {
  for (int i = 0; i<num; i++) {
    slices[i] = new Slice();
  }
}
 
class Slice {
  float ri,ro,arc;
  color c;
   
  {
  ri=random(25,50); //inner radius
  ro=ri+random(25,100);  //outer radius (added to inner radius)
  arc=random(2,5);  //determines how wide the arc spreads
  }
   
  void drawShape() {
    push();
    c=color(0,0,255);
    fill(c);
    beginShape(TRIANGLE_STRIP);
 for(float i=0; i<TWO_PI/arc; i+=PI/20) {  //increasing/decreasing the "20" in i+=PI/20 will increase/decrease the detail of the rendering (more polys)
   vertex(ri*sin(i),ri*cos(i));
   vertex(ro*sin(i),ro*cos(i));
 }
    endShape();
    pop();
  }
}  
 

 
best,
 
ess
 
kevinP

Email
Re: help - problem with polygon "rainbow" shape
« Reply #2 on: Jun 4th, 2004, 4:35pm »

Thanks; I'll take a look. I see you are using triangular polygons; maybe I should, too. But I'm also interested to find out why my shape code is not working -- am I misusing it (i.e. POLYGON won't work) or have I just done something dumb?
 
-K
 

Kevin Pfeiffer
ess

Email
Re: help - problem with polygon "rainbow" shape
« Reply #3 on: Jun 4th, 2004, 5:16pm »

I took a quick look and it's hard to say exactly what's wrong.  I'd probably have to sit down and go through your procedure methodically step by step.  But just at a glance I think it's a more complex method of drawing an arc than is needed.  I suspect in some cases the algorithm isn't supplying some of the proper endpoints for the polygons.
 
A triangle strip is better because you only need to produce two concentric rings of vertices and the strip will connect them up automatically.  This is easy with a 'for' loop and the sin,cos functions.
 
Plug this example into Processing and hold the 's' key to see the stroke().  This will show how the triangular polygons are arranged:
 
Code:
float ri,ro,arc;  
color c;  
    
void setup() {  
  size(500,500);  
  noStroke();  
}  
 
void loop() {  
  background(0);  
  translate(width/2,height/2);
 
  noStroke();
  if(keyPressed) {
    if(key=='s') {stroke(0);}
  }
  
  ri=(mouseX)*.5;  
  ro=ri+20;   
  arc=constrain(mouseY*.01,1,20);   
  
  c=color(0,0,255);
  fill(c);  
  beginShape(TRIANGLE_STRIP);  
  for(float i=0; i<TWO_PI/arc; i+=PI/20) {
    vertex(ri*sin(i),ri*cos(i));  
    vertex(ro*sin(i),ro*cos(i));  
  }  
  endShape();  
}
« Last Edit: Jun 4th, 2004, 5:18pm by ess »  
kevinP

Email
Re: help - problem with polygon "rainbow" shape
« Reply #4 on: Jun 4th, 2004, 6:59pm »

on Jun 4th, 2004, 5:16pm, ess wrote:
But just at a glance I think it's a more complex method of drawing an arc than is needed.  I suspect in some cases the algorithm isn't supplying some of the proper endpoints for the polygons.

 
My attempt is (supposed to be) one single polygon.
 
Okay, I'll try out triangles; I see what you mean. After I get the better version working then I'll go back and try to write a very simple (no loop) version of my single polygon to see if I can find my bug.
 
One note -- try your version with the smooth() function. Turning that on I do see faint polygon edges.
 
Thanks again!
 

Kevin Pfeiffer
JohnG

WWW
Re: help - problem with polygon "rainbow" shape
« Reply #5 on: Jun 4th, 2004, 7:24pm »

on Jun 4th, 2004, 4:35pm, kevinP wrote:
Thanks; I'll take a look. I see you are using triangular polygons; maybe I should, too. But I'm also interested to find out why my shape code is not working -- am I misusing it (i.e. POLYGON won't work) or have I just done something dumb

 
In the world of 3D everything ends up as triangles in the end, for various reasons, mainly to do with speed, and practicality (triangles can't be concave for example, and are always planar)
So even thouggh you're using beginShape(POLYGON) the 3D engine will, at some point convert it to triangles to actually draw it, and it looks liek things are going astray at that point.
 
ess

Email
Re: help - problem with polygon "rainbow" shape
« Reply #6 on: Jun 4th, 2004, 7:48pm »

Quote:
One note -- try your version with the smooth() function. Turning that on I do see faint polygon edges.

 
yeah,  
 
I don't know exactly how the smooth() function works, but it seems like it just blends the edges of shapes with the background.  This probably saves alot of processing (no pun intended) power, but causes some undesirable effects sometimes.
 
 
Pages: 1 

« Previous topic | Next topic »