How to shorten code

I am having trouble trying to shorten this code. The code that is taking up the most space is the vertex which are the stars. Is there a way to shorten the code for the stars?

void polygon(int n, float cx, float cy, float w, float h, float startAngle) {
  float angle = TWO_PI/ n;

  // The horizontal "radius" is one half the width,
  // the vertical "radius" is one half the height
  w = w / 2.0;
  h = h / 2.0;

  beginShape();
  for (int i = 0; i < n; i++) {
    vertex(cx + w * cos(startAngle + angle * i), 
      cy + h * sin(startAngle + angle * i));
  }
  endShape(CLOSE);
}


void drawHouse(int x, int y){
  rect(x,y,270,120);
  fill(0,0,0);
  fill(#5185A0);
  quad(20,90,100,10,220,10,290,90);
  stroke(#FF5921);
  fill(#FF5921);
  stroke(0,0,0);
}
  void draw() {
  translate(40, 105);
  fill(255,255,255);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
   translate(40, 0);
  fill(255,255,255);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
   translate(40, 0);
  fill(255,255,255);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
   translate(0, 30);
  fill(255,255,255);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
   translate(-40, 0);
  fill(255,255,255);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
   translate(-40, 0);
  fill(255,255,255);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
   translate(450, -110);
  fill(#EDE200);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
     translate(-40,0);
  fill(#EDE200);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
  translate(-40,0);
  fill(#EDE200);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
  translate(-40,0);
  fill(#EDE200);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
  translate(-40,0);
  fill(#EDE200);
  stroke(0,0,0);
  strokeWeight(2);
  beginShape();
  vertex(0, -5);//top of star
  vertex(5, 2);//right side top
  vertex(15, 5);//rightside
  vertex(8, 13);//right bottom
  vertex(10, 22);//right bottom point
  vertex(0, 17);//center
  vertex(-10, 22);//left bottom point
  vertex(-8, 13);//left bottom midpoint
  vertex(-15, 5);//left point
  vertex(-5, 2);//top left point
  endShape(CLOSE);
  
}

void drawCar(int x, int y) {
fill(#FF8181);
  rect(x,y,220,50);
  fill(#9DFFF6);
  quad(350,140,375,90,475,90,500,140);
  stroke(0,0,255);
  strokeWeight(6);  // Beastly
  line(350, 140, 375, 90);
  line(375,90,475,90);
  line(475,90,500,140);
  line(350,140,500,140);
  stroke(0,0,0);
  fill(#A0A0A0);
  ellipse(495,200,35,35);//tires
  ellipse(355,200,35,35);
  strokeWeight(2);
  line(425,90,425,140);
}
void drawFish(int x, int y) {
  fill(0,0,200);
  ellipse(x, y, 130, 80);
  fill(#FFD900);
  strokeWeight(3);
  beginShape();
  fill(255,0,0);
  vertex(125,340);//onfish
  vertex(60,300);
  vertex(80,325);
  vertex(60,330);
  vertex(80,340);
  vertex(60,350);
  vertex(80,360);
 vertex(60,385);
  vertex(125,340);//onfish
  endShape();
  beginShape();
  fill(255,0,0);
  point(254,340);
  point(230,350);
  point(248,360);
  vertex(256,340);
  vertex(230,350);
  vertex(248,360);
  endShape();
  fill(240, 240, 240);
  ellipse(x+40, y-15, 20, 20);
  fill(0,0,200);
  ellipse(x+40, y-15, 10, 10);
  fill(255,255,255);
  beginShape();
  vertex(130,356);
  vertex(127,340);
  vertex(127,330);
  vertex(135,320);
  vertex(150,309);
  vertex(190,300);
  vertex(190,380);
  vertex(150,372);
  vertex(135,362);
  vertex(130,356);
  
  endShape();
  

}
void myDrawing(int x, int y) {
  fill(#FF6CF1);
  arc(380, 320, 100, 100,PI, TWO_PI);
  stroke(#FF6CF1);
  ellipse(340, 330, 5, 70);
  ellipse(360, 330, 5, 70);
  ellipse(380, 330, 5, 70);
  ellipse(400, 330, 5, 70);
  ellipse(420, 330, 5, 70);
  fill(255,255,255);
  stroke(255,255,255);
  ellipse(395,295,25,25);
  ellipse(365,295,25,25);
  fill(0,0,255);
  ellipse(395,295,15,15);
  ellipse(365,295,15,15);
  
  stroke(0,0,0);
  
  fill(#FF6CF1);
  arc(500, 320, 100, 100,PI, TWO_PI);//face
  stroke(#FF6CF1);
  ellipse(460, 330, 5, 70);//legs
  ellipse(480, 330, 5, 70);
  ellipse(500, 330, 5, 70);
  ellipse(520, 330, 5, 70);
  ellipse(540, 330, 5, 70);
  fill(255,255,255);
  stroke(255,255,255);
  ellipse(515,295,25,25);//eyes
  ellipse(485,295,25,25);
  fill(0,255,0);
  ellipse(515,295,15,15);
  ellipse(485,295,15,15);
}
void setup() {
size(600, 400);
background(#002BA7); // white
drawCar(315,145);
fill(#005008);
rect(0,220,600,40);//grass
stroke(0,0,0);
  fill(#A0A0A0);
rect(290,220,320,50);
fill(55,26,255);
rect(0,260,600,169);
drawFish(190, 340);
myDrawing(234, 340);
stroke(0,0,0);
fill(#0935B4);
drawHouse(20,90);
strokeWeight(6);
line(160,170,160,90);
line(160,170,22,170);
strokeWeight(3);
  polygon(5, 50, 200, 75, 50, -PI / 2.0); // -90 degrees
fill(255,0,0);//red stripes
rect(160,90,130,10);
rect(160,110,130,10);
rect(160,130,130,10);
rect(160,150,130,10);
rect(20,170,270,10);
rect(20,190,270,10);
rect(20,210,270,10);
fill(255,255,255);
rect(160,100,130,10);//white stripes
rect(160,120,130,10);
rect(160,140,130,10);
rect(160,160,130,10);
rect(20,180,270,10);
rect(20,200,270,10);
line(200,170,200,220);
line(230,170,230,220);
line(200,170,230,170);
fill(0,0,0);
ellipse(225,195,3,3);
fill(#58CBFF);
polygon(6, 220, 60, 50, 50, 0);
polygon(6, 160, 35, 50, 50, 0);
polygon(6, 100, 60, 50, 50, 0);


// Call drawing functions here

}
1 Like

Please, edit your post, remove the code, use the little button that looks like </> above, and paste it in again - as this forum often corrupts code and makes it impossible to run.

As for your question, I think there is a way to make it shorter. vertex() can also accept a float[] array with 2 float numbers in it for X and Y coordinate.

So, what you can do, is to make a float[][] array, which would be an array of arrays, and put all of your coordinates in it. Then, use a for loop to iterate through them. Something like this:

//Don't put this in draw() as it would take quite a bit of time to make that array every time draw is run - put it in setup or outside of anything!
float[][] coordinates = {{0,-5}, {5,2}, {15,5}, {8,13}, {10,22}, {0,17}, {-10,22}, {-8,13}, {-15,5}, {-5,2}}; 

//...
void draw() {
  //...Other code...
  beginShape();
  for(int i=0;i<coordinates.length;i++){
    vertex(coordinates[i]);
  }
  endShape(CLOSE);
}

It would make your code wide instead though. Your choice!

1 Like

I did the edit and what do you mean it will be instead?

I tried putting my coordinates and it still will not run?

-a- the idea of @Architector_4
show you to make a shape by vertex ( using a array of floats),

-b- you also can give that shape a name ( createShape )

-c- also can do the same trick with a array for the position of the shapes

so a shorter form might look like:

PShape myshape;
float[][] coordinates = {{0, -5}, {5, 2}, {15, 5}, {8, 13}, {10, 22}, {0, 17}, {-10, 22}, {-8, 13}, {-15, 5}, {-5, 2}}; 
int  [][] pos = {{40, 105}, {40, 20}, {20, 30}, {50, 110}, {60, 30}};
int mposx, mposy;

void make_myshape() {
  myshape = createShape();
  myshape.beginShape();
  myshape.fill(#EDE200);
  myshape.stroke(0, 0, 0);
  myshape.strokeWeight(2);
  for (int i=0; i<coordinates.length; i++)  myshape.vertex(coordinates[i][0], coordinates[i][1]);
  myshape.endShape(CLOSE);
}

void setup() {
  size(200, 200);
  make_myshape();
}

void draw() {
  background(0,0, 80);
  for ( int i = 0; i< pos.length; i++)   shape(myshape, pos[i][0], pos[i][1]);
  shape(myshape, mposx, mposy);
}

void mousePressed() {
  mposx = mouseX;
  mposy = mouseY;
}

1 Like

Your original code puts the data in each draw call. Instead, you can:

  1. save the data in an array and loop over it with vertex (Architector_4)
  2. use the same data but rotated / shifted multiple times (kll)

and, if you have a lot of data / long data, you could additionally:

  1. load your points data from external files – e.g. from a text file or csv file star.txt, fish.txt, house.txt etc.
1 Like