Skip to content

Instantly share code, notes, and snippets.

@JoshuaSullivan
Created March 22, 2018 02:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaSullivan/2b35e43466be11088c0e44f242cff5e2 to your computer and use it in GitHub Desktop.
Save JoshuaSullivan/2b35e43466be11088c0e44f242cff5e2 to your computer and use it in GitHub Desktop.
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();
}
@baybulatov
Copy link

Thank you! It works!

@baybulatov
Copy link

Maybe you know how to place moving text on the stripe surface?

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