/* sublapsim.java copyright 2007,2008 William T. Prewitt, P.E. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Subdiameter lap mirror figuring simulator */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.io.*; import java.lang.String; import java.util.Random; /* declare it this way in the page */ public class sublapsim extends Applet implements ActionListener { TextField mirrrad; TextField laprad; TextField strokerad1; TextField strokelen1; TextField strokerad2; TextField strokelen2; TextField strokerad3; TextField strokelen3; TextField strokerad4; TextField strokelen4; TextField strokerad5; TextField strokelen5; TextField myword; String action; String dispunits="Cm"; CheckboxGroup units; Checkbox Cm; Checkbox Inches; Button updateButton; Button clearButton; Button iterateButton; int radstroke1,lenstroke1; int radstroke2,lenstroke2; int radstroke3,lenstroke3; int radstroke4,lenstroke4; int radstroke5,lenstroke5; // public class ButtonGrid extends Applet { public void init() { this.setBackground(Color.white); // Label unitsp = new Label("Units:",Label.LEFT); // units = new CheckboxGroup(); // add(unitsp); // add(new Checkbox("Cm",units,true)); // add(new Checkbox("Inches",units,false)); updateButton = new Button(" <<<< Draw >>>> "); add(updateButton); Label mirrradp = new Label("Mirror radius (Cm): ",Label.RIGHT); mirrrad= new TextField("76"); // 30 inches add(mirrradp); add(mirrrad); Label lapradp = new Label("Lap radius (Cm): ",Label.RIGHT); laprad= new TextField("50"); // 20 inches add(lapradp); add(laprad); Label strokerad1p = new Label("Stroke 1 radius (Cm): ",Label.RIGHT); strokerad1= new TextField("40"); // 10 inches add(strokerad1p); add(strokerad1); Label strokelen1p = new Label("Stroke 1 length (Cm): ",Label.RIGHT); strokelen1= new TextField("40"); // 10 inches add(strokelen1p); add(strokelen1); Label strokerad2p = new Label("Stroke 2 radius (Cm): ",Label.RIGHT); strokerad2= new TextField("0"); add(strokerad2p); add(strokerad2); Label strokelen2p = new Label("Stroke 2 length (Cm): ",Label.RIGHT); strokelen2= new TextField("0"); add(strokelen2p); add(strokelen2); Label strokerad3p = new Label("Stroke 3 radius (Cm): ",Label.RIGHT); strokerad3= new TextField("0"); add(strokerad3p); add(strokerad3); Label strokelen3p = new Label("Stroke 3 length (Cm): ",Label.RIGHT); strokelen3= new TextField("0"); add(strokelen3p); add(strokelen3); Label strokerad4p = new Label("Stroke 4 radius (Cm): ",Label.RIGHT); strokerad4= new TextField("0"); add(strokerad4p); add(strokerad4); Label strokelen4p = new Label("Stroke 4 length (Cm): ",Label.RIGHT); strokelen4= new TextField("0"); add(strokelen4p); add(strokelen4); Label strokerad5p = new Label("Stroke 5 radius (Cm): ",Label.RIGHT); strokerad5= new TextField("0"); add(strokerad5p); add(strokerad5); Label strokelen5p = new Label("Stroke 5 length (Cm): ",Label.RIGHT); strokelen5= new TextField("0"); add(strokelen5p); add(strokelen5); // register to receive action events updateButton.addActionListener(this); // update button mirrrad.addActionListener(this); laprad.addActionListener(this); strokerad1.addActionListener(this); strokelen1.addActionListener(this); strokerad2.addActionListener(this); strokelen2.addActionListener(this); strokerad3.addActionListener(this); strokelen3.addActionListener(this); strokerad4.addActionListener(this); strokelen4.addActionListener(this); strokerad5.addActionListener(this); strokelen5.addActionListener(this); } // init // identify what was clicked public void actionPerformed(ActionEvent event) { // if (event.getSource() instanceof Checkbox){ // if (Cm.getState()) // { // dispunits="Cm"; // } // else // { // dispunits="Inches"; // } // } if (event.getSource() instanceof Button) { Button clickedButton = (Button) event.getSource(); if (clickedButton == updateButton) { action="draw"; } else if (clickedButton == clearButton) { action="clear"; } else if (clickedButton == iterateButton) { action="iterate"; } else { action="huh"; } } repaint(); } public void paint( Graphics g) { g.drawString(" ************* SUBLAPSIM ******************* ",2,80); g.drawString(" A Subdiameter Lap figuring simulator ",2,100); g.drawString(" written by Bill Prewitt 01 Jan 2008 ",2,120); g.drawString(" Copyright 01 Jan 2008 - all rights reserved ",2,140); radstroke1 = Integer.parseInt(strokerad1.getText()); lenstroke1 = Integer.parseInt(strokelen1.getText()); radstroke2 = Integer.parseInt(strokerad2.getText()); lenstroke2 = Integer.parseInt(strokelen2.getText()); radstroke3 = Integer.parseInt(strokerad3.getText()); lenstroke3 = Integer.parseInt(strokelen3.getText()); radstroke4 = Integer.parseInt(strokerad4.getText()); lenstroke4 = Integer.parseInt(strokelen4.getText()); radstroke5 = Integer.parseInt(strokerad5.getText()); lenstroke5 = Integer.parseInt(strokelen5.getText()); laps(g,mirrrad,laprad,dispunits,radstroke1,lenstroke1,radstroke2,lenstroke2,radstroke3,lenstroke3,radstroke4,lenstroke4,radstroke5,lenstroke5); // lap simulator } // paint public void laps (Graphics g, TextField mirrrad, TextField laprad,String dispunits, int radstroke1, int lenstroke1, int radstroke2, int lenstroke2, int radstroke3, int lenstroke3, int radstroke4, int lenstroke4, int radstroke5, int lenstroke5) { double x ,y, xo, y0, x1, y1 ; int i , j, k, l, m, n, ix, iy ,ix0, iy0, ix1, iy1; int nstrokes ; int iscale,icenter; double profile[] ; double dotstroke[][]; dotstroke = new double[200][200] ; // (x,y) profile = new double[100] ; // (radius) double dotsum=0; double profilescale; double ymult, xmult, xofs, theta, radius ; int radmirr,radlap,lapmax=0; double pirad,scale,dlapmax,center; char bitmask, bytout; int ixcenter, iycenter,ixlen, iylen, radlapsqr; int radstroke=0,lenstroke=0; int strokenum; radmirr = Integer.parseInt(mirrrad.getText()); radlap = Integer.parseInt(laprad.getText()); // g.drawString(" radmirr= "+radmirr,10,100); // g.drawString(" radlap= "+radlap,10,120); // g.drawString(" dispunits= "+dispunits,10,140); // g.drawString(" action= "+action,10,160); // test state of radio buttons scale=4.; //default to value for Cm units iscale=4; if ( dispunits .equals("Inches" )) { scale=10.; iscale=10; } // ***************************************************************** // main rendering loop: // ***************************************************************** pirad=360/3.14159; center=500.; icenter=500; x=center; y=center; theta=0; radius=scale*radmirr; for ( i=0; i <360; i++ ) // plot the mirror circle { //plot 360 strokes around the circle x=radius*Math.sin(theta); y=radius*Math.cos(theta); theta=i*pirad; ix= (int) x + (int) center; iy= (int) (center - y) ; g.drawLine(ix,iy,ix,iy); } // always draw the mirror circle and axes g.drawLine(0,500,1000,500); // horizontal X axis g.drawLine(0,500,0,500); // tic marks on X axis g.drawLine(20,510,20,490); // tic marks on X axis g.drawLine(100,510,100,490); // tic marks on X axis g.drawLine(180,510,180,490); // tic marks on X axis g.drawLine(260,510,260,490); // tic marks on X axis g.drawLine(340,510,340,490); // tic marks on X axis g.drawLine(420,510,420,490); // tic marks on X axis g.drawLine(500,510,500,490); // tic marks on X axis g.drawLine(580,510,580,490); // tic marks on X axis g.drawLine(660,510,660,490); // tic marks on X axis g.drawLine(740,510,740,490); // tic marks on X axis g.drawLine(820,510,820,490); // tic marks on X axis g.drawLine(900,510,900,490); // tic marks on X axis g.drawLine(980,510,980,490); // tic marks on X axis g.drawLine(500,0,500,1000); // vertical Y axis g.drawLine(490,0,510,0); // tic marks on Y axis g.drawLine(490,20,510,20); // tic marks on Y axis g.drawLine(490,100,510,100); // tic marks on Y axis g.drawLine(490,180,510,180); // tic marks on Y axis g.drawLine(490,260,510,260); // tic marks on Y axis g.drawLine(490,340,510,340); // tic marks on Y axis g.drawLine(490,420,510,420); // tic marks on Y axis g.drawLine(490,500,510,500); // tic marks on Y axis g.drawLine(490,580,510,580); // tic marks on Y axis g.drawLine(490,660,510,660); // tic marks on Y axis g.drawLine(490,740,510,740); // tic marks on Y axis g.drawLine(490,820,510,820); // tic marks on Y axis g.drawLine(490,900,510,900); // tic marks on Y axis g.drawLine(490,980,510,980); // tic marks on Y axis if (action .equals("clear")) { for ( j=0; j < 1000; j++ ) // clear the array { for ( i=0; i < 1000; i++ ) { dotstroke[i][j] = 0; } } } if (action .equals("draw")) { // wipe the local array for ( ix=0; ix < 200; ix++ ) { for ( iy=0; iy <200; iy++ ) { dotstroke[ix][iy] = 0; } } for (strokenum=1; strokenum<6; strokenum++) { if (strokenum == 1){ radstroke=radstroke1; lenstroke=lenstroke1; } if (strokenum == 2){ radstroke=radstroke2; lenstroke=lenstroke2; } if (strokenum == 3){ radstroke=radstroke3; lenstroke=lenstroke3; } if (strokenum == 4){ radstroke=radstroke4; lenstroke=lenstroke4; } if (strokenum == 5){ radstroke=radstroke5; lenstroke=lenstroke5; } dlapmax = Math.sqrt((lenstroke/2)*(lenstroke/2)+radstroke*radstroke); lapmax = (int) dlapmax; if (lenstroke > 0)g.drawString(" lap excursion= "+lapmax,10,180+50*strokenum); if (lapmax > radmirr){ g.drawString(" Excessive lap excursion ! ",10,200+50*strokenum); } else { ix0= icenter - (iscale/2) * lenstroke; iy0= icenter - iscale * radstroke; ix1= icenter + (iscale/2) * lenstroke; iy1= icenter - iscale * radstroke; g.drawLine(ix0,iy0,ix1,iy1); iycenter= 100+radstroke; // Y value of lap center ixlen=lenstroke/2; // half length of lap stroke radlapsqr=radlap*radlap; // square of lap radius for ( j=0; j < lenstroke; j++ ) // plot each lap circle to display { theta=0; radius=scale*radlap; for ( i=0; i <360; i++ ) // plot the mirror circle to the screen { //plot 360 strokes around the circle theta=i*pirad; x=radius*Math.sin(theta); y=radius*Math.cos(theta); ix0= (int) x + (int) center - (int) iscale/2 * lenstroke +iscale * j; iy0= (int) (center - y) - (int) (iscale) * radstroke; g.drawLine(ix0,iy0,ix0,iy0); } // draw the local array at this center ixcenter= 100+j-ixlen; // X value of lap center for ( ix=0; ix < 200; ix++ ) // plot each lap circle to local array { for ( iy=0; iy <200; iy++ ) // plot the mirror circle { // add 1 for each dot in grid if under the lap if (((ix-ixcenter)*(ix-ixcenter)+(iy-iycenter)*(iy-iycenter)) < radlapsqr) { dotsum = dotstroke[ix][iy]; dotstroke[ix][iy] = dotsum + 1; } } } // plot the lap circle to local array } // draw all lap circles } // draw passes syntax checks } // strokenum <6 for ( j=0; j <100; j++ ) // clear the profile { profile[j]=0; } // g.drawString(" iterate waypoint 1",10,200); for ( j=0; j <360; j++ ) // evaluate for each of 360 pie slices { theta=j/pirad; for ( i=0; i < 100; i++ ) // plot contribution at each unit of radius { x=Math.sin(theta) * i; y=Math.cos(theta) * i; ix= (int) x + 100 ; iy= (int) y + 100 ; dotsum= dotstroke[ix][iy]; profile[i] = profile[i] + dotsum ; } } // g.drawString(" iterate waypoint 2",10,220); for (i=0;i