Skip to content

Instantly share code, notes, and snippets.

@2bbb
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save 2bbb/69817566039370313373 to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/69817566039370313373 to your computer and use it in GitHub Desktop.
Sir Roger Penrose
final int N = 7;
final int R = 300;
final float scale = 0.2;
final PVector[] basicVertecies = new PVector[N];
final PVector[] innerVertecies = new PVector[N];
final PVector[] outerVertecies = new PVector[2 * N];
final PVector[] assistVertecies = new PVector[N];
color[] colors;
void setupColors() {
colors = new color[8];
colors[0] = #ff7f7f;
colors[1] = #ff7fff;
colors[2] = #7f7fff;
colors[3] = #7fffff;
colors[4] = #7fff7f;
colors[5] = #bfff7f;
colors[6] = #ffff7f;
colors[7] = #ffbf7f;
}
void setupVertecies() {
for(int i = 0; i < N; i++) {
float theta = 2.0 * i / N * PI;
basicVertecies[i] = new PVector(-R * cos(theta), R * sin(theta));
}
for(int i = 0; i < N; i++) {
int prev = (N + i - 1) % N;
int next = (i + 1) % N;
outerVertecies[2 * i] = new PVector();
outerVertecies[2 * i].set(basicVertecies[i]);
outerVertecies[2 * i].lerp(basicVertecies[prev], scale);
outerVertecies[2 * i + 1] = new PVector();
outerVertecies[2 * i + 1].set(basicVertecies[i]);
outerVertecies[2 * i + 1].lerp(basicVertecies[next], scale);
}
for(int i = 0; i < N; i++) {
innerVertecies[i] = new PVector();
innerVertecies[i].set(basicVertecies[i]);
PVector tmp = new PVector();
tmp.set(outerVertecies[2 * i]);
tmp.lerp(outerVertecies[2 * i + 1], 0.5);
innerVertecies[i].lerp(tmp, 4);
}
for(int i = 0; i < N; i++) {
assistVertecies[i] = new PVector();
assistVertecies[i].set(innerVertecies[i]);
assistVertecies[i].sub(outerVertecies[2 * i + 1]);
assistVertecies[i].add(basicVertecies[i]);
}
}
void setup() {
size(640, 640, P2D);
setupVertecies();
setupColors();
}
void draw() {
translate(320, 320);
rotate(PI/2);
background(255, 255, 255);
// drawSub();
drawMain();
}
void drawMain() {
for(int i = 0; i < N; i++) {
int next = (i + 1) % N;
int next2 = (i + 2) % N;
color c = colors[i % colors.length];
PShape s = createShape();
s.beginShape();
s.fill(c);
s.stroke(255, 255, 255);
s.vertex(outerVertecies[2 * i].x, outerVertecies[2 * i].y);
s.vertex(outerVertecies[2 * i + 1].x, outerVertecies[2 * i + 1].y);
s.vertex(outerVertecies[2 * next].x, outerVertecies[2 * next].y);
s.vertex(assistVertecies[next2].x, assistVertecies[next2].y);
s.vertex(innerVertecies[next2].x, innerVertecies[next2].y);
s.vertex(assistVertecies[next].x, assistVertecies[next].y);
s.endShape(CLOSE);
shape(s, 0, 0);
}
}
void drawSub() {
for(int i = 0; i < N; i++) {
stroke(255, 0, 0);
line(basicVertecies[i].x,
basicVertecies[i].y,
basicVertecies[(i + 1) % N].x,
basicVertecies[(i + 1) % N].y);
stroke(0, 255, 0);
line(outerVertecies[2 * i].x,
outerVertecies[2 * i].y,
outerVertecies[(2 * i + 1) % (2 * N)].x,
outerVertecies[(2 * i + 1) % (2 * N)].y);
stroke(0, 0, 255);
line(innerVertecies[i].x,
innerVertecies[i].y,
innerVertecies[(i + 1) % N].x,
innerVertecies[(i + 1) % N].y);
stroke(0, 255, 255);
line(assistVertecies[i].x,
assistVertecies[i].y,
assistVertecies[(i + 1) % N].x,
assistVertecies[(i + 1) % N].y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment