import java.awt.*; /** Program: Quintic Purpose: graph quintic polynomials @author: Paul Garrett, garrett@math.umn.edu @version: 39, Mon 7 July 17:06:25 CDT 1997 */ public class Quintic extends java.applet.Applet { final int nwx = 10; // nw corner of drawing surface x-coord final int nwy = 10; // nw corner of drawing surface x-coord final int height = 290; final int width = 400; final int gridsize = 20; final int inc = 5; // space between grid lines final int rangeOfIndex = (int) width/(2*gridsize); final int rangeOfJndex = (int) height/(2*gridsize); final int rangeOfInc = (int) width/(2*inc); final int tall = 15; // height of click-boxes for adjust coeffs final int wd = 12; // width of click-boxes for adjust coeffs int[][] theClickBoxes; // coords of centers of coef click-boxes double[] theCoefficients; // of arbitrary degree polynomial final int degree = 5; // to know when we're done final double coefInc = 0.2; // amount to change by for one mouse click Graphics offScreenGraphics; // to draw to... Image offScreenImage; // ditto final boolean restrictAccess = false; // 'false' for development, 'true' to install String theURL = "http://www.math.umn.edu/~garrett/qy/Quintic.html"; /******************************/ double theFunction(double input) { // the raw function double output; output = theCoefficients[degree]; for (int i = degree-1; i >= 0; i--) { output = output * input + theCoefficients[i]; } return output; } int normPara(int para) { // to function input int thePara; thePara = nwx + width/2 + inc * para; return thePara; } int normOut(double output) { // with y from -32 to +32... int theY; theY = nwy + height/2 - (int) (output * ((double) height/2) / 9.0); // 32? return theY; } double normIn(int in) { // normalize inc parameter for function input double theX; theX = 2.0 * ((double) in)/((double) rangeOfInc); return theX; } void initCoefs() { theCoefficients = new double[degree+1]; theCoefficients[5] = 2.0; theCoefficients[4] = 0.0; theCoefficients[3] = -9.8; theCoefficients[2] = 0.0; theCoefficients[1] = 7.6; theCoefficients[0] = 0.0; } public void init() { // stick some coefficients in, tell degree initCoefs(); theClickBoxes = new int[degree+1][2]; for (int i=0; i<= degree; i++) { theClickBoxes[i][0] = 530; theClickBoxes[i][1] = 45 + (tall+25)*i; } offScreenImage = createImage(550, 320); offScreenGraphics = offScreenImage.getGraphics(); repaint(); } void drawGrid (Graphics g) { for (int i = -rangeOfIndex; i <= rangeOfIndex; i++) { // vertical lines g.drawLine(nwx + width/2 + gridsize * i, nwy, nwx + width/2 + gridsize * i, nwy + height); } for (int i = -rangeOfJndex; i <= rangeOfJndex; i++) { // horizontal lines g.drawLine(nwx, nwy + height/2 + gridsize * i, nwx + width, nwy + height/2 + gridsize * i); } g.fillRect(nwx + width/2 - 1, nwy, 3, height); // y-axis g.fillRect(nwx, nwy + height/2 - 1, width, 3); // x-axis // arrow on y-axis int[] xs = {nwx + width/2, nwx + width/2 - 6, nwx + width/2, nwx + width/2 + 6}; int[] ys = {nwy-2, nwy + 10, nwy + 6, nwy + 10}; g.fillPolygon(new Polygon(xs, ys, 4)); // arrow on x-axis int[] xs2 = {width + nwx + 2, width+ nwx - 10, width + nwx - 6, width + nwx - 10}; int[] ys2 = {nwy + height/2, nwy + height/2 - 6, nwy + height/2, nwy + height/2 + 6}; g.fillPolygon(new Polygon(xs2, ys2, 4)); } void writePowersOfx (Graphics g) { // x,y beginning spot for (int i =0; i <= degree; i++) { int x = theClickBoxes[i][0]; int y = theClickBoxes[i][1]; g.drawString(""+theCoefficients[i], x-100, y+5); g.drawString("x",x-25,y+5); g.drawString(""+i,x-18,y); } } void drawUpDown (Graphics g) { // coords... // SLIDERS_???? int ht; // the "tall" and "wd" are now instance vars ht = 16; for (int i = 0; i <= degree; i++) { int x = theClickBoxes[i][0]; int y = theClickBoxes[i][1]; g.drawLine(x,y-tall,x-wd/2,y-tall+ht); // top point of uparrow to left g.drawLine(x-wd/2,y-tall+ht,x+wd/2,y-tall+ht); // left side of uparrow g.drawLine(x,y-tall,x+wd/2,y-tall+ht); // close uparrow g.drawLine(x,y+tall,x-wd/2,y+tall-ht); // top point of downarrow to left g.drawLine(x-wd/2,y+tall-ht,x+wd/2,y+tall-ht); // left side of downarrow g.drawLine(x,y+tall,x+wd/2,y+tall-ht); // close downarrow } } public void paint(Graphics g) { drawOffScreen(); g.drawImage(offScreenImage,0,0,this); } public void update(Graphics g) { paint(g); } void drawOffScreen() { offScreenGraphics.setColor(Color.gray); offScreenGraphics.fillRect(0,0,550,320); offScreenGraphics.setColor(Color.black); drawGrid(offScreenGraphics); offScreenGraphics.setColor(Color.white); for (int i = -rangeOfInc ; i < rangeOfInc; i++) { offScreenGraphics.drawLine( normPara(i), normOut(theFunction(normIn(i))), normPara(i+1), normOut(theFunction(normIn(i+1)))); } offScreenGraphics.setColor(Color.black); offScreenGraphics.drawString("Coefficients",420,20); writePowersOfx(offScreenGraphics); drawUpDown(offScreenGraphics); offScreenGraphics.drawString("Equation y = ("+theCoefficients[5]+")x^5 + "+ "("+theCoefficients[4]+")x^4 + "+ "("+theCoefficients[3]+")x^3 + "+ "("+theCoefficients[2]+")x^2 + "+ "("+theCoefficients[1]+")x^1 + "+ "("+theCoefficients[0]+")",10,317); offScreenGraphics.drawRect(420,280,70,20); offScreenGraphics.drawString("Set all 0",425,297); offScreenGraphics.drawRect(420,260,70,20); offScreenGraphics.drawString("Reset",440,277); } public final boolean mouseDown(Event e, int x, int y) { test: { for (int i=0; i <= degree; i++) { if ((x <= theClickBoxes[i][0]+wd/2) && (x >= theClickBoxes[i][0] - wd/2)) { if ((y >= theClickBoxes[i][1]) && (y <= theClickBoxes[i][1]+tall)) { theCoefficients[i] = 0.1 * ((int) (10.0 * (theCoefficients[i] - coefInc))); repaint(); break test; } else if ((y <= theClickBoxes[i][1]) && (y >= theClickBoxes[i][1]-tall)) { theCoefficients[i] = 0.1 * ((int) (10.0 * (theCoefficients[i] + coefInc))); repaint(); break test; } } } if ((x <= 490) && (x >= 420)) { if ((y >= 280) && ( y <= 300)) { for (int i=0; i <= degree; i++) { theCoefficients[i] = 0.0; } repaint(); } else if ((y >= 260) && (y <= 280) && (x <= 490) && (x >= 420)) { for (int i=0; i <= degree; i++) { initCoefs(); } repaint(); } } } return true; } } // The End