import java.applet.*; import java.awt.*; import java.awt.event.*; public class pentagram extends Applet implements Runnable{ Thread th = null; Image os; Graphics og; double lx[][] = new double[7][2] , ly[][] = new double[7][2] ; double crx[] = new double[21] , cry[] = new double[21] ; double pi = 3.141592; double cx,cy; int pm,pn,ps,co,cf; double st,dt; int tx[] = new int[4],ty[] = new int[4]; public void init(){ os = createImage(360,360); og = os.getGraphics(); setBackground(Color.white); pm = 0; for(int k1 = 0;k1 < 5;k1 ++){ lx[k1][0] = Math.cos(2*pi*k1/5 - pi/2); ly[k1][0] = Math.sin(2*pi*k1/5 - pi/2); lx[k1][1] = Math.cos(2*pi*k1/5 + 3*pi/10); ly[k1][1] = Math.sin(2*pi*k1/5 + 3*pi/10); } lx[5][0] = Math.cos(pi/3) ; ly[5][0] = Math.sin(pi/3) ; lx[5][1] = Math.cos(2*pi/3) ; ly[5][1] = Math.sin(2*pi/3) ; lx[6][0] = Math.cos(-1*pi/3) ; ly[6][0] = Math.sin(-1*pi/3) ; lx[6][1] = Math.cos(-2*pi/3) ; ly[6][1] = Math.sin(-2*pi/3) ; addMouseListener( new MouseAdapter(){ public void mousePressed(MouseEvent e){ cx = (e.getX()-180.0)/150.0 ; cy = (e.getY()-180.0)/150.0 ; if(pm == 0){ if((Math.abs(cx-lx[5][0])<0.05)&&(Math.abs(cy-ly[5][0])<0.05)){ pn = 5 ; ps = 0 ; pm = 1 ; }else if((Math.abs(cx-lx[5][1])<0.05)&&(Math.abs(cy-ly[5][1])<0.05)){ pn = 5 ; ps = 1 ; pm = 1 ; }else if((Math.abs(cx-lx[6][0])<0.05)&&(Math.abs(cy-ly[6][0])<0.05)){ pn = 6 ; ps = 0 ; pm = 1 ; }else if((Math.abs(cx-lx[6][1])<0.05)&&(Math.abs(cy-ly[6][1])<0.05)){ pn = 6 ; ps = 1 ; pm = 1 ; } } repaint(); } public void mouseReleased(MouseEvent e){ pm = 0; repaint(); } } ); addMouseMotionListener( new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ st = pi/2 - Math.atan2((e.getX()-180.0)/150.0,(e.getY()-180.0)/150.0); if(pm == 1){ lx[pn][ps] = Math.cos(st); ly[pn][ps] = Math.sin(st); } for(int n1 = 0;n1 < 7;n1 ++){ for(int n2 = 0;n2 < 2;n2 ++){ if((Math.abs(lx[pn][ps]-lx[n1][n2])<0.05)&&(Math.abs(ly[pn][ps]-ly[n1][n2])<0.05)){ lx[pn][ps] = lx[n1][n2] ; ly[pn][ps] = ly[n1][n2] ; } } } repaint(); } } ); } public void paint(Graphics g){ og.setColor(Color.white); og.fillRect(0,0,360,360); og.setColor(Color.lightGray); og.drawOval(30,30,300,300); for(int d1 = 0;d1 < 7;d1 ++){ og.setColor(Color.black); og.drawLine((int)(150*lx[d1][0]+180),(int)(150*ly[d1][0]+180),(int)(150*lx[d1][1]+180),(int)(150*ly[d1][1]+180)); if(d1 > 4){ og.setColor(Color.blue); og.drawOval((int)(150*lx[d1][0]+177),(int)(150*ly[d1][0]+177),6,6); og.drawOval((int)(150*lx[d1][1]+177),(int)(150*ly[d1][1]+177),6,6); } } co = 0; for(int ln1 = 0;ln1 < 6;ln1 ++){ for(int ln2 = ln1 + 1;ln2 < 7;ln2 ++){ dt = (ly[ln1][1]-ly[ln1][0])*(lx[ln2][0]-lx[ln2][1])-(lx[ln1][0]-lx[ln1][1])*(ly[ln2][1]-ly[ln2][0]) ; if(dt == 0){ crx[co] = 200 ; cry[co] = 200 ; co ++; }else{ crx[co] = ((lx[ln2][0]-lx[ln2][1])*(lx[ln1][0]*ly[ln1][1]-lx[ln1][1]*ly[ln1][0])+(lx[ln1][1]-lx[ln1][0])*(lx[ln2][0]*ly[ln2][1]-lx[ln2][1]*ly[ln2][0]))/dt; cry[co] = ((ly[ln2][0]-ly[ln2][1])*(lx[ln1][0]*ly[ln1][1]-lx[ln1][1]*ly[ln1][0])+(ly[ln1][1]-ly[ln1][0])*(lx[ln2][0]*ly[ln2][1]-lx[ln2][1]*ly[ln2][0]))/dt; for(int n1 = 0;n1 < co;n1 ++){ if((Math.abs(crx[n1]-crx[co])<0.01)&&(Math.abs(cry[n1]-cry[co])<0.01)){ crx[co] = 200 ; cry[co] = 200 ; } } if(crx[co]*crx[co]+cry[co]*cry[co] > 1.01){ crx[co] = 200 ; cry[co] = 200 ; }else{ og.setColor(Color.red); og.fillOval((int)(150*crx[co]+178),(int)(150*cry[co]+178),5,5); } co ++ ; } } } co = 0; for(int p1 = 0;p1 < 19;p1 ++){ for(int p2 = p1+1;p2 < 20;p2 ++){ for(int p3 = p2+1;p3 < 21;p3 ++){ if((crx[p1]*crx[p1]+cry[p1]*cry[p1]<=1.01)&&(crx[p2]*crx[p2]+cry[p2]*cry[p2]<=1.01)&&(crx[p3]*crx[p3]+cry[p3]*cry[p3]<=1.01)){ for(int l1 = 0;l1 < 7;l1 ++){ if(Math.abs((lx[l1][1]-lx[l1][0])*(cry[p1]-ly[l1][0])-(ly[l1][1]-ly[l1][0])*(crx[p1]-lx[l1][0]))<0.005){ if(Math.abs((lx[l1][1]-lx[l1][0])*(cry[p2]-ly[l1][0])-(ly[l1][1]-ly[l1][0])*(crx[p2]-lx[l1][0]))<0.005){ cf = 1; for(int cn = 0;cn < 21;cn ++){ if((cn != p1)&&(cn != p2)){ if((crx[cn]*crx[cn]+cry[cn]*cry[cn])<=1.01){ if(Math.abs((lx[l1][1]-lx[l1][0])*(cry[cn]-ly[l1][0])-(ly[l1][1]-ly[l1][0])*(crx[cn]-lx[l1][0]))<0.005){ if(((crx[p1]-crx[cn])*(crx[p2]-crx[cn])<0)||((cry[p1]-cry[cn])*(cry[p2]-cry[cn])<0))cf = 0; } } } } if(cf == 1){ for(int l2 = 0;l2 < 7;l2 ++){ if(l1 != l2){ if(Math.abs((lx[l2][1]-lx[l2][0])*(cry[p2]-ly[l2][0])-(ly[l2][1]-ly[l2][0])*(crx[p2]-lx[l2][0]))<0.005){ if(Math.abs((lx[l2][1]-lx[l2][0])*(cry[p3]-ly[l2][0])-(ly[l2][1]-ly[l2][0])*(crx[p3]-lx[l2][0]))<0.005){ cf = 1; for(int cn = 0;cn < 21;cn ++){ if((cn != p2)&&(cn != p3)){ if((crx[cn]*crx[cn]+cry[cn]*cry[cn])<=1.01){ if(Math.abs((lx[l2][1]-lx[l2][0])*(cry[cn]-ly[l2][0])-(ly[l2][1]-ly[l2][0])*(crx[cn]-lx[l2][0]))<0.005){ if(((crx[p2]-crx[cn])*(crx[p3]-crx[cn])<0)||((cry[p2]-cry[cn])*(cry[p3]-cry[cn])<0))cf = 0; } } } } if(cf == 1){ for(int l3 = 0;l3 < 7;l3 ++){ if((l2 != l3)&&(l1 != l3)){ if(Math.abs((lx[l3][1]-lx[l3][0])*(cry[p3]-ly[l3][0])-(ly[l3][1]-ly[l3][0])*(crx[p3]-lx[l3][0]))<0.005){ if(Math.abs((lx[l3][1]-lx[l3][0])*(cry[p1]-ly[l3][0])-(ly[l3][1]-ly[l3][0])*(crx[p1]-lx[l3][0]))<0.005){ cf = 1; for(int cn = 0;cn < 21;cn ++){ if((cn != p3)&&(cn != p1)){ if((crx[cn]*crx[cn]+cry[cn]*cry[cn])<=1.01){ if(Math.abs((lx[l3][1]-lx[l3][0])*(cry[cn]-ly[l3][0])-(ly[l3][1]-ly[l3][0])*(crx[cn]-lx[l3][0]))<0.005){ if(((crx[p1]-crx[cn])*(crx[p3]-crx[cn])<0)||((cry[p1]-cry[cn])*(cry[p3]-cry[cn])<0))cf = 0; } } } } if(cf == 1){ tx[0] = (int)(150*crx[p1] + 180) ; ty[0] = (int)(150*cry[p1] + 180) ; tx[1] = (int)(150*crx[p2] + 180) ; ty[1] = (int)(150*cry[p2] + 180) ; tx[2] = (int)(150*crx[p3] + 180) ; ty[2] = (int)(150*cry[p3] + 180) ; tx[3] = (int)(150*crx[p1] + 180) ; ty[3] = (int)(150*cry[p1] + 180) ; og.setColor(new Color(100,100,255)); og.fillPolygon(tx,ty,4); og.setColor(Color.black); og.drawPolygon(tx,ty,4); co ++; } } } } } } } } } } } } } } } } } } og.setFont(new Font("",Font.BOLD,16)); og.drawString(""+co+" triangles",250,335); if(co == 10){ og.setFont(new Font("",Font.BOLD,60)); og.setColor(Color.black); og.drawString("clear!!",92,198); og.setColor(Color.red); og.drawString("clear!!",90,200); } g.drawImage(os,0,0,this); } public void start(){ if(th == null){ th = new Thread(this); th.start(); } } public void stop(){ th = null; } public void run(){ while(th != null){ try{ Thread.sleep(50); } catch (InterruptedException e){ } } } public void update( Graphics g ){ paint( g ) ; } }