Hello everyone! This week I made something very cool. They are spriographs. They were programmed using parametric equations. Before I begin, I would like to explain what I was doing starting code wise. I made a JFrame, and JPanel first. I will provide code only for the material in the paint method!

Here are some spirographs:

spiro3

Here is the code for this spirograph

int r = 2;
int R = 100;
int p = 100;

Graphics2D g2d = (Graphics2D) g;
GeneralPath path = new GeneralPath();
path.reset();
path.moveTo(getWidth()/2 + 180, getHeight()/2);
for(double i = 0; i < 2*Math.PI; i += Math.PI/680) {
double ex = ((R-r)*Math.cos(i) + p*Math.cos((R-r)*i/r)) + getWidth()/2;
double why = ((R-r)*Math.sin(i) + p*Math.sin((R-r)*i/r)) + getHeight()/2;
path.lineTo(ex, why);

}

g.setColor(Color.black);
g2d.setColor(Color.black);
g2d.draw(path);

Spirograph:

sprio4

Code:

int j = 3;
int k = 3;

int a = 80;
int b = 1;
int c = 1;
int d = 80;
Polygon p = new Polygon();
Graphics2D g2d = (Graphics2D) g;
GeneralPath path = new GeneralPath();
path.reset();
path.moveTo(getWidth()/2 + 100, getHeight()/2);
for(double i = 0; i < 2 * Math.PI; i += Math.PI/3050) {

double ex = ((j * Math.cos(a * i) – Math.pow(Math.cos(b * i), j)) * 50) + getWidth()/2;
double why = ((Math.sin(c * i) – Math.pow(Math.sin(d * i), k)) * 100) + getHeight()/2;
path.lineTo(ex, why);
}

g.setColor(Color.black);
g2d.setColor(Color.black);
g2d.draw(path);

Spirograph:

spiro5

Code:

int j = 3;
int k = 3;

int a = 80;
int b = 1;
int c = 80;
int d = 1;
Polygon p = new Polygon();
Graphics2D g2d = (Graphics2D) g;
GeneralPath path = new GeneralPath();
path.reset();
path.moveTo(getWidth()/2 + 100, getHeight()/2);
for(double i = 0; i < 2 * Math.PI; i += Math.PI/3020) {

double ex = ((j * Math.cos(a * i) – Math.pow(Math.cos(b * i), j)) * 50) + getWidth()/2;
double why = ((Math.sin(c * i) – Math.pow(Math.sin(d * i), k)) * 100) + getHeight()/2;
path.lineTo(ex, why);

}

g.setColor(Color.black);

g2d.setColor(Color.black);
g2d.draw(path);

Spirograph:

spiro6

Code:

int j = 1;
int k = 2;

int a = 1;
int b = 203;
int c = 200;
int d = 1;

Polygon p = new Polygon();
Graphics2D g2d = (Graphics2D) g;
GeneralPath path = new GeneralPath();
path.reset();
path.moveTo(getWidth()/2 + 20, getHeight()/2);
for(double i = 0; i < 2 * Math.PI; i += Math.PI/3020) {
double ex = ((j * Math.cos(a * i) – Math.pow(Math.cos(b * i), j)) * 90) + getWidth()/2;
double why = ((Math.sin(c * i) – Math.pow(Math.sin(d * i), k)) * 100) + getHeight()/2;

path.lineTo(ex, why);
}

g.setColor(Color.black);

g2d.setColor(Color.black);
g2d.draw(path);

Advertisements