import jlab.graphics.DWindow;
public class LineGraph {
public static void main(String[] args) {
int n = 200;
double[] x = new double[n];
double[] y = new double[n];
for (int i = 0; i < x.length; i++) {
x[i] = -10 + i * 0.1;
for (int k = 1; k <= 4; k++) {
y[i] += Math.sin(k * x[i]);
}
}
plot(300, 200, x, y);
}
public static void plot(int width, int height,
double[] x, double[] y) {
DWindow w = new DWindow(width, height);
double xmin = min(x);
double xmax = max(x);
double ymin = min(y);
double ymax = max(y);
for (int i = 0; i < x.length - 1; i++) {
double sx0 = toScreen(x[i], xmin, xmax, width);
double sx1 = toScreen(x[i+1], xmin, xmax, width);
double sy0 = toScreen(y[i], ymax, ymin, height);
double sy1 = toScreen(y[i+1], ymax, ymin, height);
w.drawLine(w.BLACK, sx0, sy0, sx1, sy1);
}
}
private static double toScreen(double v, double min,
double max, double length) {
return (v - min) * (length-1) / (max - min);
}
public static double max(double[] d) { ... } // รหัสที่ 7-8
public static double min(double[] d) { ... } // ลองเขียนเอง
}
|