Code
size (300,250);
int[] x ={
197,197,199,200,200,201,201,201,200,202,
202,202,201,200,199,200,198,194,192,189,
188,189,191,194,195,198,201,201,203,205,
203,206,205,203,203,206,209,214,215,217,
220,221,223,223,222,219,213,208,204,204,
201,197,192,189,185,182,180,178,176,168,
161,156,153,154,154,150,147,143,141,134,
131,127,123,116,109,107,101,87,77,70,65,
60,59,60,62,65,70,73,73,75,79,84,89,93,
98,100,101,103,107,108,111,112,112,112,
114,117,117,115,115,115,111,110,110,110,
110,109,108,108,109,108,107,106,106,106,
107,108,109,115,120,128,135,138,145,149,
153,157,162,166,169,173,176,181,186,191};
int[] y ={
4,9,11,14,18,23,29,35,39,42,46,49,50,52,
56,60,67,78,82,86,91,99,103,111,118,128,
137,142,153,163,171,182,191,200,204,206,
206,208,209,208,209,210,213,217,221,221,
222,221,221,225,226,227,225,227,226,226,
224,221,220,222,222,221,222,225,228,228,
226,228,228,228,228,227,228,228,220,216,
211,207,201,193,185,175,168,163,161,159,
159,161,165,170,174,179,183,185,186,186,
170,156,144,136,125,121,115,112,108,103,
97,92,87,83,71,66,61,58,56,54,49,45,40,
37,32,27,23,19,13,9,10,13,17,23,30,32,
31,29,29,30,31,31,32,28,24,18,13,9};
int lenX = x.length;
int lenY = y.length;
float theSumSoFarX = 0;
float theSumSoFarY = 0;
background (255);
noStroke();
fill (200,200,200);
rect (0,200,width,50);
fill (75,50,20);
beginShape ();
for (int i = 0; i <lenX; i ++){
vertex (x[i],y[i]);
theSumSoFarX = theSumSoFarX + x[i];
theSumSoFarY = theSumSoFarY + y[i];
}
endShape (CLOSE);
fill (200,200,0);
ellipse (145,60,15,10);
ellipse (175,60,15,10);
fill (0);
ellipse (145,58, 8,8);
ellipse (175,58, 8,8);
noFill();
stroke (255,50,50);
strokeWeight (3);
bezier (150,75, 155,65, 160,65, 165,75);
float centroidX = theSumSoFarX/lenX;
float centroidY = theSumSoFarY/lenY;
strokeWeight (1);
stroke (255,0,0);
ellipse (centroidX, centroidY, 10,10);
line (centroidX,centroidY-10, centroidX, centroidY+10);
line (centroidX-10,centroidY, centroidX+10, centroidY);
/*
It's a kitty!
*/
0506 - Iterating through an Array: Computing the Centroid of a Polygon
Statement:
// The following two arrays define a sequence of x-values,
// and a sequence of y-values, which describe a certain shape or polygon.
//
// (1) In a canvas of size 300x250 pixels, plot this shape.
// You should use array.length to automatically determine
// the number of points to plot. Also, use beginShape()/endShape()
// to plot the polygon.
//
// (2) The "centroid" of a shape is defined as a point, whose:
// x-coordinate is the average of the x-coordinates of the shape, and whose
// y-coordinate is the average of the y-coordinates of the shape.
// Compute and plot the centroid of this shape, using a small crosshair.
//
// (3) In your opinion, what does this shape portray?
// Can you find the image that I traced, using Google Image search?
int[] x ={
197,197,199,200,200,201,201,201,200,202,
202,202,201,200,199,200,198,194,192,189,
188,189,191,194,195,198,201,201,203,205,
203,206,205,203,203,206,209,214,215,217,
220,221,223,223,222,219,213,208,204,204,
201,197,192,189,185,182,180,178,176,168,
161,156,153,154,154,150,147,143,141,134,
131,127,123,116,109,107,101,87,77,70,65,
60,59,60,62,65,70,73,73,75,79,84,89,93,
98,100,101,103,107,108,111,112,112,112,
114,117,117,115,115,115,111,110,110,110,
110,109,108,108,109,108,107,106,106,106,
107,108,109,115,120,128,135,138,145,149,
153,157,162,166,169,173,176,181,186,191};
int[] y ={
4,9,11,14,18,23,29,35,39,42,46,49,50,52,
56,60,67,78,82,86,91,99,103,111,118,128,
137,142,153,163,171,182,191,200,204,206,
206,208,209,208,209,210,213,217,221,221,
222,221,221,225,226,227,225,227,226,226,
224,221,220,222,222,221,222,225,228,228,
226,228,228,228,228,227,228,228,220,216,
211,207,201,193,185,175,168,163,161,159,
159,161,165,170,174,179,183,185,186,186,
170,156,144,136,125,121,115,112,108,103,
97,92,87,83,71,66,61,58,56,54,49,45,40,
37,32,27,23,19,13,9,10,13,17,23,30,32,
31,29,29,30,31,31,32,28,24,18,13,9};
hide statement