2

I take a value from the mouse (mouseX) position in Processing, as this value gets higher, I want the amount of boxes that are rendered to lower in steps of 5 (rotStep).

I did a lot of reading and found out the method I should use is called 'Negative correlation' or at least I think it is. I have never had high-grade math so I'm pretty much in the dark here. Maybe there is a function that already exists to do this. After a lot of googling I came in here to ask.

Tried dividing the mouseX input by itself and some other random sums but it seems this might be more complicated than I anticipated.

I am trying to get into generative art generation and could use a hint to get further with my attempt of rendering more boxes (quads) as the mouseX value lowers.

void setup() {
  pixelDensity(displayDensity());
  size(500, 500);
  background(0);
  noFill();
  stroke(255);
}

void draw() {
  translate(width/2, height/2);
  ellipse(0, 0, 50, 50);
  background(0);
  mouseX= constrain(mouseX, 1, width);
  mouseY= constrain(mouseY, 1, height);

  float rotationMax = 90;
  float rotStep = (mouseX/15)+5;

  //I need to add a negative correlation so the number
  //of squares lowers as the mouseX position gets higher
  //and all this in steps of 5


  float quadSize = mouseX;
  float qs = quadSize;

  for (float i=0; i<rotationMax; i+=rotStep) {
    float deg = rotStep;
    float rad = radians(deg);
    stroke(255);
    strokeWeight(1);
    rotate(rad);
    quad(-qs, -qs, qs, -qs, qs, qs, -qs, qs);
  }
}

The rotStep variable should decrease when the mouseX variable increases and vice versa. The variable rotStep should also increase or decrease in steps of 5.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Marinus
  • 39
  • 5

1 Answers1

3

The best advice I can give you is to get out a piece of paper and a pencil, and draw a table of mouse positions and the number of boxes you want. It might look like this:

mouseX  boxes
---------------
0      | 50
100    | 40
200    | 30
300    | 20
400    | 10
500    | 0

This is just an example, so your numbers would probably be different. But the idea is to have a general mapping of mouseX to the number of boxes you want to draw.

Once you have that, then you can try to find an equation that gets you from mouseX to your box count. That might be a single equation, or it might involve if statements to bucket values together.

You can get a "negative correlation" by subtracting from the maximum possible value, or by using mouseX as a divisor.

float reverseMouseX = width - mouseX;
float inverseMouseX = 1 / mouseX;

For both of these approaches, as mouseX increases, the value of the variable will decrease. Then you can use these values in your equation or in your if statement logic.

To get to the example table I showed above, I might do something like this:

int boxes = (width - mouseX) / 10;

This is a general approach, but you can apply it to your goal to come up with a specific solution.

Good luck!

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thanks for your hints, with the reverseMouseX I have managed to get the negative correlation. Right now I am trying to get the steps right. I have done a division on the output of rotational steps and 'floored' the result. Then the result multiplicated by 5 to get a count going up in steps of 5 and this works well. Now I'm running into yet another unwanted result as the generated boxes only start to generate when the number is very low and every time I change my formula to compensate, the script crashes. I think I need a nights rest to think about it. Thanks for your advice and edit so far! – Marinus Aug 23 '19 at 18:57
  • 1
    @Marinus You're welcome. I suggest putting together a [mcve] of your new problem and posting it in its own question. Good luck! – Kevin Workman Aug 23 '19 at 20:40
  • 1
    Just t let you know, this is the result in three posts of what I managed to do with your help. So thanks a lot! https://twitter.com/ABlahstar84/status/1165169185632280576 – Marinus Aug 24 '19 at 08:17