/
pvector_array_1.pde
98 lines (69 loc) · 2.34 KB
/
pvector_array_1.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
PVectorWarpedSquare[] myPVectorWarpedSquares = new PVectorWarpedSquare[200];
float currentRow = 0;
float currentColumn = 0;
float screenBorder = 20;
void setup() {
size(480,420);
} //<>//
void draw() {
background(204);
smooth();
frameRate(10);
for (int i = 0; i < myPVectorWarpedSquares.length; i=i+1) {
float yGridPosition; // where is each square in the grid: y axis
float xGridPosition; // where is each square in the grid: x axis
yGridPosition = screenBorder + (currentRow*30);
xGridPosition = screenBorder + (currentColumn*30);
if (yGridPosition > (height - (screenBorder + 20 + 11))) {
// println("end of column");
currentColumn = currentColumn + 1;
currentRow = 0;
} else {
currentRow = currentRow + 1; // only increment the row if we're still in the same column
}
myPVectorWarpedSquares[i] = new PVectorWarpedSquare(xGridPosition, yGridPosition, 20); // Initialize objects
}
// Call functions of the objects
for (int i = 0; i < myPVectorWarpedSquares.length; i++) {
myPVectorWarpedSquares[i].display(); // Display each object }
}
// myPVectorWarpedSquare1 = new PVectorWarpedSquare(100, 100, 20);
// myPVectorWarpedSquare1.display();
currentRow = 0;
currentColumn = 0;
}
class PVectorWarpedSquare {
// data
float xPos;
float yPos;
float xWidth;
float yHeight;
color col;
float ran; // randomness
PVector point1;
PVector point2;
PVector point3;
PVector point4;
// constructor, defined with arguments
PVectorWarpedSquare (float xPos_, float yPos_, float ran_) {
xPos = xPos_;
yPos = yPos_;
xWidth = 20;
yHeight = 20;
col = color(0);
ran = ran_;
point1 = new PVector(xPos+random(-ran,ran),yPos+random(-ran,ran));
point2 = new PVector(xPos+xWidth+random(-ran,ran),yPos+random(-ran,ran));
point3 = new PVector(xPos+xWidth+random(-ran,ran),yPos+yHeight+random(-ran,ran));
point4 = new PVector(xPos+random(-ran,ran),yPos+yHeight+random(-ran,ran));
}
// functions
void display(){
//draw shape
strokeWeight(1);
line(point1.x, point1.y, point2.x, point2.y);
line(point2.x, point2.y, point3.x, point3.y);
line(point3.x, point3.y, point4.x, point4.y);
line(point4.x, point4.y, point1.x, point1.y);
}
}