// ------------------------------------ // plot.java // ----------------------------------- // Computniks have the y axis pointing down! import java.awt.Graphics; public class plot extends java.applet.Applet { double f(double x) { return (Math.exp(Math.cos(x))); } public void paint(Graphics g) { String label = "Graph of f(x) = exp(cos(x)) on [0, 10Pi] ^ [0, 3]"; int nx = size().width; // width int nyt = size().height; // total height int ny = (int)(nyt*0.75); // 25% for bottom label g.drawString(label,10, nyt-30); // margins 10 30 // scale for the region double x0 = 0; double x1 = 31.42; // 10 Pi double y0 = 0; double y1 = 3; // put axes g.drawLine(0,ny-1,nx,ny-1); g.drawLine(0,ny,0,0); // plot graph double xa = 0.0; double xb = 0.0; double ya = 0.0; double yb = 0.0; int yai = 0; int ybi = 0; for (int i = 0 ; i < nx ; i++) { xa = ((x1-x0)*i)/nx + x0; ya = f(xa); yai = (ny - (int)((ya-y0)*ny/(y1-y0))); // (!) xb = ((x1-x0)*(i+1))/nx + x0; yb = f(xb); ybi = (ny - (int)((yb-y0)*ny/(y1-y0))); // (!) g.drawLine(i, yai, i + 1, ybi); } } }