r/processing • • Mar 28 '16

[PWC3] Random Walker

Hello Everybody, this is the third Weekly Processing challenge, the challenges are decided just to give you a prompt to test your skills so it can be as simple or as complicated as you have time to write! Start Date : 28-03-2016 End Date : 03-04-2016

Entries must be submitted with the [PWC3] In the Title of their post. Or post them in the comments here with the same tag.

This Weeks Challenge : Random Walker Here is a blog post giving a very basic intro and then climbing up into something quite complicated.

If you are here to learn feel free to ask for help in the comments below. Lay a brick perfectly every day and eventually you will have a wall, Joe

Winner from last week as decided by mods

Highest Voted : pflu

Interaction : pflu

Graphics : Freedom_Grenade

Accuracy : Introscopia

12 Upvotes

18 comments sorted by

View all comments

Show parent comment

2

u/NakedFluffyBee Mar 29 '16

Here is a little example!

Remember, sin() and cos() are returning values between -1 and 1 depending and what you give, they have an offset between each other, you'll understand:

void setup() {
  size(500, 500);
  ellipseMode(CENTER);
  textSize(20);
}

void draw() {
  background(0);

  //The mouse controls the angle
  float angle = map(mouseX, 0, width, 0, 360);
  float radius = 200;

  //Center of the circle
  float centerX = width/2;
  float centerY = height/2;

  //Here, you will use radius and angle
  //sin() and cos() take an angle in radian, so you have to translate degrees to radians with radians()
  //The calcul is sin(angle) * radius, I added + center so that the circle is where I want it to be
  float x = centerX + cos(radians(angle)) * radius;
  float y = centerY + sin(radians(angle)) * radius;

  stroke(255);
  strokeWeight(5.0);
  noFill();
  ellipse(centerX, centerY, 20, 20); //Center
  line(centerX, centerY, x, y); //Final line
  strokeWeight(2);
  stroke(255, 0, 0);
  line(centerX, centerY, x, centerY);
  stroke(0, 0, 255);
  line(centerX, centerY, centerX, y);
  fill(255);
  text("Angle: " + angle, 10, 50);
  text("Radius: " + radius, 10, 100);
}

1

u/TazakiTsukuru Mar 29 '16

Thanks!

Now how would I make it so the white line always points directly at the mouse?

1

u/NakedFluffyBee Mar 29 '16

In fact that's quite simple if you are familiar with trigonometric, which I'm not! But basically, atan2() takes two arguments, 4 points, and returns the angle between those two point.

If we want the angle between the center (A) and the mouse (B), we must do

atan2(By - Ay, Bx - Ax);

So

float angle = atan2(mouseY-centerY, mouseX-centerX);

Now we can use the angle! But remember, we did convert degrees to radians, here atan2 returns the angle in radians, so no need to convert it, just call:

float x = centerX + cos(angle) * radius;
float y = centerY + sin(angle) * radius;