// // hyperbolaGraph.java // hyperbolaGraph // // Created by Chris Thiel on Tue Apr 22 2003. // added focii 23 mar 2004 // Copyright (c) 2003 Chris Thiel. // Free for Educational Use (with permission). All rights reserved. // A simple Java applet (500 x 300) // import java.awt.*; import java.awt.event.*; import java.applet.*; import java.lang.*; public class hyperbolaGraph extends Applet implements MouseMotionListener { static final String message = "Hello World!"; static final int S3_LOCATION =475; static final int S2_LOCATION =425; static final int S1_LOCATION =375; static final int SLIDER_LENGTH = 200; static final int SLIDER_TOP=90; int SLIDER_BOTTOM=SLIDER_TOP+SLIDER_LENGTH; private Font font = new Font("sansserif", Font.ITALIC + Font.BOLD, 12); int a,b,c,ax, ay, bx, by, cx, cy,j; double sx, sy, fociX, fociY; int [] side1x,side1y, side2x, side2y; public void init() { setLayout (null); this.setBackground(new Color(0xFFEEAA)); addMouseMotionListener(this); a=1; b=1; c=1; side1x = new int[25]; side1y = new int[25]; side2x = new int[25]; side2y = new int[25]; ax=S1_LOCATION; ay=SLIDER_TOP+99; bx=S2_LOCATION; by=SLIDER_TOP+99; cx=S3_LOCATION; cy=SLIDER_TOP+99; } private double f(double x){ return Math.sqrt(b*c-b*x*x/a); } private int screenX (double x){ return (int)(160+10*x+20); //converts -150 && b>0 && c>0){ sx=Math.sqrt(a*c); sy=Math.sqrt(b*c); //draw focii if(sx>sy){ fociX=Math.sqrt(a*c-b*c); fociY=0; } else { fociX=0; fociY=Math.sqrt(b*c-a*c); } g.setColor(Color.yellow); g.drawOval(screenX(-fociX)-2,screenY(-fociY)-2,2,2); g.drawOval(screenX(fociX)-2,screenY(fociY)-2,2,2); /*for(double i=-sx;i0 && b<0 ){ sx=Math.sqrt(a*c); sy=Math.sqrt(Math.abs(b)*c); //draw focii fociY=0; fociX=Math.sqrt(Math.abs(b)*c+a*c); g.setColor(Color.yellow); g.drawOval(screenX(-fociX)-2,screenY(-fociY)-2,2,2); g.drawOval(screenX(fociX)-2,screenY(fociY)-2,2,2); /*for(double i=-17.0;i<-sx-.4;i=i+.5) { g.drawLine(screenX(i) ,screenY(f(i)) ,screenX(i+.5),screenY(f(i+.5))); g.drawLine(screenX(i) ,screenY(-f(i)) ,screenX(i+.5),screenY(-f(i+.5))); } for(double i=sx;i<17.0;i=i+.5) { g.drawLine(screenX(i) ,screenY(f(i)) ,screenX(i+.5),screenY(f(i+.5))); g.drawLine(screenX(i) ,screenY(-f(i)) ,screenX(i+.5),screenY(-f(i+.5))); }*/ //asymptotes: g.setColor(new Color(0x054000)); g.drawRect(screenX(-sx),screenY(sy),(int)(20*sx),(int)(20*sy)); g.setColor(new Color(0x559933)); g.drawLine(screenX(-17.0),screenY(-17.0*sy/sx),screenX(17.0),screenY(17.0*sy/sx)); g.drawLine(screenX(-17.0),screenY(17.0*sy/sx),screenX(17.0),screenY(-17.0*sy/sx)); g.setColor(Color.cyan); j=0; for (double i = -17.0; i<=-sx; i=i+(Math.abs(17.0-sx)/12.0)){ side1x[j]=screenX(i); side1y[j]=screenY(f(i)); side1x[24-j]=side1x[j]; side1y[24-j]=screenY(-f(i)); side2x[j]=screenX(-i); side2y[j]=screenY(f(-i)); side2x[24-j]=side2x[j]; side2y[24-j]=screenY(-f(-i)); j++; } side1x[12]=screenX(-sx); side1y[12]=screenY(0); side2x[12]=screenX(sx); side2y[12]=screenY(0); if (sx<=17.0){ g.drawPolygon(side1x,side1y,25); g.drawPolygon(side2x,side2y,25); } } // if a neg b pos, then Up/Down hyperbola, sx=sqrt(a) if (a<0 && b>0 && c>0){ sx=Math.sqrt(Math.abs(a)*c); sy=Math.sqrt(b*c); //draw focii fociX=0; fociY=Math.sqrt(b*c+Math.abs(a)*c); g.setColor(Color.yellow); g.drawOval(screenX(-fociX)-2,screenY(-fociY)-2,2,2); g.drawOval(screenX(fociX)-2,screenY(fociY)-2,2,2); //asymptotes: g.setColor(new Color(0x054000)); g.drawRect(screenX(-sx),screenY(sy),(int)(20*sx),(int)(20*sy)); g.setColor(new Color(0x559933)); g.drawLine(screenX(-17.0),screenY(-17.0*sy/sx),screenX(17.0),screenY(17.0*sy/sx)); g.drawLine(screenX(-17.0),screenY(17.0*sy/sx),screenX(17.0),screenY(-17.0*sy/sx)); g.setColor(Color.cyan); for(double i=-17;i<=17;i=i+.5) { g.drawLine(screenX(i) ,screenY(f(i)) ,screenX(i+.5),screenY(f(i+.5))); g.drawLine(screenX(i) ,screenY(-f(i)) ,screenX(i+.5),screenY(-f(i+.5))); } } // Draw Controls g.setColor(Color.red); g.fillOval(ax-5,ay-5,10,10); g.setColor(Color.cyan); g.fillOval(bx-5,by-5,10,10); g.setColor(Color.magenta); g.fillOval(cx-5,cy-5,10,10); g.setColor(Color.black); g.setFont (new Font("serif", Font.ITALIC + Font.BOLD, 14)); g.drawString("x", 370, 30); g.drawString("y", 420, 30); g.setFont (new Font("serif", Font.BOLD, 12)); g.drawString("2", 378, 23); g.drawString("2", 428, 23); g.drawLine(360 , 33, 388 ,33); g.drawLine(410 , 33, 438 ,33); g.setFont (new Font("sansserif", Font.BOLD, 14)); g.drawString(Math.abs(a)+"", 365, 48); g.drawString(Math.abs(b)+"", 415, 48); g.drawString("= "+c, 450, 40); g.setFont (new Font("serif", Font.BOLD, 20)); if (a<0) g.drawLine(350, 33, 355, 33); if (b<0) g.drawLine(395,33,405, 33); if (b>0) g.drawString("+", 395, 40); g.setFont (new Font("serif", Font.ITALIC + Font.BOLD, 12)); g.drawString("a = "+a,S1_LOCATION-15, SLIDER_TOP-12); g.drawString("b = "+b,S2_LOCATION-15, SLIDER_TOP-12); g.drawString("c = "+c,S3_LOCATION-15, SLIDER_TOP-12); g.drawLine(S1_LOCATION,SLIDER_TOP,S1_LOCATION,SLIDER_BOTTOM); g.drawLine(S2_LOCATION,SLIDER_TOP,S2_LOCATION,SLIDER_BOTTOM); g.drawLine(S3_LOCATION,SLIDER_TOP,S3_LOCATION,SLIDER_BOTTOM); } public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { int mx=e.getX(); int my=e.getY(); // check if inside red or blue dot // put in approporiate //if (Math.abs(my-ry)<17 && Math.abs(mx-rx)<17){ if (my>SLIDER_TOP-17 && Math.abs(mx-S1_LOCATION)<17){ ax = S1_LOCATION; if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM; if (my=SLIDER_TOP-17 && Math.abs(mx-S2_LOCATION)<17){ bx = S2_LOCATION; if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM; if (my=SLIDER_TOP-17 && Math.abs(mx-S3_LOCATION)<17){ cx = S3_LOCATION; if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM; if (my