by Steve Wang
This is a flower field generated with Javascript. The code for the field has been written by Steve Wang, and the code for the individual flowers has been written by the whole class. I made some code that could arrange different objects into different patterns. Those patterns are 'circle', 'grid', 'cross' and 'random'. I also tried to make it modular so I could carry it over into my future projects. Making this code and improving it took a couple of months, and I am happy with it. However, it is a little messy because everyone made their flowers in different ways and putting it all together makes it a little of a mess. From this project, I feel like I have learnt a lot. I think that now, I am better better at working with other people's code, can now make code easily usable and I am also better at dealing with complex codes. One of the issues I have faced is, I would do what Dr.Bell suggested I do but when he came to check my code, there were always problems I did not think of. Another problem is communication. For example, Dr.Bell would tell me to do something that changes my code significantly and I learnt the importance of having a clear specification at the start of the project. So I feel like I have to try to make my code clearer in some way. This has been really interesting and I look forward to taking on more programming projects.
這是一個用 Javascript 生成的花田。 字段的代碼由 Steve Wang 編寫,單個花的代碼由 全班 編寫。我編寫了一些代碼,可以將不同的對象排列成不同的模式。這些圖案是“圓形”、“網格”、“十字”和“隨機”。我還嘗試將其模塊化,以便我可以將其帶到我未來的項目中。編寫這段代碼並改進它花了幾個月的時間,我對此很滿意。然而,這有點亂,因為每個人都以不同的方式製作他們的花,把它們放在一起會讓它有點亂。從這個項目中,我覺得我學到了很多東西。我認為現在,我更擅長處理其他人的代碼,現在可以使代碼易於使用,我也更擅長處理複雜的代碼。我面臨的一個問題是,我會按照 Dr.Bell 的建議去做,但是當他來檢查我的代碼時,總是有我沒有想到的問題。另一個問題是溝通。例如,Dr.Bell 會告訴我做一些顯著改變我的代碼的事情,我了解到在項目開始時有一個清晰的規範的重要性。所以我覺得我必須嘗試以某種方式使我的代碼更清晰。這真的很有趣,我期待著承擔更多的編程項目。
let colors = ["black", "gray", "red", "white"]; let shapes = ["ellipse", "rect", "triangle"]; class Point { constructor(x, y) { this.x = x; this.y = y; } move(xDistance, yDistance) { return new Point(this.x + xDistance, this.y + yDistance); } moveByAngle(angle, distance) { let r = (angle * Math.PI) / 180; return new Point( this.x + distance * Math.sin(r), this.y + distance * Math.cos(r) ); } } class LucasFantasticFlower { constructor (position,scale,color) { this.color = color; //pick(colors) this.xposition = position.x; this.yposition = position.y; this.size = randomRange(10,200); this.scale=scale } draw () { strokeWeight(1) scale(this.scale) fill(this.color) ellipse(this.xposition + 56,this.yposition + 42,36,34) ellipse(this.xposition + 56,this.yposition + 71,36,34) ellipse(this.xposition + 80,this.yposition + 80,36,34) ellipse(this.xposition + 107,this.yposition + 65,36,34) ellipse(this.xposition + 107,this.yposition + 42,36,34) ellipse(this.xposition + 80,this.yposition + 25,36,34) fill("white") ellipse(this.xposition + 76,this.yposition + 55,33,34) } } class YilerFantasticFlower { constructor(position,scale,color) { this.size = 40.5; this.space = 22.5; this.leafSpace = 23 this.pColor = color this.mColor = "undifined" this.scale=scale this.positionx=position.x this.positiony=position.y } draw() { scale(this.scale) this.mColor = "green"; noStroke(); fill(this.mColor); ellipse( this.positionx, this.positiony + this.leafSpace, 75, 25); fill(this.pColor); ellipse( this.positionx + this.space, this.positiony, this.size ); ellipse( this.positionx - this.space, this.positiony, this.size ); ellipse( this.positionx, this.positiony - this.space, this.size ); ellipse( this.positionx, this.positiony + this.space, this.size ); fill(this.mColor); ellipse( this.positionx, this.positiony, this.size + 10, this.size + 10 ); } } class RonFantasticFlower{ constructor (position,scale,color) {this.color = color; this.fantasticflowersize=scale/2; this.fantasticflower=color; this.xposition = position.x; this.yposition = position.y; this.petalNumber=randomRange(30,50) } draw(){ scale(this.fantasticflowersize); translate(this.xposition,this.yposition) for (let i = 0; i < this.petalNumber ; i++) { rotate(27) fill("white") ellipse(50,20,20,150) fill(this.color) rect(40,0,20,70)} resetMatrix() } } class RexFantasticFlower { constructor(position,scale,color) { this.color = color; this.xposition = position.x; this.yposition = position.y; this.size = 40; this.scale=scale } draw() { translate(this.xposition, this.yposition+10); scale(this.scale) fill(191, 170, 191); rect(35, 20, 14, 135, 20); rect(5, 64, 55, 25, 20); rect(30, 64, 55, 25, 20); fill(this.color); rect(0, 20, this.size, this.size, 20); rect(30, 20, this.size, this.size, 20); fill(191, 170, 191); rect(30, 35, 25, 25, 10); resetMatrix(); } } class IngramFantasticFlower { constructor (position,scale,color) { this.color = color; this.xposition = position.x; this.yposition = position.y; this.size = randomRange(150,200); this.scale=scale/5 } draw () { fill(this.color); translate(this.xposition,this.yposition); scale(this.scale) rotate(radians(40)); ellipse(75, 0, this.size, 50); rotate(radians(40)); ellipse(75, 0, this.size, 50); rotate(radians(40)); ellipse(75, 0, this.size, 50); rotate(radians(40)); ellipse(75, 0, this.size, 50); rotate(radians(40)); ellipse(75, 0, this.size, 50); rotate(radians(40)); ellipse(75, 0, this.size, 50); rotate(radians(40)); ellipse(75, 0, this.size, 50); rotate(radians(40)); ellipse(75, 0, this.size, 50); rotate(radians(40)); ellipse(75, 0, this.size, 50); resetMatrix() } } class BrendanFantasticFlower { constructor(position,size,color){ this.x = position.x; this.y = position.y; this.size=size/4; this.color=color } draw(){ translate(this.x, this.y); scale(this.size); stroke("blue") fill(this.color) for (var i = 0; i < 10; i ++) { ellipse(50, 80, 20, 80); rotate(PI/5); circle(50, 50, 50); } resetMatrix(); stroke("black") } } class ZoeyFantasticFlower { constructor(position,scale,color) { this.xposition = position.x; this.size = 50 this.scale=scale this.flowerColor = color; this.yposition = position.y this.petalN = Math.floor(randomRange(5,10)); } draw() { noStroke(); translate(this.xposition, this.yposition); scale(this.scale) fill(this.flowerColor) for (let i = 0; i < this.petalN; i++) { ellipse(0, 0, this.size, this.size/2); rotate(PI / 5); } fill(255); circle(0, 0, this.size/3); resetMatrix(); stroke(1) }} class YunaFantasticFlower { constructor (position,scale,color) { this.size = randomRange(25, 45); this.color = color; this.positionX = position.x; this.positionY = position.y; this.scale=scale } draw () { strokeWeight(2); fill(this.color) scale(this.scale) ellipse(this.positionX+180-155, this.positionY+0, this.size,this.size); ellipse(this.positionX+165-155, this.positionY+180-130, this.size,this.size); ellipse(this.positionX+0, this.positionY+150-130, this.size,this.size); ellipse(this.positionX+197-155, this.positionY+181-130, this.size,this.size); ellipse(this.positionX+205-155, this.positionY+150-130, this.size,this.size); fill(245, 233, 203); ellipse(this.positionX+180-155, this.positionY+160-130, 40, 40); fill("black") ellipse(this.positionX+172-155, this.positionY+156-130, 5, 15); ellipse(this.positionX+185-155, this.positionY+156-130, 5, 15); resetMatrix(); } } class LarissaFantasticFlower { constructor (position,scale,color) { this.color = color; this.shape=['ellipse', 'rect','circle'] this.shape = pick(this.shape); this.xposition = position.x; this.yposition = position.y; this.size = scale; } draw() { translate(this.xposition-20,this.yposition) scale(this.size) fill(this.color); if (this.shape == 'circle') {for(let i = 0; i < 7; i++){ circle(0,60,80);rotate(PI/3) }} if (this.shape == 'rect') {for(let i = 0; i < 7; i++){rect(0,60,80,80);rotate(PI/3) }} if(this.shape=="ellipse") {for(let i = 0; i < 7; i++){ellipse(0,60,80,160);rotate(PI/3) }} fill(252, 252, 144) circle(0, 0, 80); resetMatrix() } } class AnnaFantasticFlower { constructor(point,individualObjectSize, patternColor) { this.xPosition = point.x; this.yPosition = point.y; this.individualObjectSize=individualObjectSize this.colorsArray = [ "lightCoral", "salmon", "coral", "tomato", "lavender", "orange", "yellowGreen", "cornsilk", "navajoWhite", "wheat", "white", "gainsboro", "mistyRose", ]; this.shape = ["ellipse", "rect"]; this.color = patternColor; this.shape = pick(this.shape); this.stroke = 1; if (this.shape == "rect") { this.stroke = 5; } } draw() { scale(this.individualObjectSize) translate(this.xPosition, this.yPosition); fill(this.color); for (let i = 0; i < 9; i++) { rotate(40); rect(2, 0, 20, 10); } resetMatrix(); } } class OliverFantasticFlower { constructor(position, size, inputColor) { this.shape = ["ellipse", "circle", "square", "rect"]; this.colors = ["green", "blue", "black", "pink", "lavender"]; this.scale = size; this.x = position.x; this.y = position.y; this.size = 1.02 * 22; this.petal = Math.floor(Math.random() * 10); this.choice = ["random"]; this.randomOrNot = pick(this.choice); this.inputColor = inputColor; this.shapeSelector = pick(this.shape); this.colorSelector = pick(this.colors); this.rotation = 0; if (this.colorSelector == "red" && this.x < 600) { this.petal = 5; } else if (this.colorSelector == "lavender" || this.x >= 600) { this.petal = 9; } else if (this.colorSelector != "yellow" || this.x >= 1200) { this.petal = 7; } if (this.petal == 1) { this.petal += 1; } switch (this.petal) { case 10: this.rotation = 4; break; case 9: this.rotation = 3.5; break; case 8: this.rotation = 3.25; break; case 7: this.rotation = 3; break; case 6: this.rotation = 2.75; break; case 5: this.rotation = 2.5; break; case 4: this.rotation = 2; break; case 3: this.rotation = 1.75; break; case 2: this.rotation = 1.5; break; } this.r = 255; this.g = 255; this.b = 255; if (this.randomOrNot != "random" && this.randomOrNot == "notRandom") { this.c = this.inputColor; } else { this.c = this.colorSelector; } } draw() { scale(this.scale); translate(this.x, this.y); for (let i = 0; i < this.petal; i++) { fill(this.c); if (this.colorSelector == "lavender") { fill(204, 153, 255); } else { fill(this.c); } switch (this.shapeSelector) { case "rect": rect(0, 0, this.size, this.size / 2); fill(this.g, this.b, this.r); rect(5, 4, this.size / 2, this.size / 3); break; case "square": square(0, 0, this.size); fill(this.b, this.r, this.g); square(10, 5, this.size / 2); rect(50, 0, this.size / 2, this.size / 3); break; case "circle": circle(10, 0, this.size); fill(this.r, this.b, this.g); circle(10, -10, this.size / 2); break; case "ellipse": ellipse(0, 0, this.size, this.size / 2); fill(this.r, this.g, this.b); ellipse(0, 0, this.size / 1.4, this.size / 2); } rotate(PI / this.rotation); fill("yellow"); circle(0, 0, this.size / 2); fill("orange"); circle(0, 0, this.size / 3); this.r = 250 - 20 * i; this.b = 250 - 40 * i; this.g = 250 - 20 * i; } resetMatrix(); } } var canvasSize = new Point(1700, 1700); class OliviaFantasticFlower { constructor(x, individualObjectSize, patternSize) { this.position = x; this.shape = "Flower"; this.colors = ["white", "yellow", "green", "darkgreen"]; this.color = patternSize; this.size = randomRange(10, 35); this.width = this.size; this.height = this.size; this.individualObjectSize = individualObjectSize; this.petalN = 8; this.flowerA = []; } draw() { scale(this.individualObjectSize); fill(this.color); this.flowerA = oliviaCircularPositions( this.position.x, this.position.y, this.petalN, 10 ); this.flowerA.forEach((x) => circle(x.x, x.y, this.size)); circle(this.position.x, this.position.y, this.size); } } function pick(inputArray) { return inputArray[Math.floor(inputArray.length * Math.random())]; } function randomRange(min, max) { return min + (max - min) * Math.random(); } function setup() { noLoop(); createCanvas(canvasSize.x, canvasSize.y); frameRate(20); } function buildArray(n, fillFunction) { let outputArray = []; for (let i = 0; i < n; i++) { outputArray.push(fillFunction(i)); } return outputArray; } function gridPattern(nOfRows, nInRow, itemX, itemY, startX, startY) { let yP = startY; let returnA = []; for (let i = 0; i < nOfRows; i++) { yP += itemY + 2; for (let b = 0; b < nInRow[i]; b++) { returnA.push(new Point((itemX + 5) * b + startX, yP)); } } return returnA; } function randomPattern(nOfItems, minX, minY, maxX, maxY) { let returnA = []; for (let i = 0; i < nOfItems; i++) { returnA.push(new Point(randomRange(minX, maxX), randomRange(minY, maxY))); } return returnA; } function crossPatterns(nOfCrosses, crossPosition, widthOfCross, shapeSize) { let realShapeSize = shapeSize + 2; let returnArray = []; let angle = 0; for (let i = 0; i < nOfCrosses; i++) { returnArray.push(new Point(crossPosition[i].x, crossPosition[i].y)); for (let b = 0; b < 4; b++) { angle += 90; if (b == 0) { angle = 0; } for (let a = 0; a <= widthOfCross[i]; a++) { returnArray.push( crossPosition[i].moveByAngle(angle, realShapeSize * a) ); } } } return returnArray; } function oliviaCircularPositions(startingX, startingY, numberOfShapes, cSize) { let positionArray = buildArray( numberOfShapes, (i) => new Point(startingX, startingY) ); let testCircle = circlePattern(cSize, positionArray); return testCircle; } function circularPositions(nOfCircle, positions, nOfShapes, cSize) { let arrayToReturn = []; for (let i = 0; i < nOfCircle; i++) { let positionArray = buildArray( nOfShapes[i], (b) => new Point(positions[i].x, positions[i].y) ); let testCircle = circlePattern(cSize, positionArray); arrayToReturn = arrayToReturn.concat(testCircle); } return arrayToReturn; } function circlePattern(cSize, cPosition) { let angle = 360 / cPosition.length; return cPosition.map((p, i) => { return p.moveByAngle(i * angle, cSize); }); } function duplicateArray(times, arrayToCopy) { let returnArray = []; for (let i = 0; i < times; i++) { returnArray.push(arrayToCopy); } return returnArray; } //Replace in arrow function in 156 let reducer = (previousValue, currentValue) => previousValue + currentValue; function addingWholeArray(arrayToAdd) { return arrayToAdd.reduce(reducer); } let i = 0; class fantasticFlower { constructor(flowerXY, individualObjectSize, patternColor) { this.flowerX = flowerXY.x; this.flowerY = flowerXY.y; this.flowerColor = patternColor; this.petalsColor = patternColor; this.flowerSize = randomRange(10, 40); this.petalWidth = randomRange(10, 30); this.petalHeight = randomRange(10, 30); this.nOfPetals = randomRange(1, 10); this.individualObjectSize = individualObjectSize; } draw() { scale(this.individualObjectSize); if (this.flowerColor == "black") { this.flowerColor = "white"; } if (this.petalsColor == "black") { this.petalsColor = "white"; } i += 1; translate(this.flowerX, this.flowerY); rotate(i); if (this.petalsColor == "black") { this.petalsColor = "white"; } fill(this.petalsColor); let petalsRotate = 360 / this.nOfPetals; for (let a = 0; a < this.nOfPetals; a++) { rotate(petalsRotate); ellipse(0, -10, 10 + this.petalWidth, 20 + this.petalHeight); } fill(this.flowerColor); circle(0, 0, this.flowerSize); resetMatrix(); } } function arrayRandomPicker(arraytoPickFrom1, arraytoPickFrom2) { if (arraytoPickFrom1.length != arraytoPickFrom2.length) { return "The legnth of both arrays are not the same", false; } let returnArray = []; let pick = 0; for (let b = 0; b < arraytoPickFrom1.length; b++) { pick = randomRange(0, 2); if (pick == 0) { returnArray.push(arraytoPickFrom1[b]); } else { returnArray.push(arraytoPickFrom2[b]); } } return returnArray; } function iInLoopedArray(shorterArray, longerArray) { let returnArray = []; for (let a = 0; a < longerArray.length; a++) { returnArray.push(shorterArray[a % shorterArray.length]); } return returnArray; } class FlowerField { constructor( patternName, positionArray, itemSize, widthArray, individualObjectSize, patternColor ) { if (positionArray.length != widthArray.length) { if (positionArray.length < widthArray.length) { this.widthArray = widthArray; this.positionArray = iInLoopedArray(positionArray, widthArray); } else { this.widthArray = iInLoopedArray(widthArray, positionArray); this.positionArray = positionArray; } } else { this.positionArray = positionArray; this.widthArray = widthArray; } this.itemSize = itemSize; this.newPositions = this.adjustPosition( patternName, canvasSize, this.positionArray, this.itemSize, this.widthArray ); this.flowersArray = this.newPositions.map( (x) => new fantasticFlower(x, individualObjectSize, patternColor) ); this.LucasNow = this.newPositions.map( (x) => new LucasFantasticFlower(x, individualObjectSize, patternColor)); this.YilerNow = this.newPositions.map( (x) => new YilerFantasticFlower(x, individualObjectSize, patternColor)); this.RonNow = this.newPositions.map( (x) => new RonFantasticFlower(x, individualObjectSize, patternColor)); this.RexNow = this.newPositions.map( (x) => new RexFantasticFlower(x, individualObjectSize, patternColor)); this.IngramNow = this.newPositions.map( (x) => new IngramFantasticFlower(x, individualObjectSize, patternColor)); this.BrendanNow = this.newPositions.map( (x) => new BrendanFantasticFlower(x, individualObjectSize, patternColor)); this.YunaNow = this.newPositions.map( (x) => new YunaFantasticFlower(x, individualObjectSize, patternColor) ); this.LarissaNow = this.newPositions.map( (x) => new LarissaFantasticFlower(x, individualObjectSize/5, patternColor) ); this.ZoeyNow = this.newPositions.map( (x) => new ZoeyFantasticFlower(x, individualObjectSize/5, patternColor) ); this.AnnaNow = this.newPositions.map( (x) => new AnnaFantasticFlower(x, individualObjectSize, patternColor) ); this.OliviaNow = this.newPositions.map( (x) => new OliviaFantasticFlower(x, individualObjectSize, patternColor) ); this.OliverNow = this.newPositions.map( (x) => new OliverFantasticFlower(x, individualObjectSize, patternColor) ); this.flowersArray = arrayRandomPicker(this.flowersArray, this.OliviaNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.LucasNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.YilerNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.RonNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.RexNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.IngramNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.BrendanNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.YunaNow) this.flowersArray = arrayRandomPicker(this.flowersArray, this.ZoeyNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.LarissaNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.AnnaNow); this.flowersArray = arrayRandomPicker(this.flowersArray, this.OliverNow); return this.flowersArray; } help(patternName) { console.log("patternHelp Output:"); console.log( "If unsure what format to type arguments in here are the formats for arguments: (string, Point class, array filled with Point class, number, number, array)" ); console.log( "These are the instructions for:", patternName, "Instructions will alow you to understand what to type for each argument" ); if (patternName.includes("cross")) { console.log( "(Specify Pattern name,totalItems(number), Positon of crosses, Position between each shape, itemSize, Width of cross" ); } else if (patternName.includes("circle")) { console.log( "(Specify Pattern name, totalItems, Positon of circles, Position between the shapes that outline circle, itemSize, An array with the amount of items that will outline each circle" ); } else if (patternName.includes("random")) { console.log( "(Specify Pattern name, totalItems, Invalid because random positions,Invalid because randomPosition, itemSize, Invalid because each item will be on their own" ); } else if (patternName.includes("grid")) { console.log( "(Specify Pattern name, totalItems, where to start drawing grid (array with a point class), the size of each item (number), nO amount of items on each row" ); } else { console.log( "Invalid pattern name: the only patterns are cross, circle, grid, random", "End of pattenHelp" ); return false; } return "End of pattenHelp"; } adjustPosition( patternName /*(string)*/, canvasSize /*(Point class)*/, midPoints /*(array filled with Point class)*/, itemSize /*(number)*/, PatternWidth /*(array)*/ ) { if ( patternName.includes("cross") || patternName.includes("circle") || patternName.includes("random") || patternName.includes("grid") ) { if (patternName.includes("cross")) { let nOfCrosses = midPoints.length; let totalWidths = addingWholeArray(PatternWidth); let shapeSize = itemSize; let returnArray = []; return crossPatterns(nOfCrosses, midPoints, PatternWidth, shapeSize); } if (patternName.includes("circle")) { let arrayToReturn = []; let shapeSize = itemSize; let nOfCircle = midPoints.length; let nOfShapes = PatternWidth; let ps = circularPositions( nOfCircle, midPoints, PatternWidth, shapeSize ); for (let b = 0; b < ps.length; b++) { arrayToReturn = arrayToReturn.concat(ps[b]); } return arrayToReturn; } if (patternName.includes("random")) { let arrayToReturn = []; for (let i = 0; i < midPoints.length; i++) { let minXY = new Point(midPoints[i].x, midPoints[i].y); let maxX = 0; let maxY = 0; maxX = midPoints[i].x + itemSize; maxY = midPoints[i].y + itemSize; if (itemSize > canvasSize.x || itemSize > canvasSize.y) { if (itemSize > canvasSize.x) { maxX = canvasSize.x; } if (itemSize > canvasSize.y) { maxY = canvasSize.y; } } let maxXY = new Point(maxX, maxY); let arrayToJoin = randomPattern( PatternWidth[i], minXY.x, minXY.y, maxXY.x, maxXY.y ); arrayToReturn = arrayToReturn.concat(arrayToJoin); } return arrayToReturn; } if (patternName.includes("grid")) { let arrayToReturn = []; for (let i = 0; i < midPoints.length; i++) { let rowNumber = PatternWidth[i]; let gridInfo = duplicateArray(rowNumber, PatternWidth[i]); let itemXYsizes = new Point(itemSize, itemSize); let startXY = new Point(midPoints[i].x, midPoints[i].y); let arrayToJoin = gridPattern( gridInfo.length, gridInfo, itemXYsizes.x, itemXYsizes.y, startXY.x, startXY.y ); arrayToReturn = arrayToReturn.concat(arrayToJoin); } return arrayToReturn; } else { console.log( "Invalid patternName", "If help needed please type: patternHelp(patternName)" ); return false; } } } XYSet(i) { return new Point( randomRange(0, canvasSize.x), randomRange(0, canvasSize.y) ); } } let widthArray = [3]; //Flowers are made here //Making flowers let flowersArray = buildArray(4, (b) => buildArray(20, (i) => arrayOfFlowerFields( 10, 280, "horizontal", i, 200 + 400 * b, widthArray, 0.5, "cross" ) ) ); let flowersArray2 = buildArray(4, (b) => buildArray(20, (i) => arrayOfFlowerFields( 10, 280, "horizontal", i, 2000 + 400 * b, widthArray, 0.5, "cross" ) ) ); flowersArray = flowersArray.concat(flowersArray2); function arrayOfFlowerFields /*Call with forEach*/( amountOfPatterns, distanceBetween, direction, counter, linePosition, widthArray, individualObjectSize /*will times this amount*/, specificOrRandomPattern ) { let patternColor = ""; let spaceforPattern = 0; let spaceForPatternX = 0; let spaceForPatternY = 0; let rangeOfPattern = []; let patterns = ["random", "circle", "cross", "grid"]; if (specificOrRandomPattern == "random") { let drawPattern = pick(pattern); } let drawPattern = specificOrRandomPattern; if (direction == "horizontal") { let position = [new Point(distanceBetween * counter, linePosition)]; spaceForPatternX = canvasSize.x / amountOfPatterns; if (spaceForPatternX != 0) { spaceForPatternY = canvasSize.y; } else { spaceForPatternX = canvasSize.x; } if (spaceForPatternX > spaceForPatternY) { spaceforPattern = spaceForPatternY; } else { spaceforPattern = spaceForPatternX; } if (direction == "horizontal") { rangeOfPattern = [ new Point(distanceBetween * counter, linePosition), new Point(distanceBetween * counter + spaceforPattern, linePosition), ]; } else { rangeOfPattern = [ new Point(linePosition, distanceBetween * counter), new Point(linePosition, distanceBetween * counter + spaceforPattern), ]; } let midPoint = distanceBetween * counter + spaceforPattern / 2; if (drawPattern == "random") { if (direction == "horizontal") { position = [new Point(midPoint, linePosition)]; itemSize = spaceforPattern; patternColor = pick(colors); } } if (drawPattern == "circle") { position = [new Point(midPoint, linePosition)]; itemSize = (spaceforPattern / amountOfPatterns) * 4; patternColor = pick(colors); } if (drawPattern == "grid") { position = [new Point(distanceBetween * counter, linePosition - 200)]; itemSize = rangeOfPattern[0].y / 4; patternColor = pick(colors); } if (drawPattern == "cross") { position = [new Point(midPoint, linePosition)]; itemSize = 50; patternColor = pick(colors); } return new FlowerField( drawPattern, position, itemSize, widthArray, individualObjectSize, patternColor ); } else { let position = [new Point(linePosition, distanceBetween * counter)]; spaceForPatternY = (canvasSize.y - 100) / amountOfPatterns; if (spaceForPatternY != 0) { spaceForPatternY = canvasSize.y - 100; } else { spaceForPatternY = canvasSize.y - 100; } if (spaceForPatternX > spaceForPatternY) { spaceforPattern = spaceForPatternY; } else { spaceforPattern = spaceForPatternX; } rangeOfPattern = [ new Point(linePosition, distanceBetween * counter), new Point(linePosition, distanceBetween * counter + spaceforPattern), ]; let midPoint = distanceBetween * counter + spaceforPattern / 2; if (drawPattern == "random") { position = [new Point(linePosition, midPoint)]; itemSize = spaceforPattern; patternColor = pick(colors); } if (drawPattern == "circle") { position = [new Point(linePosition, midPoint)]; itemSize = (spaceforPattern / amountOfPatterns) * 4; patternColor = pick(colors); } if (drawPattern == "grid") { position = [new Point(linePosition, distanceBetween * counter)]; itemSize = rangeOfPattern[0].y / 3; patternColor = pick(colors); } if (drawPattern == "cross") { position = [new Point(linePosition, midPoint)]; itemSize = 30; patternColor = pick(colors); } return new FlowerField( drawPattern, position, itemSize, widthArray, individualObjectSize, patternColor ); } } function draw() { background(54, 166, 85); stroke(2); //shapesNow.forEach((x) => x.draw()); // flowersArray.forEach((x) => x.draw()); //flowersArray.forEach((x) => x.forEach((y) => y.draw())); flowersArray.forEach((z) => z.forEach((x) => x.forEach((y) => y.draw()))); } function shapeFromPoints(pointArray) { beginShape(); pointArray.forEach((p) => vertex(p.x, p.y)); endShape(CLOSE); } function buildArray2(n, fillFunction) { let outputArray = []; for (let i = 0; i < n; i++) { outputArray.push(fillFunction); } return outputArray; } function randomRange(min, max) { return Math.floor(min + (max - min) * Math.random()); } function head(array) { return array.splice(0, 1)[0]; } //Last item of array function last(array) { return array.splice(array.length - 1, 1)[0]; }