if you want make the tree position random, but remember it, sorry need to make
class Tree
the fake 3D effect by scale(fy) would be just depending on the Y position.
// https://discourse.processing.org/t/how-to-go-about-scaling-a-set-of-objects-all-together/6231/3
int many = 20;
Tree[] forest = new Tree[many];
void setup() {
size(400, 400);
smooth();
for (int i=0; i<many; i++) forest[i] = new Tree(random(380), random(5,80));
}
void draw() {
background(100, 100, 20);
for (Tree tree : forest ) tree.draw();
}
class Tree {
float x, y;
Tree(float x, float y) {
this.x=x;
this.y=y;
}
public void draw() {
fill(50, 150, 0);
stroke(50, 145, 0);
pushMatrix();
translate(x, y);
scale(y/40.0);
triangle(5, 143, 15, 143, 10, 0);
triangle(0, 138, 20, 138, 10, 82);
triangle(4, 102, 16, 102, 10, 60);
triangle(6, 72, 14, 72, 10, 40);
popMatrix();
}
}
now made a updated version,
- it draws the tree triangles in a loop
- solves the order problem by first creating a array of random Ypos
sorting them and use them to create the Tree array forest
( together with random Xpos )
code v03
// https://discourse.processing.org/t/how-to-go-about-scaling-a-set-of-objects-all-together/6231/3
// v02 new tree code trunk rect plus for loop 4 triangles reverse order
// v03 work on sort problem small Ypos should draw first
// hack by extra sorted single Y array of randoms
final int many = 20;
Tree[] forest = new Tree[many];
final float vvpff = 50.0; // speak: Virtual View Point Fummel Factor, unit? [m]? above ground
final float yminus = 140; // like z axis, " move tree up (back )" unit [pix]
//___________________________________________________
void setup() {
size(400, 400);
make_forest();
}
//___________________________________________________
void draw() {
background(100, 100, 20);
for (Tree tree : forest ) tree.draw(); // speak: "for each tree ( of type: class Tree ) IN forest execute its draw function"
}
//___________________________________________________
class Tree {
final int tw=10, th=30, tstep=15; // basic triangle spec.
float x, y;
Tree(float x, float y) {
this.x=x;
this.y=y;
}
public void draw() {
pushMatrix();
translate(x, y);
scale(y/vvpff); // size of each tree by its y position, up .. down == small .. big
fill(113, 71, 3);
stroke(113, 81, 3);
rect(-5, th, 10, 60); // trunk
fill(50, 150, 0);
stroke(50, 140, 0);
for (int i = 3; i>=0; i--) triangle(-tw/2*(2+i), th+tstep*i, tw/2*(2+i), th+tstep*i, 0, tstep*i);
popMatrix();
}
}
//___________________________________________________
void make_forest() {
int[] theYs = new int[many]; // hack to solve the sort problem
for (int i=0; i<many; i++) theYs[i] = int(random(20, yminus));
theYs = sort(theYs); // fast normal int array sort
for (int i=0; i<many; i++) {
int posx = int(random(20, width-20));
int posy = theYs[i]; // use the sorted array version
println("i "+i+" x "+nf(posx, 3)+" y "+nf(posy, 3)+" scale "+nf(posy/vvpff, 3, 1));
forest[i] = new Tree(posx, posy);
}
}
that could be the start of a my next assignment:
even if that “single int array sort” might be the easiest and fastest way,
i want learn how to sort the forest array on its Y pos,
like i see here:
https://forum.processing.org/two/discussion/comment/20332/#Comment_20332