// // 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 // // 31 Aug 2010 Added Graphics Buffer for better look on Windoze // Changed Layout to non null for Java 6 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 Image virtualMem; Graphics gBuffer; public void init() { int i; setLayout (new FlowLayout()); 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); virtualMem = createImage(getWidth(),getHeight()); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,getWidth(),getHeight()); } // PAINT STUFF public void paint (Graphics g) { virtualMem = createImage(getWidth(),getHeight()); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,getWidth(),getHeight()); gBuffer.setColor(Color.magenta); gBuffer.setFont(font); // gBuffer.drawString(message, 40, 80); gBuffer.drawString("\u03b8 = "+(t/Math.PI)+" \u03c0 radians",250,50); // \u03c0 is the Pi symbol gBuffer.drawString("or "+(t*180/Math.PI)+"\u00b0",250,73); // \u00b0 is the Degree symbol gBuffer.setColor(Color.red); gBuffer.drawString("x (adj) = "+((x1-x0)/100.0), 250, 100); gBuffer.setColor(Color.blue); gBuffer.drawString("y (opp) = "+((y0-y1)/100.0), 250, 120); gBuffer.setColor(Color.magenta); gBuffer.fillOval(x1-5,y1-5,10,10); gBuffer.drawArc(x0-r/4,y0-r/4,r/2,r/2,0,(int)(180*t/Math.PI)); gBuffer.setColor(Color.blue); gBuffer.drawLine (x1,y1,x1,y0); gBuffer.setColor(Color.black); gBuffer.drawLine (x1,y1,x0,y0); gBuffer.drawOval(x0-r,y0-r,2*r,2*r); gBuffer.drawLine( x0-r-20, y0, x0+r+20, y0); gBuffer.drawLine( x0, y0-r-20, x0, y0+r+20); gBuffer.drawLine( 20, y0, 230, y0); gBuffer.drawLine( 20, y0-r-20, 20, y0+r+20); gBuffer.drawLine( x0-r-20, y0, x0+r+20, y0); gBuffer.setFont(new Font("serif", Font.BOLD, 12)); gBuffer.drawLine( x0, y0-r-20, x0, y0+r+20); gBuffer.drawString("1", 10,y0-r); gBuffer.drawString("0", 10, y0); gBuffer.drawString("-1",10,y0+r); gBuffer.drawString("\u03c0", 65,y0+12); gBuffer.drawString("__", 62,y0+11); gBuffer.drawString("2", 65,y0+24); gBuffer.drawString("\u03c0", 115,y0+12); gBuffer.drawString("3\u03c0", 165,y0+12); gBuffer.drawString("__", 163,y0+11); gBuffer.drawString("2", 167,y0+24); gBuffer.drawString("2\u03c0", 215,y0+12); gBuffer.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){ gBuffer.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)); } gBuffer.setColor(Color.BLUE); gBuffer.fillOval(20+(int)(t*100.0/Math.PI)-4, y0-(int)(Math.sin(t)*r)-4,8,8); gBuffer.setColor(Color.BLACK); gBuffer.drawString("sin \u03b8 = ------ = "+ ((int)(10000*Math.sin(t))/10000.0), 235, 370); gBuffer.setColor(Color.blue); gBuffer.drawString( ((y0-y1)/100.0)+" ", 290,360); gBuffer.setColor(Color.black); gBuffer.drawString(" 1", 290,380); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; gBuffer.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); gBuffer.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){ gBuffer.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)); } gBuffer.setColor(Color.BLUE); gBuffer.fillOval(20+(int)(t*100.0/Math.PI)-4, y0-(int)(Math.cos(t)*r)-4,8,8); gBuffer.setColor(Color.BLACK); gBuffer.drawString("cos \u03b8 = ------ = "+ ((int)(10000*Math.cos(t))/10000.0), 235, 370); gBuffer.setColor(Color.red); gBuffer.drawString( ((x1-x0)/100.0)+" ", 290,360); gBuffer.setColor(Color.black); gBuffer.drawString(" 1", 290,380); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; gBuffer.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); gBuffer.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){ gBuffer.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)); } gBuffer.setColor(Color.BLUE); gBuffer.fillOval(20+(int)(t*100.0/Math.PI)-4, y0-(int)(Math.tan(t)*r)-4,8,8); gBuffer.setColor(Color.black); if (x0-x1==0){gBuffer.drawString("tan \u03b8 = ------ = undefined", 235, 370);} else{ gBuffer.drawString("tan \u03b8 = ------ = "+ ((int)(10000*Math.tan(t))/10000.0), 235, 370);} gBuffer.setColor(Color.blue); gBuffer.drawString( ((y0-y1)/100.0)+" ", 290,360); gBuffer.setColor(Color.red); gBuffer.drawString( ((x1-x0)/100.0)+" ", 290,380); gBuffer.setColor(Color.black); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; gBuffer.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); gBuffer.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){ gBuffer.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)))); } gBuffer.setColor(Color.BLUE); gBuffer.fillOval(20+(int)(t*100.0/Math.PI)-4, y0-(int)(r/Math.sin(t))-4,8,8); gBuffer.setColor(Color.BLACK); if(y1-y0==0){gBuffer.drawString("csc \u03b8 = ------ = undefined", 235,370 );} else {gBuffer.drawString("csc \u03b8 = ------ = "+ (1.0/Math.sin(t)), 235, 370);} gBuffer.setColor(Color.blue); gBuffer.drawString( ((y0-y1)/100.0)+" ", 290,380); gBuffer.setColor(Color.black); gBuffer.drawString(" 1", 290,360); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; gBuffer.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); gBuffer.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){ gBuffer.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)))); } gBuffer.setColor(Color.BLUE); gBuffer.fillOval(20+(int)(t*100.0/Math.PI)-4, y0-(int)(r/Math.cos(t))-4,8,8); gBuffer.setColor(Color.BLACK);if(x0-x1==0) {gBuffer.drawString("sec \u03b8 = ------ = undefined", 235, 370);} else {gBuffer.drawString("sec \u03b8 = ------ = "+ (1.0/Math.cos(t)), 235, 370);} gBuffer.setColor(Color.red); gBuffer.drawString( ((x1-x0)/100.0)+" ", 290,380); gBuffer.setColor(Color.black); gBuffer.drawString(" 1", 290,360); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; gBuffer.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); gBuffer.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){ gBuffer.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)))); } gBuffer.setColor(Color.BLUE); gBuffer.fillOval(20+(int)(t*100.0/Math.PI)-4, y0-(int)(r/Math.tan(t))-4,8,8); gBuffer.setColor(Color.BLACK); if (y0-y1==0){gBuffer.drawString("cot \u03b8 = ------ = undefined", 235, 370);} else{ gBuffer.drawString("cot \u03b8 = ------ = "+ (int)(10000*(1/Math.tan(t)))/10000.0, 235, 370);} gBuffer.setColor(Color.blue); gBuffer.drawString( ((y0-y1)/100.0)+" ", 290,380); gBuffer.setColor(Color.red); gBuffer.drawString( ((x1-x0)/100.0)+" ", 290,360); gBuffer.setColor(Color.black); if(y1-y0 > 0) yOffset=10; else yOffset=-10; if(x1-x0 > 0) xOffset=10; else xOffset=-70; gBuffer.drawString("("+((x1-x0)/100.0)+","+((y0-y1)/100.0)+")", x1+xOffset,y1+yOffset); gBuffer.drawString(" 1", x0+((x1-x0)/2), y0+((y1-y0)/2)+yOffset); } break; //end of cot }//end of switch gBuffer.setColor(Color.red); gBuffer.drawLine (x1,y0,x0,y0); //Now we send the result to the screen g.drawImage(virtualMem,0,0,this); } // 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 }