//package tideCalendar; import java.io.*; import java.util.LinkedList; public class Grapher { /** * Stores the x and y locations of the sine wave in the given arrays. * The sizes of the given arrays will be returned by the function and * are dependent on the graph viewing space and the amplitude modification * @param offset how many pixels to move over before beginning to graph * @param param the Parameter of the line being drawn * @param ylocation the int[] that will store the locations * of all of the x coordinates when the function returns * @param xlocation the int[] that will store the heights of * all of the y coordinates when the function returns * @param size the size of the viewing space that the graphed wave will be * drawn in. * @param yoffset the [insert correct term] * @param xoffset the distance in pixels that the graph should be from the * "zero" line of the drawing surface * @param amplitude the amplitude modification. * @param pConvert a conversion factor between the Parameter * and the pixels. * @return how many of the array locations are valid data (this is used * in the case that an offset smaller than the start of the wave or beyond * the end of the wave is entered. * @author- Tim Bollman * @copyright- 2005 Earthguide **/ public static int graph(float offset, Parameter param, LinkedList ylocation, LinkedList xlocation, int size, int yoffset, int xoffset, double amplitude, double pConvert) { /* ******************************************************************* * xDistance stores the pixels neccessary to get to the next point * * startingHeight stores the initial height of the line * * endingHeight stores the ending height of the line * * slope stores the slope (rise/run) of the line. * * x, i, j are incrementers. * * sineWave is a variable to store the height of the sine wave that * * is described by the parameters * *********************************************************************/ int i; int length = param.getSize(); double xDistance = 0; for (i = 0; i < length; ++i) { xDistance += param.getXDistance(i); if (xDistance > offset) break; } int nextI = ((i + 1) < length) ? i + 1 : i; xDistance *= pConvert; double lastXDistance = xDistance; double x = xoffset + xDistance; double intermediateXDistance = xDistance + param.getXDistance(nextI)*pConvert; double step = 1; if (pConvert < 1) { step = pConvert; size = (int)Math.round((size / pConvert) + 0.5); } double deltaX = param.getXDistance(nextI)*pConvert; double startingHeight = param.getHeight(i); double endingHeight = param.getHeight(nextI); double slope = (endingHeight - startingHeight)/deltaX; int numPoints; double sine; xlocation.clear(); ylocation.clear(); for (numPoints = 0; numPoints < size; ++numPoints, x += step, xDistance += step) { if (xDistance >= intermediateXDistance) { ++i; if ((i + 1) >= length) break; nextI = ((i + 1) < length) ? i + 1 : i; deltaX = param.getXDistance(nextI)*pConvert; startingHeight = param.getHeight(i); endingHeight = param.getHeight(nextI); slope = (endingHeight - startingHeight)/deltaX; lastXDistance = intermediateXDistance; intermediateXDistance += deltaX; } //for the sin wave, want -PI/2 to PI/2 sine = (Math.sin(Math.PI*(xDistance - lastXDistance)/deltaX - Math.PI/2) + 1)/2; xlocation.add(new Integer((int)Math.round(x))); ylocation.add(new Integer((int)Math.round(-(startingHeight + sine*(endingHeight - startingHeight))*amplitude + yoffset))); } return numPoints; } public static Parameter fileReader(TideLine line) { Parameter param = new Parameter(0); try { DataInputStream input = new DataInputStream((param.getClass()).getResourceAsStream(line.getFile())); int count = input.readInt(); param = new Parameter(count); count=0; //param.setOffset(input.readFloat()); //float totalDistance = -param.getOffset(); float totalDistance = 0; while(input.available() != 0) { param.setXDistance(count, input.readFloat()); param.setHeight(count, input.readFloat()); totalDistance += param.getXDistance(count); count++; } param.setTotalDistance(totalDistance); input.close(); } catch (Exception e){System.out.println(e);} param.setLine(line); return param; } }