Why Won't My Self-Made Functions Run?

edited January 2018 in Questions about Code

I'm attempting to create a project for school where I make a flower using created functions. I can't get both functions to run simultaneously. I tried changing variables, constants, etc. Nothing is working. I can have one function run, but not the other. Please help

void setup(){

  size(300,300);
}


void draw(){

  petals(40,40,200,0,0);

  stem(50,100);
}

//Making petal function

void petals(float x,float y,float a,float b,float c){

  fill(a,b,c);

  translate(width/2,height/2);

  ellipse(0,0,x,y);

  fill(a/2,b/2,c/2);

  ellipse(0,0,x/2,y/2);
}

//Making stem function

void stem(float b,float p){

  translate(width/2,height/2);

  fill(0,p,0);

  line(0,0,0,b);

} 

Answers

  • edited January 2018 Answer ✓

    Both of your functions call translate(). This moves where (0,0) is. But nothing moves it back! So your first call to translate centers (0,0), and your second call moves it AGAIN - to the lower right corner.

    The best thing to do to fix this is to just have one call to translate - to center the origin once:

    void setup() {
      size(300, 300);
    }
    
    void draw() {
      // Center the origin.
      translate(width/2, height/2);
      petals(40, 40, 200, 0, 0);
      stem(50, 100);
    }
    
    //Making petal function
    void petals(float x, float y, float a, float b, float c) {
      fill(a, b, c);
      ellipse(0, 0, x, y);
      fill(a/2, b/2, c/2);
      ellipse(0, 0, x/2, y/2);
    }
    
    //Making stem function
    void stem(float b, float p) {
      fill(0, p, 0);
      line(0, 0, 0, b);
    }
    

    You might also consider using pushMatrix() and popMatrix().

  • Answer ✓

    edit post, highlight code, press ctrl-o to format.

  • TfGuy, thanks for the help.

    Also, I appreciate the advice, koogs.

Sign In or Register to comment.