/* Hidden curve removal by T.N '97 7/17 class Point3; // 頂点の3次元座標 class Trans; // 座標変換 */ import java.applet.Applet; import java.awt.*; //import java.io.*; /* **************************************************************: */ public class hidfunc extends java.applet.Applet{ int width, height; int x_origin,y_origin; //center of screem double theta=70., phi=20., R=800.; // 視点位置 int idrag=0; Point prev = new Point(0, 0); Trans henkan = new Trans(); /* **************************************************************: */ public void init() { setBackground(Color.lightGray); Dimension appsize = size(); width = appsize.width; // for JDK1.1 height = appsize.height; // width = getSize().width; // for JDK1.2 // height = getSize().height; x_origin = width/2; y_origin = height/2; henkan.setVRpnt(0.,0.,0.); // set view refrence point henkan.setview(R,theta,phi); // set viewpoint } /* *************** 描画関数 *****************************: */ public void paint(Graphics g) { int px,py; int ymin[] = new int[width]; int ymax[] = new int[width]; g.setColor(Color.black); henkan.preper(); // 座標変換の係数セット for(int i=0;i=-170;z=z-dz){ for(int x=-170;x<=170;x=x+dx){ Point3 P = FuncHat(x,z); // 関数生成 P(x,y,z) Point q = henkan.project(P); // 透視投影 q(x,y) px = x_origin + q.x ; py = y_origin - q.y; if(px>0 && pxymax[px]){ ymax[px]=py; g.drawLine(px,py, px,py); } } } // loop for x } // loop for z } /* ************** Function z = F(x,y) *******************: */ public Point3 FuncHat(int x,int y) { double rd= Math.PI/180; double r = Math.sqrt(x*x+y*y)*rd; double z=30.*(Math.cos(r)+Math.cos(3.*r)); return new Point3(x,y,z); } /* **************************************************************: */ public boolean mouseDown(Event evt, int x, int y) { prev = new Point(x, y); repaint(); return true; } // ------------------------------------------------------------------ public boolean mouseDrag(Event ev, int x, int y) { int ia,ib; ia = x-prev.x; ib=y-prev.y; if(ia>8 || ia<-8 || ib>8 || ib<-8) { idrag=1; theta += ia/2; phi -= ib/2; henkan.setview(R,theta,phi); } else idrag=0; repaint(); prev.move(x, y); return true; } // ------------------------------------------------------------------ public boolean mouseUp(Event ev, int x, int y) { prev = null; idrag=0; return true; } } // -- Class for perspective transformation --------- class Trans { double cos_theta=1., cos_phi=1., sin_theta=0., sin_phi=0.; Point3 ptViewRef = new Point3(0, 0, 0); // view reference point double theta, phi, R; Trans() { } public void setVRpnt( double x, double y, double z) { ptViewRef = new Point3(x,y,z); } public void setview(double r, double the, double ph) { theta=the; phi=ph; R=r; } // perspective transformation public void preper() { double d2r= Math.PI/180; cos_theta = Math.cos(theta*d2r); sin_theta = Math.sin(theta*d2r); cos_phi = Math.cos(phi*d2r); sin_phi = Math.sin(phi*d2r); } // Transforms world coordinate-system to viewpoint coordinate-system. public Point3 tranfrm(Point3 point) { Point3 p1 = new Point3(); Point3 p2 = new Point3(); p1.x = point.x - ptViewRef.x; p1.y = point.y - ptViewRef.y; p1.z = point.z - ptViewRef.z; double xy = cos_theta*p1.x+sin_theta*p1.y; p2.z =(R-cos_phi*xy-sin_phi*p1.z)/R; p2.x = cos_theta*p1.y-sin_theta*p1.x; p2.y = cos_phi * p1.z - sin_phi * xy; return p2; } public Point project(Point3 point) { Point3 p1= tranfrm(point); return new Point((int)(p1.x/p1.z), (int)(p1.y/p1.z)); } } // 3D real point class class Point3 { public double x, y, z; Point3() { } Point3(double ox, double oy, double oz) { x = ox; y = oy; z = oz; } }