import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; public class test1 extends Applet { MotionCanvas M; Animator A; ControlCanvas C; Manager Z; Panel P1; public void init() { Z=new Manager(); setBackground(Color.black); A=new Animator(); A.Z=Z; M=new MotionCanvas(Z); C=new ControlCanvas(Z); Z.A=A; Z.M=M; Z.C=C; P1=new Panel(); M.setBackground(Color.blue); C.setBackground(Color.black); C.resize(150,150); M.resize(150,150); P1.add(M); P1.add(C); add(P1); try {A.start();} catch(Exception e) {} } } class DBCanvas extends Canvas { public void update(Graphics g) { Graphics g2; Image offscreen = null; offscreen = createImage(size().width, size().height); g2 = offscreen.getGraphics(); paint(g2); g.drawImage(offscreen, 0, 0, this); g2.dispose(); offscreen.flush(); } } class Manager { Animator A; ControlCanvas C; MotionCanvas M; Manager() {} } class MotionCanvas extends DBCanvas implements MouseListener, MouseMotionListener { double h,w; PolyVector P,PP; Vector v; double time; int number,diag; Color C1,C2; int sleep; int state; Manager Z; MotionCanvas (Manager Z) { w=50; h=24; number=24; this.Z=Z; v=new Vector(0,75,1); P=new PolyVector(number); P=P.entail(v,number,w,h); sleep=50; C1=Color.red; C2=Color.white; addMouseListener(this); addMouseMotionListener(this); state=1; diag=1; } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.black); g.fillOval(0,0,149,149); g.drawOval(0,0,149,149); g.fillOval(0,0,149,149); relax(); P.render(g,C1,C2); g.setColor(Color.blue); g.fillOval((int)(75-w),(int)(75-h),(int)(2*w),(int)(2*h)); g.drawOval((int)(75-w),(int)(75-h),(int)(2*w),(int)(2*h)); g.setColor(Color.white); g.drawRect(0,0,149,149); } void evolve() { double factor=1; if((w-h)*(w-h)>1500) factor=.3; double x1=-v.y; double y1=v.x; double x2=v.x+.0002*factor*sleep*x1; double y2=v.y+.0002*factor*sleep*y1; double d=Math.sqrt(x2*x2+y2*y2); v.x=75*x2/d; v.y=75*y2/d; P=P.entail(v,number,w,h); v=P.v[2]; } void relax() { double d=P.closingDistance(); if(d>10) d=10; if(P.crossTest()==1) { w=w*(1+d/15000); h=h*(1+d/15000); } if(P.crossTest()==0) { w=w*(1-d/15000); h=h*(1-d/15000); } } public void mouseExited(MouseEvent e) { Z.C.S[0]="Rich: Applet 37"; Z.C.S[1]=""; Z.C.S[2]="Poncelet's porism"; Z.C.repaint(); } public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseEntered(MouseEvent e) { Z.C.S[0]="This window shows"; Z.C.S[1]="the Poncelet Polygon"; Z.C.S[2]=""; Z.C.repaint(); } public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} } class ControlCanvas extends DBCanvas implements MouseMotionListener,MouseListener { Manager Z; int history; double speed; ViewInteger v1,v2; ListenSquare[] L=new ListenSquare[20]; String[] S=new String[3]; ControlCanvas(Manager Z) { this.Z=Z; history=-1; v1=new ViewInteger(23); v2=new ViewInteger(1); addMouseListener(this); addMouseMotionListener(this); L[1]=new ListenSquare(0,0,75,75); L[2]=new ListenSquare(75,30,75,15); L[3]=new ListenSquare(75,45,15,15); L[4]=new ListenSquare(135,45,15,15); L[16]=new ListenSquare(75,60,15,15); L[17]=new ListenSquare(135,60,15,15); L[15]=new ListenSquare(90,45,45,30); L[7]=new ListenSquare(75,0,15,15); L[5]=new ListenSquare(90,0,15,15); L[8]=new ListenSquare(105,0,15,15); L[9]=new ListenSquare(120,0,15,15); L[6]=new ListenSquare(135,0,15,15); L[12]=new ListenSquare(75,15,15,15); L[10]=new ListenSquare(90,15,15,15); L[13]=new ListenSquare(105,15,15,15); L[14]=new ListenSquare(120,15,15,15); L[11]=new ListenSquare(135,15,15,15); S[0]="Rich: Applet 37"; S[1]=""; S[2]="Poncelet's porism"; } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); L[1].render(g,Color.black,Color.white); L[3].render(g,Color.blue,Color.white); L[4].render(g,Color.blue,Color.white); L[16].render(g,Color.blue,Color.white); L[17].render(g,Color.blue,Color.white); L[5].render(g,Color.yellow,Color.white); L[6].render(g,Color.red,Color.white); L[7].render(g,Color.blue,Color.white); L[9].render(g,Color.white,Color.white); L[8].render(g,Color.black,Color.white); L[10].render(g,Color.yellow,Color.black); L[12].render(g,Color.blue,Color.white); L[14].render(g,Color.white,Color.black); L[13].render(g,Color.black,Color.white); L[11].render(g,Color.red,Color.white); L[15].render(g,Color.black,Color.white); L[11].render(g,Color.red,Color.white); L[2].render(g,Color.blue,Color.white); g.setColor(Color.blue); g.fillRect(6,6,63,63); g.setColor(Color.black); g.translate(2,2); g.fillOval(10,10,5,5); g.fillOval(22,22,10,10); g.fillOval(40,40,20,20); g.fillOval(22,10,10,5); g.fillOval(40,10,20,5); g.fillOval(40,10,20,5); g.fillOval(10,40,5,20); g.fillOval(40,22,20,10); g.fillOval(10,22,5,10); g.fillOval(22,40,10,20); g.drawOval(10,10,5,5); g.drawOval(22,22,10,10); g.drawOval(40,40,20,20); g.drawOval(22,10,10,5); g.drawOval(40,10,20,5); g.drawOval(40,10,20,5); g.drawOval(10,40,5,20); g.drawOval(40,22,20,10); g.drawOval(10,22,5,10); g.drawOval(22,40,10,20); g.translate(-2,-2); double q=Z.M.w; if(q80)&&(test==0)) { Z.C.S[0]="Rich: Applet 37"; Z.C.S[1]=""; Z.C.S[2]="Poncelet's porism"; repaint(0,75,150,75); } if(test==1) { S[0]="click here to change"; S[1]="the shape/size of the"; S[2]="blue ellipse"; repaint(0,75,150,75); } if(test==2) { S[0]="click here to change"; S[1]="the velocity of the"; S[2]=" polygon"; repaint(0,75,150,75); } if(test==3) { S[0]="click here to decrease"; S[1]="the # of sides of the"; S[2]="polygon by 1."; repaint(0,75,150,75); } if(test==4) { S[0]="click here to increase"; S[1]="the # of sides of the"; S[2]="polygon by 1."; repaint(0,75,150,75); } if(test==16) { S[0]="click here to decrease"; S[1]="the # of sides of the"; S[2]="polygon by 10."; repaint(0,75,150,75); } if(test==17) { S[0]="click here to increase"; S[1]="the # of sides of the"; S[2]="polygon by 10."; repaint(0,75,150,75); } if(test==5) { S[0]="click here to color"; S[1]="the polygon"; S[2]="yellow"; repaint(0,75,150,75); } if(test==6) { S[0]="click here to color"; S[1]="the polygon"; S[2]="red"; repaint(0,75,150,75); } if(test==7) { S[0]="click here to color"; S[1]="the polygon"; S[2]="blue"; repaint(0,75,150,75); } if(test==8) { S[0]="click here to color"; S[1]="the polygon"; S[2]="black"; repaint(0,75,150,75); } if(test==9) { S[0]="click here to color"; S[1]="the polygon"; S[2]="white"; repaint(0,75,150,75); } if(test==10) { S[0]="click here to color"; S[1]="the edges of"; S[2]="the polygon yellow"; repaint(0,75,150,75); } if(test==11) { S[0]="click here to color"; S[1]="the edges of"; S[2]="the polygon red"; repaint(0,75,150,75); } if(test==12) { S[0]="click here to color"; S[1]="the edges of"; S[2]="the polygon blue"; repaint(0,75,150,75); } if(test==13) { S[0]="click here to color"; S[1]="the edges of"; S[2]="the polygon black"; repaint(0,75,150,75); } if(test==14) { S[0]="click here to color"; S[1]="the edges of"; S[2]="the polygon white"; repaint(0,75,150,75); } if(test==15) { S[0]="this number records"; S[1]="number of sides of"; S[2]="the polygon."; repaint(0,75,150,75); } } } public void mousePressed(MouseEvent e) { e.consume(); Point p= new Point(); p.x=e.getX(); p.y=e.getY(); int test=0; for(int i=1;i<=17;++i) { if(L[i].inside(p)==1) test=i; } if(test==1) { double size=p.x*p.x+p.y*p.y; size=Math.sqrt(size); if((p.x<72)&&(p.y<72)&&(p.x>3)&&(p.y>3)) {Z.M.w=p.x;Z.M.h=p.y;repaint();} } if(test==2) Z.M.sleep=p.x-75; if((Z.M.number>4)&&(test==3)) --Z.M.number; if((Z.M.number<39)&&(test==4)) ++Z.M.number; if((Z.M.number<30)&&(test==17)) Z.M.number=Z.M.number+10; if((Z.M.number>13)&&(test==16)) Z.M.number=Z.M.number-10; if(test==5) Z.M.C1=Color.yellow; if(test==6) Z.M.C1=Color.red; if(test==7) Z.M.C1=Color.blue; if(test==8) Z.M.C1=Color.black; if(test==9) Z.M.C1=Color.white; if(test==10) Z.M.C2=Color.yellow; if(test==11) Z.M.C2=Color.red; if(test==12) Z.M.C2=Color.blue; if(test==13) Z.M.C2=Color.black; if(test==14) Z.M.C2=Color.white; if((test==3)||(test==4)||(test==16)||(test==17)||(test==2)) repaint(); } } class Vector { double x,y,z; Vector() { this.x=0; this.y=0; this.z=0; } Vector(double x,double y,double z) { this.x=x; this.y=y; this.z=z; } Vector cross(Vector V,Vector W) { Vector X=new Vector(); X.x=V.y*W.z-V.z*W.y; X.y=V.z*W.x-V.x*W.z; X.z=V.x*W.y-V.y*W.x; return(X); } Vector normalize() { Vector X=this; X.x=x/z; X.y=y/z; X.z=1; return(X); } Vector next(double w,double h) { Vector W=new Vector(); double x1=this.x/this.z; double y1=this.y/this.z; double c=(x1/w)*(x1/w)+(y1/h)*(y1/h); double t=Math.sqrt(c-1); double x2=(w*t*y1/h+x1)/c; double y2=(y1-h*t*x1/w)/c; double vx=y2-y1; double vy=x1-x2; double d=Math.sqrt(vx*vx+vy*vy); double ux=vx/d; double uy=vy/d; d=x1*ux+y1*uy; W.x=-x1+2*d*ux; W.y=-y1+2*d*uy; W.z=1; return(W); } void render(Graphics g,Color C) { int xx=(int)(this.x/this.z); int yy=(int)(this.y/this.z); g.setColor(C); g.fillOval(75+xx-5,75+yy-5,10,10); } void render(Vector v,Graphics g,Color C) { int x1=(int)(this.x/this.z); int y1=(int)(this.y/this.z); int x2=(int)(v.x/v.z); int y2=(int)(v.y/v.z); g.setColor(C); g.drawLine(75+x1,75+y1,75+x2,75+y2); } } class ListenSquare { double x1,x2; double y1,y2; ListenSquare(double x1,double y1,double x2,double y2) { this.x1=x1; this.x2=x2; this.y1=y1; this.y2=y2; } int inside(Point p) { int val; val=1; if(p.x<=this.x1) val=0; if(p.x>this.x1+this.x2) val=0; if(p.y<=this.y1) val=0; if(p.y>this.y1+this.y2) val=0; return(val); } void render(Graphics g,Color C1,Color C2) { int xx1,yy1,xx2,yy2; xx1=(int)(this.x1); yy1=(int)(this.y1); xx2=(int)(int)(this.x2); yy2=(int)(int)(this.y2); g.setColor(C1); g.fillRect(xx1,yy1,xx2,yy2); g.setColor(C2); g.drawRect(xx1,yy1,xx2,yy2); } } class PolyVector { int n; int SIZE; Vector[] v=new Vector[50]; PolyVector(int S) { this.n=40; this.SIZE=S; for(int i=1;i<=40;++i) v[i]=new Vector(); } PolyVector regular(int n) { PolyVector P=new PolyVector(SIZE); P.n=n; for(int i=1;i<=P.n;++i) { P.v[i].x=75*Math.cos(2*Math.PI*i/n); P.v[i].y=75*Math.sin(2*Math.PI*i/n); P.v[i].z=1; } P.v[P.n+1]=P.v[1]; P.v[n+2]=P.v[2]; return(P); } void tclOutput() { System.out.println(""); for(int i=1;i<=n-2;++i) { System.out.println(v[i].x); System.out.println(v[i].y); } int j=n-1; System.out.println(v[j].x); System.out.println(v[j].y); System.out.println(-9999); } void mathOutput() { System.out.println("P={"); for(int i=1;i<=n-2;++i) { System.out.println("{"+v[i].x+","+v[i].y+"},"); } int j=n-1; System.out.println("{"+v[j].x+","+v[j].y+"}}"); } PolyVector entail(Vector v,int n,double w,double h) { PolyVector P=new PolyVector(n); P.v[1]=v; for(int i=1;i<=n-1;++i) { P.v[i+1]=P.v[i].next(w,h); } P.v[n+1]=P.v[1]; P.v[n+2]=P.v[2]; P.n=n; return(P); } void render(Graphics g,Color C1,Color C2) { int[] X=new int[42]; int[] Y=new int[42]; for(int i=0;i<=n-1;++i) { X[i]=(int)(75+this.v[i+1].x/this.v[i+1].z); Y[i]=(int)(75+this.v[i+1].y/this.v[i+1].z); } g.setColor(C1); g.fillPolygon(X,Y,n); g.setColor(C2); g.drawPolygon(X,Y,n); } void photoRender(Graphics g,Color C1,Color C2,int s) { int[] X=new int[52]; int[] Y=new int[52]; for(int i=0;i<=n-1;++i) { X[i]=(int)(s*75+s*this.v[i+1].x/this.v[i+1].z); Y[i]=(int)(s*75+s*this.v[i+1].y/this.v[i+1].z); } g.setColor(C1); g.fillPolygon(X,Y,n); g.setColor(C2); g.drawPolygon(X,Y,n); } int crossTest() { Vector v1=v[1]; Vector v2=v[2]; Vector v3=v[n-1]; Vector v4=v[n]; Vector w=v1.cross(v1.cross(v1,v2),v1.cross(v3,v4)); w=w.normalize(); int test=0; if((w.x>v1.x)&&(w.xv2.x)) test=1; return(test); } double closingDistance() { Vector v1=v[1]; Vector v2=v[n]; double d=(v1.x-v2.x)*(v1.x-v2.x); d=d+(v1.y-v2.y)*(v1.y-v2.y); d=Math.sqrt(d); return(d); } } class ViewInteger { int x; ViewInteger(int x) { this.x=x; } void render(int u, int v, Color C,Graphics g) { int y=x; int place=0; while(y>0) { int z=y-10*(y/10); g.setColor(C); char ch=(char)(z+48); Character CH1=new Character(ch); g.setFont(new Font("Helvetica",Font.PLAIN,20)); g.drawString(CH1.toString(),u-place,v); place=place+12; y=(y-z)/10; } } } class Animator extends Thread { Manager Z; public void run() { while(true) { Z.M.evolve(); Z.M.repaint(); try {sleep(20);} catch(Exception e) {} } } }