import java.awt.*; import java.awt.event.*; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import java.applet.Applet; public class slice extends Applet implements ItemListener { CurvePanel mainpanel; Button plusButton,minusButton,upButton,downButton,leftButton,rightButton,resetButton; Checkbox singleCross, multiCross, approx; CheckboxGroup cbg; //double tr[][] = {{1,0,0},{0,1,0},{0,0,1}}; double tr[][] = {{Math.cos(5/36.*Math.PI),0,-Math.sin(5/36.*Math.PI)}, {-Math.sin(5/36.*Math.PI)*Math.sin(5/36.*Math.PI), Math.cos(5/36.*Math.PI),-Math.sin(5/36.*Math.PI)*Math.cos(5/36.*Math.PI)}, {Math.sin(5/36.*Math.PI)*Math.cos(5/36.*Math.PI),Math.sin(5/36.*Math.PI), Math.cos(5/36.*Math.PI)*Math.cos(5/36.*Math.PI)}}; double sin5 = Math.sin(5/180.*Math.PI), cos5 = Math.cos(5/180.*Math.PI); double temp,volume,aveArea; double t0,x1,y1,z1,x0,y0,z0; int curvex[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int curvey[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; double yt1(double t) {double z=5*(1-t)+.09004962810*t ; return -1.3*(.005787858720*z*z*z-.05447596853*z*z+.1096702813*z+.09006546675) ;} double zt1(double t) {return 5*(1-t)+.09004962810*t ;} double yt2(double t) {double z=2.034443936*t + 4.612720328*(1-t); return .13*Math.sin(z) ;} double zt2(double t) {double z=2.034443936*t + 4.612720328*(1-t); return .1+.1*Math.cos(z) ;} double yt3(double t) {double z = .05527864044*(1-t)+5*t; return 1.3*(.01783935569*z*z*z-.1959451301*z*z+.5214996241*z+.06121067058);} double zt3(double t) {return .05527864044*(1-t)+5*t;} int xtran(double x, double y, double z) {return (int)(200+16*(tr[0][0]*x+tr[0][1]*y+tr[0][2]*z)+.5);} int ytran(double x, double y, double z) {return (int)(200-16*(tr[1][0]*x+tr[1][1]*y+tr[1][2]*z)+.5);} double xend,xend0,shrink; int sliceLoc=0, numSlices=5,numSlabs=3; double sliceWidth,slabWidth; ActionListener resetListener = new ActionListener() { public void actionPerformed (ActionEvent e) { tr[0][0] = Math.cos(5/36.*Math.PI); tr[0][1] = 0; tr[0][2] = -Math.sin(5/36.*Math.PI); tr[1][0] = -Math.sin(5/36.*Math.PI)*Math.sin(5/36.*Math.PI); tr[1][1] = Math.cos(5/36.*Math.PI); tr[1][2] = -Math.sin(5/36.*Math.PI)*Math.cos(5/36.*Math.PI); tr[2][0] = Math.sin(5/36.*Math.PI)*Math.cos(5/36.*Math.PI); tr[2][1] = Math.sin(5/36.*Math.PI); tr[2][2] = Math.cos(5/36.*Math.PI)*Math.cos(5/36.*Math.PI); sliceLoc=0; numSlices=5; numSlabs=3; mainpanel.repaint(); } }; ActionListener plusListener = new ActionListener() { public void actionPerformed (ActionEvent e) { if (singleCross.getSelectedObjects()!=null) {sliceLoc+=2; if (sliceLoc>10) sliceLoc=10;} if (multiCross.getSelectedObjects()!=null) numSlices+=1; if (approx.getSelectedObjects()!=null) numSlabs+=1; mainpanel.repaint(); } }; ActionListener minusListener = new ActionListener() { public void actionPerformed (ActionEvent e) { if (singleCross.getSelectedObjects()!=null) {sliceLoc-=2; if (sliceLoc<-10) sliceLoc=-10;} if (multiCross.getSelectedObjects()!=null) {numSlices-=1; if (numSlices<1) numSlices=1;} if (approx.getSelectedObjects()!=null) {numSlabs-=1; if (numSlabs<1) numSlabs=1;} mainpanel.repaint(); } }; ActionListener upListener = new ActionListener() { public void actionPerformed (ActionEvent e) { for (int i=0;i<3;i++){ temp = tr[1][i]*cos5 + tr[2][i]*sin5; tr[2][i] = -tr[1][i]*sin5 + tr[2][i]*cos5; tr[1][i] = temp; } mainpanel.repaint(); } }; ActionListener downListener = new ActionListener() { public void actionPerformed (ActionEvent e) { for (int i=0;i<3;i++){ temp = tr[1][i]*cos5 - tr[2][i]*sin5; tr[2][i] = tr[1][i]*sin5 + tr[2][i]*cos5; tr[1][i] = temp; } mainpanel.repaint(); } }; ActionListener leftListener = new ActionListener() { public void actionPerformed (ActionEvent e) { for (int i=0;i<3;i++){ temp = tr[0][i]*cos5 - tr[2][i]*sin5; tr[2][i] = tr[0][i]*sin5 + tr[2][i]*cos5; tr[0][i] = temp; } mainpanel.repaint(); } }; ActionListener rightListener = new ActionListener() { public void actionPerformed (ActionEvent e) { for (int i=0;i<3;i++){ temp = tr[0][i]*cos5 + tr[2][i]*sin5; tr[2][i] = -tr[0][i]*sin5 + tr[2][i]*cos5; tr[0][i] = temp; } mainpanel.repaint(); } }; public void init(){ setBackground(Color.white); mainpanel = new CurvePanel(); setLayout(null); mainpanel.setBounds(200,0,399,399); add(mainpanel); setFont(new Font("SansSerif",Font.PLAIN,12)); plusButton = new Button("+"); add(plusButton); minusButton = new Button("-"); add(minusButton); upButton = new Button("up"); add(upButton); downButton = new Button("down"); add(downButton); leftButton = new Button("left"); add(leftButton); rightButton = new Button("right"); add(rightButton); resetButton = new Button("reset"); add(resetButton); plusButton.setBounds(100,50,20,20); minusButton.setBounds(80,50,20,20); upButton.setBounds(80,300,40,20); leftButton.setBounds(40,320,40,20); resetButton.setBounds(80,320,40,20); rightButton.setBounds(120,320,40,20); downButton.setBounds(80,340,40,20); resetButton.addActionListener(resetListener); plusButton.addActionListener(plusListener); minusButton.addActionListener(minusListener); upButton.addActionListener(upListener); downButton.addActionListener(downListener); leftButton.addActionListener(leftListener); rightButton.addActionListener(rightListener); cbg = new CheckboxGroup(); singleCross = new Checkbox("Single Cross-section", cbg, true); add(singleCross); multiCross = new Checkbox("Many Cross-sections", cbg, false); add(multiCross); approx = new Checkbox("Approximate Volume", cbg, false); add(approx); singleCross.setBounds(30,140,150,20); singleCross.addItemListener(this); multiCross.setBounds(30,160,150,20); multiCross.addItemListener(this); approx.setBounds(30,180,150,20); approx.addItemListener(this); } public void itemStateChanged(ItemEvent e) { mainpanel.repaint(); } public class CurvePanel extends Panel { public void paint(Graphics g){ g.setColor(Color.black); g.drawRect(0,0,398,398); //box it for (int end=0; end<=1; end++){ if (end==0) shrink=1; else shrink=.4; if (end==0) xend=10; else xend=-10; for (int i=0; i<=10; i++) {t0 = i/10.; //curve1 if (i>0) {x0=x1; y0=y1; z0=z1;} x1=xend; y1=shrink*yt1(t0); z1=shrink*zt1(t0); if (i>0) g.drawLine(xtran(x0,y0,z0),ytran(x0,y0,z0),xtran(x1,y1,z1),ytran(x1,y1,z1)); } for (int i=0; i<=10; i++) {t0 = i/10.; //curve2 if (i>0) {x0=x1; y0=y1; z0=z1;} x1=xend; y1=shrink*yt2(t0); z1=shrink*zt2(t0); if (i>0) g.drawLine(xtran(x0,y0,z0),ytran(x0,y0,z0),xtran(x1,y1,z1),ytran(x1,y1,z1)); } for (int i=0; i<=10; i++) {t0 = i/10.; //curve3 if (i>0) {x0=x1; y0=y1; z0=z1;} x1=xend; y1=shrink*yt3(t0); z1=shrink*zt3(t0); if (i>0) g.drawLine(xtran(x0,y0,z0),ytran(x0,y0,z0),xtran(x1,y1,z1),ytran(x1,y1,z1)); } g.drawLine(xtran(-10,0,0),ytran(-10,0,0),xtran(13,0,0),ytran(13,0,0)); //leading edge? // g.drawLine(xtran(-10,.4,0),ytran(-10,.4,0),xtran(10,1,0),ytran(10,1,0)); g.drawLine(xtran(-10,0,2),ytran(-10,0,2),xtran(10,0,5),ytran(10,0,5)); //trailing edge } if (singleCross.getSelectedObjects()!=null) { shrink=(70+3*sliceLoc)/100.; g.setFont(new Font("Dialog",0,16)); String s = "Area of slice = " + String.valueOf((int)(1000*shrink*shrink*2.5+.5)/1000.) + " sq.ft."; g.drawString(s,10,20); g.setColor(Color.blue); xend=sliceLoc; for (int i=0; i<10; i++) {t0 = i/10.; //curve1 x1=xend; y1=shrink*yt1(t0); z1=shrink*zt1(t0); curvex[i]=xtran(x1,y1,z1);curvey[i]=ytran(x1,y1,z1); } for (int i=0; i<10; i++) {t0 = i/10.; //curve2 x1=xend; y1=shrink*yt2(t0); z1=shrink*zt2(t0); curvex[i+10]=xtran(x1,y1,z1); curvey[i+10]=ytran(x1,y1,z1); } for (int i=0; i<10; i++) {t0 = i/10.; //curve3 x1=xend; y1=shrink*yt3(t0); z1=shrink*zt3(t0); curvex[i+20]=xtran(x1,y1,z1); curvey[i+20]=ytran(x1,y1,z1); } g.fillPolygon(curvex,curvey,30); g.drawPolygon(curvex,curvey,30); } if (multiCross.getSelectedObjects()!=null) { aveArea=0; sliceWidth = 20./(numSlices+1); g.setColor(Color.red); for (int j=1; j<=numSlices; j++){ xend=-10+j*sliceWidth; shrink=(70+3*xend)/100.; aveArea+=shrink*shrink*2.5; for (int i=0; i<10; i++) {t0 = i/10.; //curve1 x1=xend; y1=shrink*yt1(t0); z1=shrink*zt1(t0); curvex[i]=xtran(x1,y1,z1);curvey[i]=ytran(x1,y1,z1); } for (int i=0; i<10; i++) {t0 = i/10.; //curve2 x1=xend; y1=shrink*yt2(t0); z1=shrink*zt2(t0); curvex[i+10]=xtran(x1,y1,z1); curvey[i+10]=ytran(x1,y1,z1); } for (int i=0; i<10; i++) {t0 = i/10.; //curve3 x1=xend; y1=shrink*yt3(t0); z1=shrink*zt3(t0); curvex[i+20]=xtran(x1,y1,z1); curvey[i+20]=ytran(x1,y1,z1); } g.fillPolygon(curvex,curvey,30); g.drawPolygon(curvex,curvey,30); } g.setColor(Color.black); g.setFont(new Font("Dialog",0,16)); String s = "Ave. area of slices = " + String.valueOf((int)(1000*aveArea/numSlices+.5)/1000.) + " sq.ft."; g.drawString(s,10,20); } if (approx.getSelectedObjects()!=null) { slabWidth = 20./numSlabs; volume = 0.0; g.setColor(Color.magenta); slabWidth = 20./numSlabs; for (int j=0; j