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

- All Categories 25.7K
- Announcements & Guidelines 13
- Common Questions 30
- Using Processing 22.1K
- Programming Questions 12.2K
- Questions about Code 6.4K
- How To... 4.2K
- Hello Processing 72
- GLSL / Shaders 292
- Library Questions 4K
- Hardware, Integration & Other Languages 2.7K
- Kinect 668
- Arduino 1K
- Raspberry PI 188
- Questions about Modes 2K
- Android Mode 1.3K
- JavaScript Mode 413
- Python Mode 205
- Questions about Tools 100
- Espanol 5
- Developing Processing 548
- Create & Announce Libraries 211
- Create & Announce Modes 19
- Create & Announce Tools 29
- Summer of Code 2018 93
- Rails Girls Summer of Code 2017 3
- Summer of Code 2017 49
- Summer of Code 2016 4
- Summer of Code 2015 40
- Summer of Code 2014 22
- p5.js 1.6K
- p5.js Programming Questions 947
- p5.js Library Questions 315
- p5.js Development Questions 31
- General 1.4K
- Events & Opportunities 288
- General Discussion 365

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:

## Answers

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

exampleIn 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

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.

Thanks again koogs, I appreciate your help.