// // trigdial.java // trigdial // // This is a simple Java applet to help see how the six trig functions // can be found for obtuse angles by showing the graph and // the reference tringle inside a unit circle. Click // (or click and drag) either on the graph or the circle // to see the lengths of the reference triangle and the // value of the trig function. // // // Created by Chris Thiel on 1/9/05. // Copyright (c) 2005 Chris Thiel. All rights reserved. // Free to use or change for educational purposes, with proper // citation. You shouldn't charge people money for // this otherwise free applet. Questions, or comments? // email me at cct@ktb.net // or see latest e-mail at www.mathorama.com // import java.awt.*; import java.applet.*; import java.awt.event.*; import java.lang.*; public class trigdial extends Applet implements MouseMotionListener, MouseListener,ItemListener { String message = "0"; private Font font = new Font("serif", Font.ITALIC, 18); //global variables: static final int x0= 450; static final int y0= 200; // coords of origin static final int r= 100; // radius length int x1,y1; // point location int dx,dy; // distance of point to origin int r2=r*r; // radius squared double t; // angle int xOffset, yOffset; Choice daMenu; private String[] fName = new String[6]; // names of the 6 functions int d; //dist from drag to circle ..make local later public void init() { int i; setLayout (null); this.setBackground(new Color(0xF0FFFF)); addMouseMotionListener(this); addMouseListener(this); x1 = x0+r; y1 = y0; daMenu = new Choice(); fName[0]="sin"; fName[1]="cos"; fName[2]="tan"; fName[3]="csc"; fName[4]="sec"; fName[5]="cot"; for (i=0; i<6; i++) daMenu.addItem(fName[i]); add(daMenu); daMenu.addItemListener(this); } // PAINT STUFF public void paint (Graphics g) { g.setColor(Color.magenta); g.setFont(font); // g.drawString(message, 40, 80); g.drawString("\u03b8 = "+(t/Math.PI)+" \u03c0 radians",250,30); // \u03c0 is the Pi symbol g.drawString("or "+(t*180/Math.PI)+"\u00b0",250,53); // \u00b0 is the Degree symbol g.setColor(Color.red); g.drawString("x (adj) = "+((x1-x0)/100.0), 250, 80); g.setColor(Color.blue); g.drawString("y (opp) = "+((y0-y1)/100.0), 250, 100); g.setColor(Color.magenta); g.fillOval(x1-5,y1-5,10,10); g.drawArc(x0-r/4,y0-r/4,r/2,r/2,0,(int)(180*t/Math.PI)); g.setColor(Color.blue); g.drawLine (x1,y1,x1,y0); g.setColor(Color.black); g.drawLine (x1,y1,x0,y0); g.drawOval(x0-r,y0-r,2*r,2*r); g.drawLine( x0-r-20, y0, x0+r+20, y0); g.drawLine( x0, y0-r-20, x0, y0+r+20); g.drawLine( 20, y0, 230, y0); g.drawLine( 20, y0-r-20, 20, y0+r+20); g.drawLine( x0-r-20, y0, x0+r+20, y0); g.setFont(new Font("serif", Font.BOLD, 12)); g.drawLine( x0, y0-r-20, x0, y0+r+20); g.drawString("1", 10,y0-r); g.drawString("0", 10, y0); g.drawString("-1",10,y0+r); g.drawString("\u03c0", 65,y0+12); g.drawString("__", 62,y0+11); g.drawString("2", 65,y0+24); g.drawString("\u03c0", 115,y0+12); g.drawString("3\u03c0", 165,y0+12); g.drawString("__", 163,y0+11); g.drawString("2", 167,y0+24); g.drawString("2\u03c0", 215,y0+12); g.setFont(new Font("serif", Font.BOLD, 14)); switch (daMenu.getSelectedIndex() ){ //sin case(0):{ for(double j=0.0;j<23.0*Math.PI/12.0;j=j+Math.PI/12.0){ g.drawLine(20+(int)(j*100.0/Math.PI), y0-(int)(Math.sin(j)*r), 20+(int)((j+Math.PI/12.0)*100.0/Math.PI), y0-(int)(Math.sin((j+Math.PI/12.0))*r)); } g.fillOval(20+(int)(t*100.0/Math.PI)-2, y0-(int)(Math.sin(t)*r)-2,4,4); g.drawString("sin \u03b8 = ------ = "+ ((int)(10000*Math.sin(t))/10000.0), 235, 370); g.setColor(Color.blue); g.drawString( ((y0-y1)/100.0)+" ", 290,360); g.setColor(Color.black); g.drawString(" 1", 290,380); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; g.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); g.drawString(" 1", x0+((x1-x0)/2), y0+((y1-y0)/2)+yOffset); } break; //end of sin //cos case(1):{ for(double j=0.0;j<23.0*Math.PI/12.0;j=j+Math.PI/12.0){ g.drawLine(20+(int)(j*100.0/Math.PI), y0-(int)(Math.cos(j)*r), 20+(int)((j+Math.PI/12.0)*100.0/Math.PI), y0-(int)(Math.cos((j+Math.PI/12.0))*r)); } g.fillOval(20+(int)(t*100.0/Math.PI)-2, y0-(int)(Math.cos(t)*r)-2,4,4); g.drawString("cos \u03b8 = ------ = "+ ((int)(10000*Math.cos(t))/10000.0), 235, 370); g.setColor(Color.red); g.drawString( ((x1-x0)/100.0)+" ", 290,360); g.setColor(Color.black); g.drawString(" 1", 290,380); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; g.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); g.drawString(" 1", x0+((x1-x0)/2), y0+((y1-y0)/2)+yOffset); } break; //end of cos //tan case(2):{ for(double j=-1*Math.PI/13.0;j<24.0*Math.PI/12.0;j=j+Math.PI/12.0){ g.drawLine(20+(int)(j*100.0/Math.PI), y0-(int)(Math.tan(j)*r), 20+(int)((j+Math.PI/12.0)*100.0/Math.PI), y0-(int)(Math.tan((j+Math.PI/12.0))*r)); } g.setColor(Color.red); g.fillOval(20+(int)(t*100.0/Math.PI)-2, y0-(int)(Math.tan(t)*r)-2,4,4); g.setColor(Color.black); if (x0-x1==0){g.drawString("tan \u03b8 = ------ = undefined", 235, 370);} else{ g.drawString("tan \u03b8 = ------ = "+ ((int)(10000*Math.tan(t))/10000.0), 235, 370);} g.setColor(Color.blue); g.drawString( ((y0-y1)/100.0)+" ", 290,360); g.setColor(Color.red); g.drawString( ((x1-x0)/100.0)+" ", 290,380); g.setColor(Color.black); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; g.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); g.drawString(" 1", x0+((x1-x0)/2), y0+((y1-y0)/2)+yOffset); } break; //end of tan //csc case(3):{ for(double j=Math.PI/13.0;j<24.0*Math.PI/12.0;j=j+Math.PI/12.0){ g.drawLine(20+(int)(j*100.0/Math.PI), y0-(int)(r/(Math.sin(j))), 20+(int)((j+Math.PI/12.0)*100.0/Math.PI), y0-(int)(r/Math.sin((j+Math.PI/12.0)))); } g.fillOval(20+(int)(t*100.0/Math.PI)-2, y0-(int)(r/Math.sin(t))-2,4,4); if(y1-y0==0){g.drawString("csc \u03b8 = ------ = undefined", 235,370 );} else {g.drawString("csc \u03b8 = ------ = "+ (1.0/Math.sin(t)), 235, 370);} g.setColor(Color.blue); g.drawString( ((y0-y1)/100.0)+" ", 290,380); g.setColor(Color.black); g.drawString(" 1", 290,360); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; g.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); g.drawString(" 1", x0+((x1-x0)/2), y0+((y1-y0)/2)+yOffset); } break; //end of csc //sec case(4):{ for(double j=0.0;j<25.0*Math.PI/13.0;j=j+Math.PI/13.0){ g.drawLine(20+(int)(j*100.0/Math.PI), y0-(int)(r/Math.cos(j)), 20+(int)((j+Math.PI/13.0)*100.0/Math.PI), y0-(int)(r/Math.cos((j+Math.PI/13.0)))); } g.fillOval(20+(int)(t*100.0/Math.PI)-2, y0-(int)(r/Math.cos(t))-2,4,4); if(x0-x1==0) {g.drawString("sec \u03b8 = ------ = undefined", 235, 370);} else {g.drawString("sec \u03b8 = ------ = "+ (1.0/Math.cos(t)), 235, 370);} g.setColor(Color.red); g.drawString( ((x1-x0)/100.0)+" ", 290,380); g.setColor(Color.black); g.drawString(" 1", 290,360); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; g.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); g.drawString(" 1", x0+((x1-x0)/2), y0+((y1-y0)/2)+yOffset); } break; //end of sec //cot case(5):{ for(double j=Math.PI/13.0;j<25.0*Math.PI/12.0;j=j+Math.PI/12.0){ g.drawLine(20+(int)(j*100.0/Math.PI), y0-(int)(r/Math.tan(j)), 20+(int)((j+Math.PI/12.0)*100.0/Math.PI), y0-(int)(r/Math.tan((j+Math.PI/12.0)))); } g.setColor(Color.red); g.fillOval(20+(int)(t*100.0/Math.PI)-2, y0-(int)(r/Math.tan(t))-2,4,4); g.setColor(Color.black); if (y0-y1==0){g.drawString("cot \u03b8 = ------ = undefined", 235, 370);} else{ g.drawString("cot \u03b8 = ------ = "+ (int)(10000*(1/Math.tan(t)))/10000.0, 235, 370);} g.setColor(Color.blue); g.drawString( ((y0-y1)/100.0)+" ", 290,380); g.setColor(Color.red); g.drawString( ((x1-x0)/100.0)+" ", 290,360); g.setColor(Color.black); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; g.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); g.drawString(" 1", x0+((x1-x0)/2), y0+((y1-y0)/2)+yOffset); } break; //end of cot }//end of switch g.setColor(Color.red); g.drawLine (x1,y0,x0,y0); } // MOUSE MOTION LISTENER STUFF private void updateByCircle(int mx, int my){ d = (int)(Math.sqrt(dx*dx+dy*dy) - r); // (how close drag is to circle) dx = mx-x0; dy = y0-my; // (vertical screen coords are reversed from the cartesian coord system) t=Math.acos(dx/Math.sqrt(dx*dx+dy*dy)); // this way results are from 0 to Pi if (dy<0) t=2*Math.PI-t; // adjustment for the computer's trig function, since it expects 0-Pi x1=x0+(int)(r*Math.cos(t)); y1=y0+(int)(-1*r*Math.sin(t)); } private void updateByGraph(int mx){ t=Math.PI*(mx-20)/100.0; x1=x0+(int)(r*Math.cos(t)); y1=y0+(int)(-1*r*Math.sin(t)); } public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { int mx=e.getX(); int my=e.getY(); if (mx > 250) {updateByCircle(mx,my);} if (mx > 19 && mx<221){updateByGraph(mx);} repaint(); } // MOUSE LISTENER STUFF public void mouseClicked(MouseEvent e) { int mx=e.getX(); int my=e.getY(); if (mx > 250) {updateByCircle(mx,my);} if (mx > 19 && mx<221){updateByGraph(mx);} repaint(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void itemStateChanged(ItemEvent e) { repaint();} //redraw if the trig function was changed }