We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I am trying to create a sketch in which you can place black squares with a key input and then hit a 'Populate' button to show a dot density around the placed squares. I want the density of dots to reduce the further away it gets from a black square. At the minute I am using nested for loops and probability areas surrounding the black square to say if the for loop is in a 20 pixel radius of the square then there is a 50% chance that a point will be placed at the location. (Please see attached script).
There are a couple of problems with this though. It isn't a gradual change from dense to less dense and also i am struggling to work out how to place more black squares with a key input and then get the sketch to run a similar density calculation on the placed squares......it's all a little confusing, and I'm not quite sure what i'm missing...
Any help would be much appreciated. Thanks
import controlP5.*;
int factoryX = 300;
int factoryY = 300;
int factoryW = 20;
int factoryH = 20;
float prob;
ControlP5 switchButton;
Button populate;
void setup() {
size(600, 600);
noStroke();
smooth();
switchButton = new ControlP5(this);
populate = switchButton.addButton("Populate Map")
.setPosition(50, 50)
.setSize(100, 20)
.setSwitch(true)
.setOff();
}
void draw() {
background(180);
fill(0);
rect(factoryX, factoryY, factoryW, factoryH);
switchButton.draw();
if (populate.booleanValue() == true) {
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
float r = random(1);
if ((i > factoryX-20 && i<factoryX + factoryW+20) && (j>factoryY-20 && j<factoryY + factoryH+20)) {
if ( r < 0.5) {
stroke(#B200FF);
point(i, j);
}
}
}
}
}
if (keyPressed) {
if (key == 'f' || key == 'F') {
rect(mouseX, mouseY, factoryW, factoryH);
}
}
}
Answers
If you want your dot population to become less dense farther away, unless there's some other hidden way that I don't know about, you won't be able to do this without PVector (unless, of course, you want to do the hypotenuse calculations yourself... ;) )
If you want to have the ability to place multiple squares that do the same thing, this is where classes come in. They will save you a lot of time and space.
Just research the two methods. They're both in the Processing reference.
When I said that this is where classes come in, I didn't remember that it's harder to make an array of object in classes than in an ArrayList, which is what I actually meant.
BTW, I’m working on the code right now, so just sit back for now.
I was playing with this, but now i have to leave, Still some dumbness I've already detected, an some i haven't, but it might be an idea.
I haven't got as far as the placement of multiple squares, but I have some code for your required gradual change. Here:
Hope that helps.
-- MenteCode