Skip to content

Instantly share code, notes, and snippets.

@suzukimilanpaak
Created January 4, 2014 04:54
Show Gist options
  • Save suzukimilanpaak/8251910 to your computer and use it in GitHub Desktop.
Save suzukimilanpaak/8251910 to your computer and use it in GitHub Desktop.
Drawing dots with Fibonacci
import java.util.*;
Fibonacci fibonacci;
float unitAngle;
float unitFibonacciAngle;
int NUM_POINTS = 90;
float NUM_DISTANCE_FROM_APEX = 1;
class Fibonacci {
List<Long> array = new ArrayList<Long>();
Fibonacci(int index) {
array = generateFibonacciArray(index);
}
private List<Long> generateFibonacciArray(int index) {
if(array.size() != 0) return array;
for(int i = 0; i <= index; i++) {
if(i == 0) {
array.add((long)0);
}else if(i == 1) {
array.add((long)1);
} else {
array.add(array.get(i - 2) + array.get(i -1));
}
}
return array;
}
public float diff(int i) {
if(i == 0 || i == 1) return 0;
return (float)array.get(i) / (float)array.get(i - 1);
}
}
void setup() {
size(300, 300);
noLoop();
unitAngle = TWO_PI * (1 + sqrt(5)) / 2;
fibonacci = new Fibonacci(NUM_POINTS);
}
void draw() {
background(0);
noStroke();
float r, x, y;
float angle;
for(int i = 0; i < NUM_POINTS; ++i) {
angle = unitAngle * i;
r = i * NUM_DISTANCE_FROM_APEX;
x = 150 + r * cos(angle);
y = 150 + r * sin(angle);
fill(255, 255, 255, 200);
ellipse(x, y, 4, 4);
}
for(int i = 0; i < NUM_POINTS; ++i) {
// With real Fibonacci value
angle = (TWO_PI * fibonacci.diff(i)) * i;
println(i + ": " + fibonacci.array.get(i) + ": " + fibonacci.diff(i));
r = i * NUM_DISTANCE_FROM_APEX;
x = 150 + r * cos(angle);
y = 150 + r * sin(angle);
fill(255, 128, 128, 150);
ellipse(x, y, 4, 4);
}
}
@suzukimilanpaak
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment