Skip to content

Instantly share code, notes, and snippets.

@Nekodigi
Created December 4, 2020 10:13
Show Gist options
  • Select an option

  • Save Nekodigi/cf6c08f814d9bf2dfd45de49fd3ce6f1 to your computer and use it in GitHub Desktop.

Select an option

Save Nekodigi/cf6c08f814d9bf2dfd45de49fd3ce6f1 to your computer and use it in GitHub Desktop.
//based on this site (jp)https://sw1227.hatenablog.com/entry/2018/12/03/235105
int n = 3;
int res = 50;
float a = 0;
float scale = 70;
void setup(){
colorMode(HSB, 360, 100, 100);
size(500, 500, P3D);
}
void draw(){
noStroke();
lights();
background(360);
strokeWeight(5);
translate(width/2, height/2);
rotateX(float(frameCount)/100);
rotateY(float(frameCount)/200);
rotateZ(float(frameCount)/300);
a = map(mouseX, 0, width, 0, 10);
n = (int)map(mouseY, 0, height, 2, 7);
//a += 0.01;
for(int k1=0; k1<n; k1++){
float phi1 = TWO_PI*k1/n;
for(int k2=0; k2<n; k2++){
float phi2 = TWO_PI*k2/n;
for(int i=0; i<res; i++){
float x = map(i, 0, res, 0, HALF_PI);
for(int j=0; j<res; j++){
float y = map(j, 0, res, -HALF_PI, HALF_PI);
fill(map(i, 0, res, 0, 360), map(j, 0, res, 0, 100), 100);
beginShape();
PVector pos = posAt(phi1, phi2, x, y);
vertex(pos.x*scale, pos.y*scale, pos.z*scale);
PVector pos1 = posAt(phi1, phi2, x+HALF_PI/res, y);
vertex(pos1.x*scale, pos1.y*scale, pos1.z*scale);
PVector pos2 = posAt(phi1, phi2, x+HALF_PI/res, y+PI/res);
vertex(pos2.x*scale, pos2.y*scale, pos2.z*scale);
PVector pos3 = posAt(phi1, phi2, x, y+PI/res);
vertex(pos3.x*scale, pos3.y*scale, pos3.z*scale);
endShape(CLOSE);
}
}
}
}
}
PVector posAt(float phi1, float phi2, float x, float y){
Complex z1 = exp(c(0, phi1)).mult(cos(c(x, y)).powt(2./n));
Complex z2 = exp(c(0, phi2)).mult(sin(c(x, y)).powt(2./n));
PVector pos = new PVector(z1.x, z2.x, z1.y*cos(a) + z2.y*sin(a));
return pos;
}
Complex sin(Complex z){//based on this site https://en.wikipedia.org/wiki/Sine#Relationship_to_complex_numbers
return new Complex(sin(z.x)*(float)Math.cosh(z.y), cos(z.x)*(float)Math.sinh(z.y));
}
Complex cos(Complex z){//https://en.wikibooks.org/wiki/Trigonometry/Functions_of_complex_variables#:~:text=The%20trigonometric%20functions%20can%20be,all%20real%20and%20complex%20numbers.&text=cos(i%20x)%20%3D%20cosh,)%20%3D%20i%20sinh(x)
return new Complex(cos(z.x)*(float)Math.cosh(z.y), -sin(z.x)*(float)Math.sinh(z.y));
}
Complex exp(Complex z){//based on this site https://mathworld.wolfram.com/ComplexExponentiation.html
return new Complex(cos(z.y), sin(z.y)).mult(exp(z.x));
}
Complex c(float x, float y){
return new Complex(x, y);
}
Complex r(float x){
return new Complex(x, 0);
}
class Complex{//Complexex number
float x, y;
Complex(float x, float y){
this.x = x;
this.y = y;
}
String toString(){
return "("+str(x)+","+str(y)+")";
}
float mag(){
return sqrt(x*x + y*y);
}
boolean equal(Complex z){
return x==z.x && y==z.y;
}
Complex powt(Complex z){//https://mathworld.wolfram.com/ComplexExponentiation.html
float arg = atan2(y, x);
float rmag = pow(x*x + y*y, z.x/2)*exp(-z.y*arg);
float rarg = z.x*arg + 1./2*z.y*log(x*x + y*y);
return new Complex(cos(rarg), sin(rarg)).mult(rmag);
}
Complex powt(float value){//return this^value
float angle = atan2(y, x);
float mag = sqrt(x*x + y*y);
float rangle = angle*value;
float rmag = pow(mag, value);
return new Complex(cos(rangle)*rmag, sin(rangle)*rmag);
}
//based on this site https://www.onlinemathlearning.com/multiplying-complex-numbers.html
Complex mult(Complex z){
return new Complex(x*z.x - y*z.y, y*z.x + x*z.y);
}
Complex mult(float z){
return new Complex(x*z, y*z);
}
//based on this site http://www.sosmath.com/complex/number/basic/soscv.html
Complex div(Complex z){
return new Complex((x*z.x + y*z.y)/(z.x*z.x + z.y*z.y), (y*z.x - x*z.y)/(z.x*z.x + z.y*z.y));
}
Complex add(Complex target){
return new Complex(x+target.x, y+target.y);
}
Complex add(float target){
return new Complex(x+target, y);
}
Complex sub(Complex target){
return new Complex(x-target.x, y-target.y);
}
Complex sub(float target){
return new Complex(x-target, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment