/*
 * Copyright (c) 2005, Jean-Michel MORANI
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met :
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer. 
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or 
 *   other materials provided with the distribution. 
 * - Neither the name of the Nayxx nor the names of its contributors may be used to
 *   endorse or promote products derived from this software without specific prior
 *   written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

import java.applet.*;
import java.awt.*;


public class fournyx extends Applet {

    static int X_TEMP = 28;
    static int Y_TEMP = 120;
    static int M_TEMP = 10;
    static int H_TEMP = 100;
    static int L_TEMP = 600;
    static int X_FOUR = 450;
    static int Y_FOUR = 370;
    static int L_FOUR = 130;
    static int H_FOUR = 100;
    
    int freq = 440;
    double a = 1;
    double b = 0.6;
    double c = 0.5;
    
    public void init() {
      String s;
      setBackground(Color.white);
      s = getParameter("freq");
      if (s != null) freq = Integer.parseInt(s);
      s = getParameter("a");
      if (s != null) a = (Integer.parseInt(s)) / 100.0;
      s = getParameter("b");
      if (s != null) b = (Integer.parseInt(s)) / 100.0;
      s = getParameter("c");
      if (s != null) c = (Integer.parseInt(s)) / 100.0;
    }

    private double periode() {
        return (1000 / (double)freq); 
    }
    
    private void dessineAxes(Graphics g) {
                
        // Temporel
        g.setColor(Color.yellow);
        for(int i = -3; i <= 3; i++) {            
            int y = Y_TEMP - (2*i * H_TEMP) / 7;
            g.drawLine(X_TEMP+1, y, X_TEMP+L_TEMP, y);
        }
        g.setColor(Color.black);
        g.drawLine(X_TEMP, Y_TEMP - H_TEMP, X_TEMP, Y_TEMP + H_TEMP);
        g.drawLine(X_TEMP, Y_TEMP, X_TEMP+L_TEMP+6, Y_TEMP);
        g.setColor(Color.blue);
        for(int i = -6; i <= 6; i++) {            
            int y = Y_TEMP - (i * H_TEMP) / 7;
            g.drawLine(X_TEMP-4, y, X_TEMP, y);
            if (i<0) {
                g.drawString(""+((double)i)/2, 0, y+5);
            } else {
                g.drawString(""+((double)i)/2, 4, y+5);
            }        
        }
        g.drawString("> t (ms)", X_TEMP+L_TEMP+2, Y_TEMP+5);
        g.drawString("Domaine temporel : S(t)", X_TEMP+5, Y_TEMP-H_TEMP-3);
        g.setColor(Color.green);
        g.drawLine(X_TEMP, Y_TEMP+H_TEMP, X_TEMP+L_TEMP/3, Y_TEMP+H_TEMP);
        g.drawLine(X_TEMP, Y_TEMP+H_TEMP, X_TEMP, Y_TEMP+H_TEMP-4);
        g.drawLine(X_TEMP+L_TEMP/3, Y_TEMP+H_TEMP, X_TEMP+L_TEMP/3, Y_TEMP+H_TEMP-4);
        g.drawString("Période : "+periode()+" ms", X_TEMP+7, Y_TEMP+H_TEMP+12);
    
        // Fréquenciel
        g.setColor(Color.black);
        g.drawLine(X_FOUR-L_FOUR, Y_FOUR, X_FOUR+L_FOUR, Y_FOUR);
        g.drawLine(X_FOUR, Y_FOUR-H_FOUR, X_FOUR, Y_FOUR);
        
        g.setColor(Color.blue);
        for(int i = -3; i <= 3; i++) {            
            int x = X_FOUR + (i * L_FOUR) / 4;
            g.drawLine(x, Y_FOUR, x, Y_FOUR+5);
            g.drawString(""+freq*i, x-3, Y_FOUR+18);
        }
        g.drawString("Domaine de Fourrier", X_FOUR-50, Y_FOUR-H_FOUR-15);
        g.drawString("> f (Hz)", X_FOUR+L_FOUR-4, Y_FOUR+5);
    }
                
    private double signal(double x) {
        return -(a*Math.sin(x)+b*Math.sin(2*x)+c*Math.sin(3*x));
    }
    
    private int f(int x) {
        return (int) (H_TEMP/3.5*signal( ((double)x) / (L_TEMP/(3*2*Math.PI))));
    }
    
    public void dessineTemp(Graphics g) {
        g.setColor(Color.red);
        for(int x = 0; x < L_TEMP; x++) {
            g.drawLine(X_TEMP+x, Y_TEMP+f(x), X_TEMP+x+1, Y_TEMP+f(x+1));
        }
    }

    private double fourrier(int f) {
        if (Math.abs(f)==1) { return a; }
        if (Math.abs(f)==2) { return b; }
        if (Math.abs(f)==3) { return c; }
        return 0;
    }

    private int four(int f) {
        return (int)(fourrier(f) * H_FOUR);       
    }
    
    public void dessineFour(Graphics g) {
        g.setColor(Color.red);
        for(int i = -3; i <= 3; i++) {            
            int x = X_FOUR + (i * L_FOUR) / 4;
            g.drawLine(x, Y_FOUR, x, Y_FOUR-four(i));
            g.drawString(""+fourrier(i), x+3, Y_FOUR-four(i));
        }
    }
    
    public void dessineResume(Graphics g) {
        g.setColor(Color.red);
        g.drawString(" X0 = "+a, X_TEMP+70, Y_TEMP+H_TEMP+50);
        g.drawString(" X1 = "+b, X_TEMP+70, Y_TEMP+H_TEMP+70);
        g.drawString(" X2 = "+c, X_TEMP+70, Y_TEMP+H_TEMP+90);
    }
    
    public void paint(Graphics g) {
        super.paint(g);
        dessineAxes(g);
        dessineTemp(g);
        dessineFour(g);
        dessineResume(g);
        g.setColor(Color.lightGray);
        g.drawString("Illustration du domaine de Fourrier - Jean-Michel MORANI pour "
                     +"Noushin - http://nayxx.com/~jmichel/fournyx", 35, Y_FOUR+35);
    }
}
e(g);
        g.setColor(Color.lightGray);
        g.drawString("Illustration du domaine de Fourrier - Jean-Michel MORANI pour "
                     +"Noushin - http://nayxx.com/~jmichel/fournyx", 35, Y_FOUR+35);
    }
}
