A basic question about Classes/constraints

edited November 2013 in Questions about Code

Hi all,

A rookie question again but maybe someone can help me. I'm practising using classes for the first time and have created a "PieChart" class and used it to draw 3 "pie" objects with labels on them. I have their locations set to random values but I wanted to constrain them so they never went off the edges of the cavas, so I used the constrain command:
xposCentre = constrain(xposCentre, (0+radius), (height - radius)); yposCentre = constrain(yposCentre, (0+radius), (width - radius));

However sometimes when I run the code they are still falling off the edge. Can someone point me to what I am doing wrong?

Also I would like to have the programme run so that the "pies" never overlap eahcother. Is there an easy way to do this?

Full code below.

Many Thanks

class PieChart {

  //declare variables


  float radius; 
  float arcWidth; 
  float arcHeight;
  float startAngle;
  float stopAngle; 
  float xposCentre;
  float yposCentre;
  String title;



  //constructor and setting initial values

 PieChart(String titleTemp, float stopAngleTemp){ 

   radius = random(50,100); 
   xposCentre = random(100,700); 
   yposCentre = random(100,700);
   arcHeight = random(50,400);
   arcWidth = arcHeight;
   startAngle = 0; 
   stopAngle = stopAngleTemp * PI; 
   title = titleTemp;}


   //actions it can perform

   void show(){ //show the pie


   stroke(0);
   strokeWeight(1); 
   fill(0); 
   arc(xposCentre,yposCentre,arcWidth,arcHeight, startAngle, stopAngle);
     fill(111); 
     xposCentre = constrain(xposCentre, (0+radius), (height - radius));
     yposCentre = constrain(yposCentre, (0+radius), (width - radius));
     textAlign(CENTER);      
     text(title, xposCentre, yposCentre);
   }

}

PieChart pie1;
PieChart pie2; 
PieChart pie3; 

 void setup(){


     size(800,800);
     smooth();
     background(255);


     pie1 = new PieChart("Half a pie", 1);
     pie2 = new PieChart("3/4 of a pie", 1.5); 
     pie3 = new PieChart("Whole pie", 2); 


   }

   void draw(){

     pie1.show(); 
     pie2.show(); 
     pie3.show();


   }

Answers

  • Answer ✓

    The problem is that you are using the radius value for the constraint and the arcWidth and arcHeight for the drawing but they are not the same. Here is my code to solve the problem. BTW in lines 41 & 42 where you constrain the pie centre you got the width and height swapped over.

    class PieChart {
    
      //declare variables
    
    
      float radius;
      float arcWidth;
      float arcHeight;
      float startAngle;
      float stopAngle;
      float xposCentre;
      float yposCentre;
      String title;
    
    
    
      //constructor and setting initial values
    
      PieChart(String titleTemp, float stopAngleTemp) {
    
        radius = random(50, 200);
        xposCentre = random(100, 700);
        yposCentre = random(100, 700);
        //    arcHeight = random(50, 400);
        arcWidth = arcHeight = radius;
        startAngle = 0;
        stopAngle = stopAngleTemp * PI;
        title = titleTemp;
      }
    
    
      //actions it can perform
    
      void show() { //show the pie
        stroke(0);
        strokeWeight(1);
        fill(0);
        arc(xposCentre, yposCentre, arcWidth, arcHeight, startAngle, stopAngle);
        fill(0, 200, 0);
        xposCentre = constrain(xposCentre, radius, (width - radius));
        yposCentre = constrain(yposCentre, radius, (height - radius));
        textAlign(CENTER);     
        text(title, xposCentre, yposCentre);
      }
    }
    
    PieChart pie1;
    PieChart pie2;
    PieChart pie3;
    
    void setup() {
    
    
      size(800, 800);
      smooth();
      background(255);
    
    
      pie1 = new PieChart("Half a pie", 1);
      pie2 = new PieChart("3/4 of a pie", 1.5);
      pie3 = new PieChart("Whole pie", 2);
    }
    
    void draw() {
      background(255);
      pie1.show();
      pie2.show();
      pie3.show();
    }
    
  • _vk_vk
    edited November 2013 Answer ✓

    edit: late... : P

    You don't really use radius in your drawing (no use at all?), so constraining by it won't do you good. Use arcWidth and arcHeight to do your constrain, I would do it in the constructor, like:

      //constructor and setting initial values
    
      PieChart(String titleTemp, float stopAngleTemp) { 
        arcHeight = random(50, 400);
        arcWidth = arcHeight;
        xposCentre = constrain(random(100, 700), arcWidth, (width - arcWidth)); 
        yposCentre = constrain(random(100, 700), arcHeight, (height - arcHeight));
        startAngle = 0; 
        stopAngle = stopAngleTemp * PI; 
        title = titleTemp;
      }
    

    To avoid overlapping you need to keep trying random values until you get a free space big enough to fit your new pie. But an instance of a class does not know about others instances... So I think you need to manage this outside this class, maybe in a PieMaker class that would hold all your pies. Here is a good thread about no overlapping circles

  • edited November 2013

    Oh man! Thanks so much, I can't believe I didn't notice "radius" wasn't even an attribute of my pies! That was a case of staring at something for so long you can't see the obvious right in front of you! Thanks both.

Sign In or Register to comment.