Fourier

グラフを書いてみようのプログラムです。

幅と高さを、縮小・拡大するようにしています。

なんかのたたき台に?なるかもで載せておきます。

/* Fourier.java -- a graph drawing program
 *
 * Copyright (C) 2009 Erica Asai

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Usage: java CountDown
 * 
*/

import javax.swing.*;
import java.awt.*;

public class Fourier extends JPanel
{
    double start=-Math.PI*3.0;
    double end=Math.PI*3.0;

    public void paint(Graphics g)
    {
	int w=getWidth();
	int h=getHeight();
	int yOffset=h/20;
  	h-=yOffset*2;

	double min=0;
	double max=0;
	double value=0;
	for(int i=0;i<w;i++)
	    {
		value=f(i);
		if(i==0)
		    {
			min=value;
			max=value;
		    }
		if(value<min)
		    min=value;
		if(max<value)
		    max=value;
	    }
	double scaleX=(double)w/(end-start);
 	double step=(end-start)/w;
	double scaleY=(double)h/(max-min);

	Point p0=new Point(-1,-1);
	Point p1=new Point(-1,-1);

	int y0=(int)(-min*scaleY)+yOffset;	

	for(double i=start;i<end;i+=step)
	    {
		p1.x=(int)((i-start)*scaleX);
		p1.y=(int)(-f(i)*scaleY+y0);

		if(0<p0.x)
		    {
			g.drawLine(p0.x,p0.y,p1.x,p1.y);
		    }
		p0.x=p1.x;
		p0.y=p1.y;
	    }	
    }

    public double f(double x)
    {
	double value=Math.sin(x);
	for(int i=3;i<700;i+=2)
	    value+=Math.sin(i*x)/i;
	value*=4.0/Math.PI;
	return value;
    }

}

/* Fourier.java -- a graph drawing program
 *
 * Copyright (C) 2009 Erica Asai

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
*/

import javax.swing.*;
import java.awt.*;

public class FourierFrame extends JFrame
{
    Fourier f=new Fourier();

    FourierFrame()
    {
	getContentPane().add(f);
	setTitle("Fourier");
	setSize(1000,800);
	setVisible(true);
    }
    public static void main(String  args[])
    {
	FourierFrame frame=new FourierFrame();
    }

}

inserted by FC2 system