class Fonction { // paramètres du graphiques // static final double xMin = -7; static final double xMax = 7; static final double xTic = 1; static final double yMin = -1.2; static final double yMax = 1.2; static final double yTic = 0.2; static double f(double x) { return Math.sin(x * x); } // conversion coordonnées réelles -> coordonnées fenêtre static int rtowX(final DrawingWindow w, double rx) { return (int)Math.round((w.width - 1) * (rx - xMin) / (xMax - xMin)); } static int rtowY(final DrawingWindow w, double ry) { return (int)Math.round((w.height - 1) * (yMax - ry) / (yMax - yMin)); } // conversion coordonnées fenêtre -> coordonnées réelles static double wtorX(final DrawingWindow w, int wx) { return (xMax - xMin) * wx / (w.width - 1) + xMin; } static double wtorY(final DrawingWindow w, int wy) { return -(yMax - yMin) * wy / (w.height - 1) + yMax; } // dessine les axes static void drawAxes(DrawingWindow w) { int x0 = rtowX(w, 0); int y0 = rtowY(w, 0); w.drawLine(0, y0, w.width - 1, y0); w.drawLine(x0, 0, x0, w.height - 1); for (double x = -xTic; x >= xMin; x -= xTic) { int xi = rtowX(w, x); w.drawLine(xi, y0 - 2, xi, y0 + 2); } for (double x = xTic; x <= xMax; x += xTic) { int xi = rtowX(w, x); w.drawLine(xi, y0 - 2, xi, y0 + 2); } for (double y = -yTic; y >= yMin; y -= yTic) { int yi = rtowY(w, y); w.drawLine(x0 - 2, yi, x0 + 2, yi); } for (double y = yTic; y <= yMax; y += yTic) { int yi = rtowY(w, y); w.drawLine(x0 - 2, yi, x0 + 2, yi); } } public static void main(String[] args) { DrawingWindow w = new DrawingWindow("Fonction", 640, 480); drawAxes(w); w.setColor("red"); int xiPrev = 0; int yiPrev = rtowY(w, f(wtorX(w, 0))); for (int xi = 1; xi < w.width; xi++) { int yi = rtowY(w, f(wtorX(w, xi))); // deux points consécutifs sont reliés par un trait pour faire // plus joli w.drawLine(xiPrev, yiPrev, xi, yi); xiPrev = xi; yiPrev = yi; } } }