Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Processing script for rendering a Möbius Strip.
class MobiusRing {
float majorRadius, minorRadius;
int segments;
PVector[] vertices;
MobiusRing(float majorRadius, float minorRadius, int segments) {
this.majorRadius = majorRadius;
this.minorRadius = minorRadius;
this.segments = segments;
vertices = new PVector[segments * 2];
float da = TWO_PI / float(segments);
float dr = PI / float(segments);
for(int i = 0; i < segments; i++) {
float a = da * float(i);
float r0 = dr * float(i);
float r1 = r0 + PI;
PVector p = new PVector(cos(a) * majorRadius, 0.0, sin(a) * majorRadius);
//println("Starting center: " + p);
PVector q0 = p.copy().add(computeQ(p, minorRadius, r0));
PVector q1 = p.copy().add(computeQ(p, minorRadius, r1));
//println((i * 2) + ": " + q0.x + ", " + q0.y + ", " + q0.z);
//println((i * 2 + 1) + ": " + q1.x + ", " + q1.y + ", " + q1.z);
vertices[i * 2] = q0;
vertices[i * 2 + 1] = q1;
}
}
PVector computeQ(PVector center, float radius, float angle) {
PVector w = center.copy();
w.normalize();
PVector q0 = w.mult(radius * cos(angle));
PVector q1 = new PVector(0, radius * sin(angle), 0);
PVector qFinal = q0.add(q1);
return qFinal;
}
void draw() {
beginShape(TRIANGLE_STRIP);
int count = segments * 2;
for(int i = 0; i < count; i++) {
PVector v = vertices[i % count];
vertex(v.x, v.y, v.z);
}
vertex(vertices[1].x, vertices[1].y, vertices[1].z);
vertex(vertices[0].x, vertices[0].y, vertices[0].z);
endShape();
}
}
MobiusRing m;
float a = 0;
void setup(){
size(800, 800, P3D);
m = new MobiusRing(380, 120, 60);
}
void draw() {
a += PI / 240;
noStroke();
fill(204, 204, 255);
ambientLight(102, 102, 102);
directionalLight(204, 204, 204, 40, 700, -300);
specular(255, 255, 255);
shininess(8.0);
background(26);
pushMatrix();
translate(400, 400, -400);
pushMatrix();
rotateX(-PI / 6.0);
rotateY(a);
m.draw();
popMatrix();
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment