#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

#### In this Discussion

edited September 2017

Greetings

I hope some one can point me in the right direction. I am attempting to recreate this fractal using a game of chaos with a random die roll. Would someone who has better knowledge of PVectors please elaborate on where my code is failing? I also would appreciate constructive critique on how to better approach this problem?

``````PVector pA, pB, pC, cPos, nPos;
int goal=100000;

void setup() {
size(800,800);
background(50);
pA = new PVector(width/2, 100);
pB = new PVector(100,700);
pC = new PVector(700,700);
cPos = new PVector();
nPos = new PVector();
fill(255,0,0);
ellipse (pA.x, pA.y, 10, 10);
fill(0,255,0);
ellipse (pB.x, pB.y, 10, 10);
fill(0,0,255);
ellipse (pC.x, pC.y, 10, 10);

fill(220);
translate(width/2,height/2);
for (int i=0; i<=goal; i++){
int x = int(random(1,7));

if (x<3) {
nPos = PVector.sub(pA,cPos);
nPos.div(2);
ellipse (nPos.x, nPos.y, 10, 10);
}else if ((x>2) && (x<5)){
nPos = PVector.sub(pB,cPos);
nPos.div(2);
ellipse (nPos.x, nPos.y, 10, 10);
}else if ((x>4) && (x<7)){
nPos = PVector.sub(pC,cPos);
nPos.div(2);
ellipse (nPos.x, nPos.y, 10, 10);
}

cPos=nPos;
}
``````
Tagged:

• How is the code failing? Describe the problem

• I am not quite eloquent so I'm attaching a picture of what it generates. The red, blue and green ellipses represent the corners of the triangle, the white ellipses represent the random instances triggered by the die roll. I think my math is somehow off for the pattern that is generated is completely different from this example

In a nutshell, I am trying to draw a new ellipse halfway between the last white ellipse drawn and it's corresponding corner (Red (Die 1-2)|Green (3-4)|Blue(5-6))

Thank you for taking the time to respond koogs, I appreciate it. Do you see something obvious in the code that might be causing this result?

• a few things...

the next point is halfway between the current point and the chosen control point. halfway between a and b is (a + b) / 2. there's no subtract.

if all your control points are visible on the screen then all the generated points will be. you don't need the translate (alternatively it needs applying before you draw the control points, which will make some of them off screen)

the random(7) is a bit odd. why not switch(int(random(3))) and have cases 0, 1 and 2?

• Maybe it's just the translate

• (it wasn't)

• Using case statements is a much better solution, thank you. As for the PVector.sub(), I was following Daniel Shiffman's example titled "VectorMath" that comes with the processing install. Here's a snippet of the code

`````` // A vector that points to the mouse location
PVector mouse = new PVector(mouseX,mouseY);
// A vector that points to the center of the window
PVector center = new PVector(width/2,height/2);
// Subtract center from mouse which results in a vector that points from center to mouse
mouse.sub(center);
// Normalize the vector
mouse.normalize();
// Multiply its length by 150 (Scaling its length)
mouse.mult(150);
``````

I was hoping to use the same methodology to generate a new vector pointing to one of the "control" points. If I don't translate, the white ellipses are drawn to the upper left hand corner. Here's a picture

Can you explain a bit more as to why the midpoint of two vectors is (V1+V2)/2; this feels more like an average of the two in my opinion, but I could be wrong. Thanks again koogs and chrisir, I appreciate your feedback.

What is the midpoint if it's not the average? What's halfway between a and b?

( More strictly it's a plus half the vector from a to b, so a+(b-a)/2 == a+b/2-a/2 == a/2+b/2 == (a+b)/2 )

Did you try replacing the sub with add?

• Genius, seems to be working, I will rework the code so it loops infinitely but I am seeing a recognizable pattern. Thank you for the input, I appreciate it

• Can you explain why line 6 of the example code above behaves the way it does?

• Not from that snippet.

If he's using translate then that might be something to do with mapping the mouse position (in screen coords) to the translated coords of the gasket. Would need to see the whole sketch though.

• I think I understand a little bit more now. I believe the sub() returns a PVector originating from 0,0 regardless of the mathematical operation being performed on the PVectors.

Here's the entire code from the example.

``````/**
* Vector
* by Daniel Shiffman.
*
* Demonstration some basic vector math: subtraction, normalization, scaling
* Normalizing a vector sets its length to 1.
*/

void setup() {
size(640,360);
}

void draw() {
background(0);

// A vector that points to the mouse location
PVector mouse = new PVector(mouseX,mouseY);
// A vector that points to the center of the window
PVector center = new PVector(width/2,height/2);
// Subtract center from mouse which results in a vector that points from center to mouse
mouse.sub(center);

// Normalize the vector
mouse.normalize();

// Multiply its length by 150 (Scaling its length)
mouse.mult(150);

translate(width/2,height/2);
// Draw the resulting vector
stroke(255);
strokeWeight(4);
line(0,0,mouse.x,mouse.y);

}
``````
• Thanks again koogs, I appreciate your help.