float x, y, xn, yn;
ArrayList<Float> xpts;
ArrayList<Float> ypts;
ArrayList<Float> xptsReflected;
ArrayList<Float> yptsReflected;
int nsides;
void setup()
{
size(900, 900);
nsides = 7;
}
void draw()
{
background(255);
xpts = new ArrayList<Float>();
ypts = new ArrayList<Float>();
xptsReflected = new ArrayList<Float>();
yptsReflected = new ArrayList<Float>();
for (float t = 0; t < TWO_PI - 0.01; t += TWO_PI/nsides){
float xt = map(cos(t),-1,1,mouseX-100,mouseX+100);
float yt = map(sin(t),-1,1,mouseY-100,mouseY+100);
xpts.add(xt);
ypts.add(yt);
}
noFill();
stroke(120);
ellipse(width/2, height/2, width/2, height/2);
for (int i = 0; i < xpts.size(); i++)
{
x = map(xpts.get(i), 0, width, -2, 2);
y = map(ypts.get(i), height, 0, -2, 2);
float d = sqrt((x*x + y*y));
float theta = atan2(y, x);
xn = (1/d)*cos(theta);
yn = (1/d)*sin(theta);
float xpoint = map(x, -2, 2, 0, width);
float ypoint = map(y, -2, 2, height, 0);
//point(xpoint, ypoint);
xpoint = map(xn, -2, 2, 0, width);
ypoint = map(yn, -2, 2, height, 0);
//point(xpoint, ypoint);
xptsReflected.add(xpoint);
yptsReflected.add(ypoint);
}
beginShape();
fill(0,40);
stroke(0);
for (int i = 0; i < xpts.size(); i++)
{
float xt = xpts.get(i);
float yt = ypts.get(i);
vertex(xt,yt);
}
endShape(CLOSE);
beginShape();
fill(0,40);
stroke(0);
for (int i = 0; i < xpts.size(); i++)
{
float xt = xptsReflected.get(i);
float yt = yptsReflected.get(i);
vertex(xt,yt);
}
endShape(CLOSE);
}
void keyPressed() {
if (keyCode == UP){
nsides += 1;
}
else if (keyCode == DOWN){
if (nsides > 3){
nsides -= 1;
}
}
}