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 › creating a polygon class
Page Index Toggle Pages: 1
creating a polygon class (Read 704 times)
creating a polygon class
Dec 2nd, 2009, 1:06pm
 
Hello .. I want to create a polygon class with variable number of corners. The problem is in line 3 of the code. I can't use "fl_corners", which is the number of corners of the polygon, before it is defined.

I hope somebody can help me with this problem.

class Fl
{
 PVector [] vertices = new PVector[fl_corners];
 float w;
 float angle;
 int fl_corners;

 
 Fl (int fl_corners_, float w_)
 {
   fl_corners = fl_corners_;
   w = w_;
   angle = 360/fl_corners;
 
     
   for (int i=0; i<fl_corners; i++)
   {
     vertices[i] = new PVector((w/2)*cos(radians(angle)), (w/2)*sin(radians(angle)));    
   }
 }
   
 void create()
 {
   beginShape();
   for (int i=0; i<fl_corners; i++)
   {
     vertex(vertices[i].x, vertices[i].y);
   }
   endShape();
 }
 
}
Re: creating a polygon class
Reply #1 - Dec 2nd, 2009, 2:01pm
 
i wish i would knew exactly how to say it in "programmer languag" initialize, declare, set variables. etc. but what i want to say is. you can write it that way


class Fl
{
PVector [] vertices;
float w;
float angle;
int fl_corners;


Fl (int fl_corners_, float w_)
{
  fl_corners = fl_corners_;
  w = w_;
  angle = 360/fl_corners;
  vertices = new PVector[fl_corners];



didnt test it but i hope you get the idea
Re: creating a polygon class
Reply #2 - Dec 2nd, 2009, 2:11pm
 
Well - clearly you can't create the array before you know how long it's going to be, but you do at least need to declare its existence:

Code:
class Fl {

float w;
float angle;
int fl_corners;
PVector [] vertices;

Fl (int fl_corners_, float w_) {
  fl_corners = fl_corners_;
  vertices = new PVector[fl_corners];
  w = w_;
  angle = 360/fl_corners;

   
  for (int i=0; i<fl_corners; i++)
  {
    vertices[i] = new PVector((w/2)*cos(radians(angle)), (w/2)*sin(radians(angle)));    
  }
}
 
void create()
{
  beginShape();
  for (int i=0; i<fl_corners; i++)
  {
    vertex(vertices[i].x, vertices[i].y);
  }
  endShape();
}

}


A couple of minor points on coding style:

1.  What's an 'Fl'?  If you're going to create a class you need to give it a meaningful name so people know what they're going to use it for Wink

2. I find all those underscores in the argument names really ugly and actually a little difficult to read.  It's a matter of taste, but you can use the same name as the variable for the argument in a constructor and precede the Object variable with 'this' - e.g.:

Code:
class myPolygonClass {

float w;
float angle;
int corners;
PVector [] vertices;

myPolygonClass (int corners, float w) {
  this.corners = corners;
  this.vertices = new PVector[corners];
  this.w = w;
  this.angle = 360/corners;

   
  for (int i=0; i<corners; i++)   {
    vertices[i] = new PVector((w/2)*cos(radians(angle)), (w/2)*sin(radians(angle)));
    println(vertices[i].x + " " + vertices[i].y);
  }
}


Note that I've added a println to highlight a slight problem - you might want to use the value of i to adjust those vertex coordinates.  I'd also be tempted to add parameters to the create method so you can choose where to place your rendered polygon...
Re: creating a polygon class
Reply #3 - Dec 2nd, 2009, 3:00pm
 
Hey .. thx for the first help. Your points in case of coding style sounds useful.

Now i get a NullPointerException at the initialization of the vertices and i don't know how to deal with.

These are my first approaches in processing, especially in creating classes.

Code:
class myPolygonClass {

PVector [] vertices;
float w;
float angle;
int corners;

myPolygonClass (int corners, float w) {
this.corners = corners;
this.w = w;
this.vertices = new PVector[corners];
this.angle = 360/corners;

for (int i=0; i<corners; i++) {
vertices[i].x = (w/2)*cos(radians(angle*i));
vertices[i].y = (w/2)*sin(radians(angle*i));
}
}

void create() {
beginShape();
for (int i=0; i<corners; i++) {
vertex(vertices[i].x, vertices[i].y);
}
endShape();
}
}
Re: creating a polygon class
Reply #4 - Dec 2nd, 2009, 3:46pm
 
Quote:
for (int i=0; i<corners; i++) {
     vertices[i].x = (w/2)*cos(radians(angle*i));
     vertices[i].y = (w/2)*sin(radians(angle*i));  
   }


Whilst separating this out from the PVector creation is neater and easier to read, you do still have to create the PVector object first:

Code:
for (int i=0; i<corners; i++) {
       vertices[i] = new PVector(); // create a PVector object!
vertices[i].x = 1;//(w/2)*cos(radians(angle*i));
vertices[i].y = (w/2)*sin(radians(angle*i));  
   }
Page Index Toggle Pages: 1