Question 1: In your own words, please explain the difference between having your for loop from Step 2 in setup()
as opposed to in draw()
.
Having for-loop inside the setup() and draw() is definitely making the big difference because setup() function only runs once, meaning the change will not occur even though you keep your serial open. However, if the for-loop is inside the draw() function, the for-loop will be running continuously, meaning the change will occur constantly.
Question 2: What is the benefit of using arrays? How might you use arrays in a potential project?
Arrays often add efficiency to the code. Like a database, you can add and delete values to and from the code whenever the situation is met (which the situation can be adjusted by the programmer. In addition, by grouping the similar types of values (like x-positions), it is easy to see what the values are for then having 100, for example, variables.
If you use the array, you can go through the values using a loop, which definitely adds efficiency to the code.
I can use arrays in a variety of ways in the potential project. For example, in the recitation 6 coding, I used array to make a snow falling effect. I can make the snow falling effect again or can use an array to simply save the position values.
Code:
PImage mollang; PImage mouth; int amtMollang = 0; float[] xPos = new float[0]; float[] yPos = new float[0]; float[] bodyWidth = new float[0]; float mouthRatio = 0.1269349845; // 41/323 PImage[] mouthLoad = new PImage[amtMollang]; int[] mouthSize = new int[0]; float[] xSpeed = new float[0]; float[] ySpeed = new float[0]; void setup(){ size(960, 600); mollang = loadImage("images/mollang.png"); mouth = loadImage("images/mouth.png"); } void draw(){ background(#DB8B5D); tint(255, 100); //image(mollang, 308, 55); for(int i = 0; i < xPos.length; i++){ drawMollang(xPos[i], yPos[i], bodyWidth[i], mouthLoad[i]); xPos[i] += xSpeed[i]; yPos[i] += ySpeed[i]; if (xPos[i] < 0 || xPos[i] > width){ xSpeed[i] = -xSpeed[i]; } if (yPos[i] < 0 || yPos[i] > height){ ySpeed[i] = -ySpeed[i]; } } } void drawMollang(float x, float y, float mWidth, PImage mouthImg){ float bodyRatio = 1.1176470588; // 361/323 float strokeRatio = 0.03095975232; // 10/323 float earWidthRatio = 0.1517027864; // 49/323 float earHeightRatio = 0.2493074792; // 90/361 float eyesRatio = 0.06811145511; // 22/323 float earDistanceRatio = 0.5761772853; // 92/361 float earX = x - (mWidth * earWidthRatio)/2; float earY = y - ((mWidth * bodyRatio)*earDistanceRatio); float eyesXRatio = 0.213622291; // 69/323 float eyesYRatio = 0.2271468144; // 82/361 float eyesXDistance = (mWidth*eyesXRatio); float eyesY = y - (mWidth * bodyRatio)*eyesYRatio; float cheekXRatio = 0.2879256966; // 93/323 float cheekYRatio = 0.1772853186; // 64/361 float cheekXDistance = (mWidth*cheekXRatio); float cheekY = y - (mWidth * bodyRatio) * cheekYRatio; float cheekRatio = 0.1393188854; // 45/323 float mouthXRatio = 0.05882352941; // 19/323 float mouthYRatio = 0.3405572755; // 110/323 float mouthX = x-(mWidth*mouthXRatio); float mouthY = y - (mWidth*mouthYRatio); fill(255); stroke(#703015); strokeWeight(mWidth * strokeRatio); //ear ellipse(earX, earY, mWidth * earWidthRatio, mWidth * bodyRatio* earHeightRatio); ellipse(earX + ((mWidth * earWidthRatio)*10)/11, earY, mWidth * earWidthRatio, mWidth * bodyRatio* earHeightRatio); //body ellipse(x, y, mWidth, mWidth * bodyRatio); // cheek fill(#FCA1C7); noStroke(); ellipse(x - cheekXDistance, cheekY, mWidth*cheekRatio, mWidth*cheekRatio); ellipse(x + cheekXDistance, cheekY, mWidth*cheekRatio, mWidth*cheekRatio); //println(x - cheekXDistance); // eyes fill(#703015); noStroke(); ellipse(x - eyesXDistance, eyesY, mWidth*eyesRatio, mWidth*eyesRatio); ellipse(x + eyesXDistance, eyesY, mWidth*eyesRatio, mWidth*eyesRatio); // mouth tint(255, 255); image(mouthImg, mouthX, mouthY); } void mousePressed(){ println(pmouseX, pmouseY); xPos = append(xPos, pmouseX); yPos = append(yPos, pmouseY); bodyWidth = append(bodyWidth, random(50, 200)); amtMollang++; mouthLoad = new PImage[amtMollang]; mouthSize = append(mouthSize, int(bodyWidth[amtMollang-1] * mouthRatio)); for(int i = 0; i < amtMollang; i++){ mouthLoad[i] = loadImage("images/mouth.png"); mouthLoad[i].resize(mouthSize[i], 0); } xSpeed = append(xSpeed, random(5, 10)); ySpeed = append(ySpeed, random(5, 10)); //println(mouthSize[amtMollang-1]); }