Mouse click to randomize the four numbers and produce new curve.
A fork of Random Farris Curves by Robert S. Robbins
xxxxxxxxxx
// Mathematician Frank A. Farris of Santa Clara University found the formula for these mystery curves which have five-fold rotational symmetry.
// https://mathlesstraveled.com/2015/06/04/random-cyclic-curves-5/
// Create text object
PFont myFont = createFont("Arial Bold", 20, true);
textFont(myFont);
let hu = 0; // hue
void setup() {
size(1112, 834);
colorMode(HSB, 255);
rectMode(CENTER);
smooth();
noLoop();
}
void draw() {
background(255);
// Get random numbers
int a = (int)random(0,5);
int b = (int)random(1,10);
int c = (int)random(-20,-30);
int d = (int)random(0,20);
/*fill(0);
text("a: " + a, 10, 20);
text("b: " + b, 10, 40);
text("c: " + c, 10, 60);
text("d: " + d, 10, 80);*/
// Invert the y axis
scale(1, -1);
translate(0, -height);
// This centers what is drawn
translate(width / 2, height / 2);
strokeWeight(2);
//stroke(0,0,0);
fill(255);
// Add the points
int[] t = linspace(0, 2 * Math.PI, 1000);
for(int i=1; i < 1000; i++) {
complex ft = Swirly(t[i], a, b, c, d);
stroke(i/1000*255, 255, 200);
ellipse(ft.x * 200, ft.y * 200, 50, 50);
}
}
// Returns evenly spaced numbers over a specified interval.
// Based on numpy.linspace from Python
function linspace(float min, float max, int points) {
double[] d = new float[points];
for (int i = 0; i < points; i++){
d[i] = min + i * (max - min) / (points - 1);
}
return d;
}
// The function which does the complex number calculations
function Swirly(float t, float a, float b, float c, float d) {
complex j1 = new complex(0, 1);
complex objComplexTemp = j1.times(t).times(a);
complex r1 = objComplexTemp.exp();
objComplexTemp = j1.times(t).times(b);
complex r2 = objComplexTemp.exp().divides(2);
objComplexTemp = j1.times(t).times(c);
objComplexTemp = j1.mult(objComplexTemp.exp());
complex r3 = objComplexTemp.divides(3);
objComplexTemp = j1.times(t).times(d);
complex r4 = objComplexTemp.exp().divides(4);
complex objComplex = r1.minus(r2).add(r3).add(r4);
return objComplex;
}
// Redraw the design with every mouse click
let lapse = 0; // mouse timer
void mousePressed(){
// prevents mouse press from registering twice
if (millis() - lapse > 400){
redraw();
lapse = millis();
}
}
// Complex numbers for JavaScript with a few added operations
function complex(x,y) {
this.x = x;
this.y = y;
this.add = function(z){
return new complex(this.x + z.x,this.y + z.y);
}
this.minus = function(z){
return new complex(this.x - z.x,this.y - z.y);
}
this.mult = function(z){
return new complex(this.x * z.x - this.y * z.y ,this.x * z.y + this.y * z.x);
}
// Complex number times an integer
this.times = function(float z){
return new complex(this.x * z, this.y * z);
}
// Complex number divided an integer
this.divides = function(float z){
return new complex(this.x / z, this.y / z);
}
// Complex number exponentiation
this.exp = function(){
return new complex(Math.exp(this.x) * Math.cos(this.y), Math.exp(this.x) * Math.sin(this.y));
}
this.scale = function(s){
return new complex(this.x * s ,this.y * s);
}
this.sq = function() {
return this.mult(this);
}
this.sqrt = function() {
var r = sqrt(this.modulus());
var arg = this.arg()/2.0;
return new complex(r*cos(arg),r*sin(arg));
}
this.modulus = function() {
return sqrt(this.x * this.x + this.y * this.y);
}
this.arg = function() {
return atan2(this.y,this.x);
}
}