Graph inputted values
Hey everyone,
I'm writing a program that takes the derivative of an equation and outputs the derivative and shows a graph with the original equation and the derivative. How would I go about making a graph show up in the panel, and then draw the lines. Im confused at this part. I can get the input working, but how do I make a graph and lines and such.
[366 byte] By [
bcleva] at [2007-11-27 6:46:56]

Using
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class Graph2D extends Applet{
public void init(){
this.setBackground(Color.LIGHT_GRAY);
}
public void paint(Graphics g){
Graphics2D gfx = (Graphics2D)g;
gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gfx.setColor(Color.BLACK);
Xscale = (double)this.getWidth() / (Xend - Xstart);
Yscale = (double)this.getHeight() / (Yend - Ystart);
double iterScale = (Xend - Xstart) / (double)this.getWidth();
double fx;
convertToScreen(start, Xstart, function(Xstart));
for(double x = Xstart; x <= Xend; x += iterScale){
fx = function(x);
convertToScreen(end, x, fx);
line.setLine(start, end);
gfx.draw(line);
start.setLocation(end.getX(), end.getY());
}
}
public double function(double x){
return Math.sin(x);
}
public void convertToScreen(Point2D pt, double x, double y){
double px = ((x - Xstart) * Xscale);
double py = this.getHeight() - (((y - Ystart) * Yscale));
pt.setLocation(px, py);
}
double Xstart = -30;
double Xend = 30;
double Xscale;
double Ystart = -2;
double Yend = 2;
double Yscale;
Point2D start = new Point2D.Double();
Point2D end = new Point2D.Double();
Line2D.Double line = new Line2D.Double();
}
How would I graph x^2 and such? I tried replacing Math.sin(x) with x*x, but it doesnt work.
bcleva at 2007-7-12 18:19:23 >
