Drawing Heart,problem with class


#1

Hi all coders out there, i have drawn a heart shape using points and it is working , for drawing multiple hearts i have made a object “heart” with a constructor of passing 3 variable (size,x position and y position ) and two functions for drawing and beating but when i am drawing single heart its working but when trying to draw multiple heart only it displays the last heart drawn.

so please guys please help.

thank and regards
Abu Yazid.

code

/*heart h1=new heart(2,200,300);
heart h2=new heart(2,100,600);*/
heart h3=new heart(3, 700, 550);
//heart h4=new heart(5,500,550);
void setup()
{
  fullScreen(P2D);
  frameRate(60);
  background(0);
}

void draw()
{   
  background(0);
  /*h1.drawHeart();
  h1.beat();
  h2.drawHeart();
  h2.beat();*/
  h3.drawHeart();
  h3.beat();
}


float HeartSize,x,y;
float orgSize;
class heart
{
  heart(float hSize,float xPos, float yPos)
  {
    HeartSize=hSize;
    orgSize=hSize;
    x=xPos;
    y=yPos;
  }
  void drawHeart()
  {
    stroke(250,0,0);
    strokeWeight(2); 
    for(float t=0;t<=2*PI;t+=.01)
     point((-16*HeartSize*pow(sin(t),3))+x,(-(13*HeartSize*cos(t)-5*HeartSize*cos(2*t)-2*HeartSize*cos(3*t)-cos(4*t)))+y);
 
  }
  
  void beat()
  {
      delay(30);
      HeartSize+=.1;
      if(HeartSize>=orgSize+1)
        HeartSize=orgSize;
      
  }
}

#2

If you want each heart to have its own size and position, the variables used to remember a heart’s size and position must be inside the class. Also, your code did not format at all well for the forums… Please learn how to post code here properly.

heart h1=new heart(2, 200, 300);
heart h2=new heart(2, 100, 600);
heart h3=new heart(3, 700, 550);
//heart h4=new heart(5, 500, 550);

void setup() {
  fullScreen(P2D);
  //frameRate(60);
  //background(0);
}

void draw() {
  background(0);
  h1.drawHeart();
  h1.beat();
  h2.drawHeart();
  h2.beat();
  h3.drawHeart();
  h3.beat();
}


class heart {
  float HeartSize, x, y;
  float orgSize;
  heart(float hSize, float xPos, float yPos) {
    HeartSize=hSize;
    orgSize=hSize;
    x=xPos;
    y=yPos;
  }
  void drawHeart() {
    stroke(250, 0, 0);
    strokeWeight(2);
    for (float t=0; t<=2*PI; t+=.01)
      point((-16*HeartSize*pow(sin(t), 3))+x, (-(13*HeartSize*cos(t)-5*HeartSize*cos(2*t)-2*HeartSize*cos(3*t)-cos(4*t)))+y);
  }
  void beat() {
    delay(30);
    HeartSize+=.1;
    if (HeartSize>=orgSize+1)
      HeartSize=orgSize;
  }
}

#3

thank you very much for correcting my mistake TfGuy44. and I’m new here i didn’t know how to post the code properly formatted,next time ill be take care thanks.

Abu Yazid