import java.awt.*; /* * Programmer: Richard Glenn * Modified by: Konstantin Lukin */ public class SineWave { public int wavelength, amplitude, phase; public SineWave(int w, int a, int p) { wavelength = w; amplitude = a; phase = p; } public void advance(int phaseIncrement) { // note: this advances the initial phase, so a positive value // moves the wave form to the left, negative to the right. phase -= phaseIncrement; if(phase >= 360) phase %= 360; if(phase < 0) phase += 360; } public SineWave copy() { return new SineWave(wavelength, amplitude, phase); } public int getY(int x) { double degreesPerPixel = 360.0 / wavelength; double degrees = (-phase+180) + x*degreesPerPixel; return (int)(amplitude*Math.sin(degrees*Math.PI/180.0)); } // draws a very nice looking sine curve if the wavelength is not // too small or amplitude too big. Good for presentations. public void drawThick(Graphics g, int x, int y, int width) { double degreesPerPixel = 360.0 / wavelength; int yOffset = (int)(amplitude/2.0)+2; double degrees; int xloc; int yloc; for (int i=0; i