import java.awt.*; /** Program: IterPoly Purpose: show orbits of iterations of quadratic polynomials @author: Paul Garrett, garrett@math.umn.edu @version: Tue Sep 23 14:19:01 CDT 1997 */ /* * Here we iterate x^2+c very quickly, about 100 times, drawing * the orbits. The parameter c is adjustable in real time, with the * curve and the orbits getting redrawn asap as the slider... * is moved. */ /* As of 10-08-97, there is still the problem of some platforms' confusion over events... somehow resetting the x-coord to -1/2, etc. Dunno... */ /******************************************************************/ public class IterPoly extends java.applet.Applet { // These are read in from the HTML in init() int height, width, background_color, foreground_color; double theConstant; // as in "x^2 + theConstant" int inset = 35; double xInset = 2.0; // how negative x double xRange = 4.0; // total range of x double yInset = 2.0; // how much negative y double yRange = 4.0; // total range of y int drawHeight, drawWidth; // drawing area int range; // want inc * range = drawWidth or so int inc = 5; // for drawing the curve Scrollbar sb; // with values 0-99 Label theDisplay; // to display constant value... int sbValue; // used to help filter events Panel p; int number_iterations; double initX; double theX; // starting value for 100 or so iterations /***************************/ public void init() { initX = 0.99d; try { height = Integer.parseInt(getParameter("height")); width = Integer.parseInt(getParameter("width")); background_color = Integer.parseInt(getParameter("background color"), 16); foreground_color = Integer.parseInt(getParameter("foreground color"), 16); number_iterations = Integer.parseInt(getParameter("number iterations")); } catch (Exception e) { height = 320; width = 550; background_color = 0x302030; foreground_color = 0xbbbbbb; number_iterations = 25; } drawHeight = height - 2*inset; drawWidth = width - 2*inset; range = drawWidth / inc; // The following _prior_ to all other Color-setting... setBackground(new Color(background_color)); setForeground(new Color(foreground_color)); setLayout(new BorderLayout()); p = new Panel(); add("North", p); p.setBackground(new Color(0x808080)); p.setForeground(new Color(0x000000)); sb = new Scrollbar(Scrollbar.VERTICAL, 170, 10, 0, 199); add("West", sb); sb.setBackground(new Color(0x808080)); sb.setForeground(new Color(0x000000)); sbValue = sb.getValue(); theConstant = sbToConstant(sbValue); theX = initX; theDisplay = new Label("y = x * x "+theConstant); p.add(theDisplay); theDisplay.setBackground(new Color(0x808080)); // just to be safe... theDisplay.setForeground(new Color(0x000000)); repaint(); } final double theFunction(double x) { return (double) (theConstant + x * x); } void drawAxes(Graphics g, Color c) { // draws x-axis and y-axis g.setColor(c); g.fillRect(inset, yToPix(0d), drawWidth, 2); // x-axis // arrowhead on x-axis int[] xs = {inset + drawWidth + 2, inset + drawWidth - 10, inset + drawWidth - 6, inset + drawWidth - 10}; int[] ys = {yToPix(0d), yToPix(0d)-6, yToPix(0d), yToPix(0d)+6}; g.fillPolygon(new Polygon(xs, ys, 4)); // arrowhead on y-axis g.fillRect(xToPix(0d)-1, inset, 2, drawHeight); // y-axis int[] xs2 = {xToPix(0d), xToPix(0d) - 6, xToPix(0d), xToPix(0d) + 6}; int[] ys2 = {inset - 2, inset + 10, inset + 6, inset + 10}; g.fillPolygon(new Polygon(xs2, ys2, 4)); } final int xToPix (double x) { // rescales x-values to horizontal on screen double w = (double) drawWidth; return inset + (int) ( (x + xInset) * w / xRange); } final int yToPix (double y) { // rescales y-values to vertical on screen return inset + (drawHeight - ((int) ( (y + yInset) * ((double) drawHeight) / yRange))); } final double pixToX(int i) { // inverse of xToPix return ((double) i - inset) * xRange / ((double) drawWidth) - xInset; } // public void update(Graphics g) { // not appropriate // paint(g); // } double tTox (int t) { // rescales [0,range] to [-xInset, xRange-xInset] return ((double) (t * inc)) * xRange / ((double) drawWidth) - xInset; } final synchronized double sbToConstant(int value) { return -2d + ((double) value)/ 100d; } void drawCurveAndDiagonal(Graphics g, Color c) { g.setColor(c); for (int i=0; i<= range; i++) { DrawLine(g, tTox(i), theFunction(tTox(i)), tTox(i+1), theFunction(tTox(i+1))); } g.drawLine(xToPix(-xInset), yToPix(-xInset), xToPix(xRange-xInset), yToPix(xRange-xInset)); // the diagonal } final synchronized void DrawLine(Graphics g, double x1, double y1, double x2, double y2) { g.drawLine(xToPix(x1), yToPix(y1), xToPix(x2), yToPix(y2)); } public final synchronized void paint (Graphics g) { // g.clipRect(inset,inset,drawWidth,drawHeight); // not appropriate drawAxes(g, new Color(foreground_color)); drawCurveAndDiagonal(g, Color.red); // setBackground(new Color(background_color)); //__EDIT__: delete this g.setColor(Color.yellow); double theY = 0; double nextY = theFunction(theX); double nextX = nextY; for (int i=0; i=inset && i<= inset+drawWidth && Math.abs((height-j)-(drawHeight - yToPix(0d)+inset)) <= 40) { initX = pixToX(i); theX = initX; repaint(); return true; } else { return false; } } }// The End