import java.applet.Applet; import java.awt.*; import java.util.*; public class coor extends Applet { int cj,jug; int wid=600,hei=500,bai=50; double px[] = new double[100]; double py[] = new double[100]; int cn = 0; String coo; TextField tx; List L; Button del; Graphics g; Vector vPointx = new Vector(); Vector vPointy = new Vector(); Vector vCoorx = new Vector(); Vector vCoory = new Vector(); static int nPoint = 0; public void init() { Panel p1 = new Panel(); Panel p2 = new Panel(); tx = new TextField("",50); L = new List(); jug = 0; del = new Button("全て削除"); setBackground(Color.white); setLayout(new BorderLayout()); p1.setLayout(new BorderLayout()); p1.add("Center",tx); p1.add("South",L); p1.add("East",del); p2.setBackground(Color.white); CheckboxGroup cg = new CheckboxGroup(); Checkbox c = new Checkbox("グラフなし",cg,true); c.setBackground(Color.white); p2.add(c); c = new Checkbox("直線で結ぶ",cg,false); c.setBackground(Color.white); p2.add(c); c = new Checkbox("曲線で結ぶ",cg,false); c.setBackground(Color.white); p2.add(c); c = new Checkbox("最小二乗法",cg,false); c.setBackground(Color.white); p2.add(c); c = new Checkbox("座標を通過する",cg,false); c.setBackground(Color.white); p2.add(c); p2.add(new Button("拡大")); p2.add(new Button("縮小")); add("South",p1); add("North",p2); g = getGraphics(); } public void sort(int n) { int i,min; Integer ix1,ix2,iy1,iy2; for (i = 0;i < n-1;i++) { min = i; for (int j = i+1;j < n;j++) { ix1 = (Integer)vPointx.elementAt(min); ix2 = (Integer)vPointx.elementAt(j); if (ix1.intValue() > ix2.intValue()) min = j; } if (min != i) { ix1 = (Integer)vPointx.elementAt(i); ix2 = (Integer)vPointx.elementAt(min); vPointx.setElementAt(ix2,i); vPointx.setElementAt(ix1,min); iy1 = (Integer)vPointy.elementAt(i); iy2 = (Integer)vPointy.elementAt(min); vPointy.setElementAt(iy2,i); vPointy.setElementAt(iy1,min); } } } public void paint(Graphics g) { int i,di,cnt; double x,y,dx,dy,ix,iy; String buf; cn = 0; g.translate(wid/2,hei/2); g.drawLine(-wid/2,0,wid/2,0); g.drawLine(0,hei/2,0,-hei/2); g.drawString("0",-10,15); g.drawString("x",wid/2-15,-10); g.drawString("y",10,-hei/2+40); for (i = -(int)wid/bai;i <= (int)wid/bai;i++) { if (i == 0) i++; di = i*bai; buf = String.valueOf(i); g.drawString(buf,di-10,15); g.drawString(buf,-15,-di+15); g.drawLine(di,-5,di,5); g.drawLine(-5,di,5,di); } cnt = vPointx.size(); for (i = 0;i < cnt;i++) { drawCoor(i); } if (jug == 1) { double idx=0,idy=0; Integer intx,inty; sort(cnt); ix = iy = 0; for (int f = 0;f < cnt;f++) { intx = (Integer)vPointx.elementAt(f); inty = (Integer)vPointy.elementAt(f); ix = intx.doubleValue(); iy = inty.doubleValue(); if (f != 0) g.drawLine((int)idx,(int)idy,(int)ix,(int)iy); idx = ix; idy = iy; } } else if (jug == 2) { int s,t,np,mm; double c1,c2,hl,hr,xmin,xmax,idx,tk; double a2[] = new double[100]; double a3[] = new double[100]; double xa[] = new double[100]; double xb[] = new double[100]; double xc[] = new double[100]; double xd[] = new double[100]; double xs[] = new double[100]; double xu[] = new double[100]; Integer intx1,intx2,inty1,inty2; mm = 100; dx = dy = ix = iy = 0; sort(cnt); if (cnt > 2) { np = cnt-2; for (s = 1;s <= np;s++) { intx1 = (Integer)vPointx.elementAt(s); intx2 = (Integer)vPointx.elementAt(s-1); hl = intx1.doubleValue() - intx2.doubleValue(); intx2 = (Integer)vPointx.elementAt(s+1); hr = intx2.doubleValue() - intx1.doubleValue(); xa[s] = hl/(hl+hr); xb[s] = 1-xa[s]; inty1 = (Integer)vPointy.elementAt(s+1); inty2 = (Integer)vPointy.elementAt(s); c1 = (inty1.doubleValue() - inty2.doubleValue())/hr; inty1 = (Integer)vPointy.elementAt(s-1); c2 = (inty2.doubleValue() - inty1.doubleValue())/hl; xc[s] = 6.0*((c1-c2)/(hl+hr)); xd[s] = 2.0; } for (s = 1;s < np;s++) { xb[s] = xb[s]/xd[s]; xc[s] = xc[s]/xd[s]; xd[s+1] = xd[s+1]-xa[s+1]*xb[s]; xc[s+1] = xc[s+1]-xa[s+1]*xc[s]; } xs[np] = xc[np]/xd[np]; for (s = np-1;s > 0;s--) xs[s] = xc[s]-xb[s]*xs[s+1]; intx1 = (Integer)vPointx.elementAt(1); intx2 = (Integer)vPointx.elementAt(0); hl = intx1.doubleValue() - intx2.doubleValue(); inty1 = (Integer)vPointy.elementAt(1); inty2 = (Integer)vPointy.elementAt(0); xu[0] = (inty1.doubleValue() - inty2.doubleValue())/hl - hl*xs[1]/6.0; for (s = 1;s <= np+1;s++) { intx1 = (Integer)vPointx.elementAt(s); intx2 = (Integer)vPointx.elementAt(s-1); hl = intx1.doubleValue() - intx2.doubleValue(); xu[s] = xu[s-1]+hl*(xs[s-1]+xs[s])/2.0; } for (s = 1;s <= np+1;s++) { intx1 = (Integer)vPointx.elementAt(s); intx2 = (Integer)vPointx.elementAt(s-1); hl = intx1.doubleValue() - intx2.doubleValue(); a3[s] = (xs[s]-xs[s-1])/(6.0*hl); a2[s] = xs[s-1]/2.0; } intx1 = (Integer)vPointx.elementAt(0); xmin = intx1.doubleValue(); intx2 = (Integer)vPointx.elementAt(cnt-1); xmax = intx2.doubleValue(); idx = (xmax-xmin)/mm; s = 1; for (t = 0;t <= mm;t++) { x = xmin+idx*t; intx1 = (Integer)vPointx.elementAt(s); if (x >= intx1.doubleValue()) s++; intx1 = (Integer)vPointx.elementAt(s-1); tk = x - intx1.doubleValue(); inty1 = (Integer)vPointy.elementAt(s-1); y = ((a3[s]*tk+a2[s])*tk+xu[s-1])*tk+inty1.doubleValue(); ix = x; iy = y; if (t != 0) { g.drawLine((int)dx,(int)dy,(int)ix,(int)iy); } dx = ix; dy = iy; } } } else if (jug == 3 || jug == 4) { drawGraph(); } } public void setGraph() { double x,y,dx,dy,ix,iy; Integer ibx,iby; int cnt = vPointx.size(); vCoorx.removeAllElements(); vCoory.removeAllElements(); if (jug == 3) { int h,len; double m,sumx,sumy,sumx2,sumxy,bb,a,b; String cc,bua,bub; char bufa[] = new char[10]; char bufb[] = new char[10]; dx = dy = sumx = sumy = sumx2 = sumxy = 0; nPoint = 0; for (int iCnt = 0;iCnt < cnt;iCnt++) { ibx = (Integer)vPointx.elementAt(iCnt); iby = (Integer)vPointy.elementAt(iCnt); double dbx = ibx.doubleValue()/bai; double dby = iby.doubleValue()/bai; sumx += dbx; sumy += dby; sumx2 += dbx*dbx; sumxy += dbx*dby; } bb = cnt*sumx2-sumx*sumx; a = (cnt*sumxy-sumx*sumy)/bb; b = (sumx2*sumy-sumx*sumxy)/bb; for (m = -wid/bai;m <= wid/bai;m += 0.01) { x = m; y = a*m+b; if (y > 299) y = 299; else if (y <- 299) y = -299; x = x*bai; y = y*bai; if (m != -wid) { Integer intx = new Integer((int)x); Integer inty = new Integer((int)y); vCoorx.addElement(intx); vCoory.addElement(inty); nPoint++; } dx = x; dy = y; } } else if (jug == 4) { int s,t,u,imax,len; double aik,ee,h,m,pp,v,z; String cc; double bx[][] = new double[100][100]; double by[] = new double[100]; double nx[] = new double[100]; char bufn[] = new char[10]; char buft[] = new char[10]; dx = dy = 0; nPoint = 0; if (cnt > 1) { for (s = 0;s < cnt;s++) { for (t = 0;t < cnt;t++) { u = cnt-t-1; ibx = (Integer)vPointx.elementAt(s); double dbx = ibx.doubleValue()/bai; bx[s][t] = Math.pow(dbx,u); } iby = (Integer)vPointy.elementAt(s); double dby = iby.doubleValue()/bai; by[s] = dby; } for (s = 0;s < cnt-1;s++) { h = Math.abs(bx[s][s]); imax = s; for (t = s+1;t < cnt;t++) { aik = Math.abs(bx[t][s]); if (aik > h) { h = aik; imax = t; } } if (h == 0.0) return; if (imax != s) { for (t = s;t < cnt;t++) { ee = bx[s][t]; bx[s][t] = bx[imax][t]; bx[imax][t] = ee; } ee = by[s]; by[s] = by[imax]; by[imax] = ee; } pp = bx[s][s]; for (t = s;t < cnt;t++) bx[s][t] = bx[s][t]/pp; by[s] = by[s]/pp; for (t = s+1;t < cnt;t++) { v = bx[t][s]; for (u = s;u < cnt;u++) bx[t][u] = bx[t][u]-v*bx[s][u]; by[t] = by[t]-v*by[s]; } } nx[cnt] = by[cnt]/bx[cnt][cnt]; for (s = cnt-1;s >= 0;s--) { z = by[s]; for (t = s+1;t < cnt;t++) z = z-bx[s][t]*nx[t]; nx[s] = z; } for (m = -wid/bai;m <= wid/bai;m += 0.01) { x = m; y = 0.0; cc = ""; for (s = 0;s < cnt;s++) { t = cnt-s-1; y += nx[s]*Math.pow(m,t); } if (y > 299) y = 299; else if (y <- 299) y = -299; x = x*bai; y = y*bai; if (m != -wid) { Integer intx = new Integer((int)x); Integer inty = new Integer((int)y); vCoorx.addElement(intx); vCoory.addElement(inty); nPoint++; } dx = x; dy = y; } } } } public boolean action(Event e,Object o) { if (e.target instanceof Checkbox) { Checkbox c = (Checkbox)e.target; if ("グラフなし".equals(c.getLabel())) { jug = 0; } else if ("直線で結ぶ".equals(c.getLabel())) { jug = 1; } else if ("曲線で結ぶ".equals(c.getLabel())) { jug = 2; } else if ("最小二乗法".equals(c.getLabel())) { jug = 3; setGraph(); } else if ("座標を通過する".equals(c.getLabel())) { jug = 4; setGraph(); } } else if (e.target instanceof TextField) { coo = (String)o; cj = 0; Coordinates(coo); coo = "( "+coo+")"; L.addItem(coo); if (cj != -1) { tx.setText(""); coo = ""; } } else if (e.target instanceof Button) { Button b = (Button)e.target; if ("全て削除".equals(b.getLabel())) { L.clear(); tx.setText(""); vPointx.removeAllElements(); vPointy.removeAllElements(); vCoorx.removeAllElements(); vCoory.removeAllElements(); } else if ("拡大".equals(b.getLabel())) { if (bai < 100) { bai += 10; vPointx.removeAllElements(); vPointy.removeAllElements(); vCoorx.removeAllElements(); vCoory.removeAllElements(); for (int iCnt = 0;iCnt < L.getItemCount();iCnt++) { String str = L.getItem(iCnt); str = str.substring(1,str.length()-1); Coordinates(str); setGraph(); } } else return true; } else if ("縮小".equals(b.getLabel())) { if (bai > 20) { bai -= 10; vPointx.removeAllElements(); vPointy.removeAllElements(); vCoorx.removeAllElements(); vCoory.removeAllElements(); for (int iCnt = 0;iCnt < L.getItemCount();iCnt++) { String str = L.getItem(iCnt); str = str.substring(1,str.length()-1); Coordinates(str); setGraph(); } } else return true; } } repaint(); return true; } public boolean mouseUp(Event e,int x,int y) { double x1,y1; String dot = ""; coo = ""; if (y < 35 || y > hei-80) return true; x1 = x-wid/2; y1 = y-hei/2; x1 = x1/bai; y1 = -y1/bai; dot = Double.toString(x1); if (x1 > 0.0) dot = " "+dot; if (dot.length() > 5) coo = dot.substring(0,5); else coo = dot; coo = coo+','; dot = ""; dot = Double.toString(y1); if (y1 > 0.0) dot = " "+dot; if (dot.length() > 5) dot = dot.substring(0,5); coo = coo+dot; Coordinates(coo); coo = "( "+coo+")"; L.addItem(coo); if (jug == 3 || jug == 4) setGraph(); repaint(); return true; } public void cerr() { cj = -1; } public void Coordinates(String coo) { coo = coo+' '; int i,len; double x=0,y=0,x1,x2; char a; g = getGraphics(); g.translate(wid/2,hei/2); len = coo.length(); int b = 0; int c = -1; for (i = 0;i < len;i++) { a = coo.charAt(i); if (a == '-') { c = -c; if (i !=0 && cj == i) { cerr(); showStatus("正しく入力してください。"); return; } } else if (a >= '0' && a <= '9') { x1 = a-'0'; cj = i+1; while ((a = coo.charAt(++i)) >= '0' && a <= '9') x1 = 10*x1+(a-'0'); if (a == '.') { x2 = 1.0; while ((a = coo.charAt(++i)) >= '0' && a <= '9') { x2 = x2/10.0; x1 = x1+x2*(a-'0'); } } a = coo.charAt(--i); if (c == 1) x1 = -x1; if (b == 0) x = x1; else y = x1; } else if (a == ',') { if (b == 1) { cerr(); showStatus("x , y の形式で入力してください。"); return; } b = 1; c = -1; x1 = 0; } else if (a == ' ') cj++; else if (a == '+') { if (i == 0) ; else if (cj == i) { cerr(); showStatus("正しく入力してください。"); return; } } else { cerr(); showStatus("正しく入力してください。"); return; } } if (b == 0) { cerr(); showStatus("x , y の形式で入力してください。"); return; } px[cn] = x; py[cn] = y; cn++; x = x*bai; y = -y*bai; Integer ix = new Integer((int)x); Integer iy = new Integer((int)y); vPointx.addElement(ix); vPointy.addElement(iy); } public void drawCoor(int i) { double x,y; Integer ix,iy; String strCoor; ix = (Integer)vPointx.elementAt(i); iy = (Integer)vPointy.elementAt(i); g = getGraphics(); g.translate(wid/2,hei/2); g.fillOval(ix.intValue()-1,iy.intValue()-1,3,3); x = ix.doubleValue(); y = -iy.doubleValue(); x = x/50; y = y/50; strCoor = L.getItem(i); g.drawString(strCoor,ix.intValue()+2,iy.intValue()+15); } public void drawGraph() { int xPoint[] = new int[nPoint]; int yPoint[] = new int[nPoint]; Integer ix,iy; for (int ipos = 0;ipos < nPoint;ipos++) { ix = (Integer)vCoorx.elementAt(ipos); iy = (Integer)vCoory.elementAt(ipos); xPoint[ipos] = ix.intValue(); yPoint[ipos] = iy.intValue(); } g = getGraphics(); g.translate(wid/2,hei/2); g.drawPolyline(xPoint,yPoint,nPoint); } }