//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 * *********************************************************************/ double xDistance=0.0, startingHeight=0.0; double endingHeight=0.0, slope = 0.0, sineWave = 0.0; double paramOffset = param.getOffset(); int i = 0, x = 0; double j = 0.0, k = 0.0, step = 1.0; /*System.err.println("Grapher Debug:"); System.err.println("Offset- " + offset); System.err.println("size- " + size); System.err.println("yoffset- " + yoffset); System.err.println("xoffset- " + xoffset); System.err.println("amplitude- " + amplitude);*/ /* Start Invalid input check 1 */ if ((offset+paramOffset)*pConvert < -size) return 0; //nothing to graph /* If offset is less than zero, graph the wave as if the user had * asked to draw the wave from the "zero" offset to the end of the * viewing screen.*/ if (offset < 0) { offset = -offset; return graph(0, param, ylocation, xlocation, size-(int)offset, yoffset, xoffset+(int)offset, amplitude, pConvert); } if (offset > param.getTotalDistance()) { return 0; } /* END invalid input check 1 */ //move the index until the line has moved onto the screen do { xDistance += param.getXDistance(i++); }while (xDistance < offset + paramOffset); //then back up one parameter so that you are just before the screen xDistance-= param.getXDistance(--i); j = pConvert*(offset + paramOffset - xDistance); //set the starting values for xDistance etc. xDistance = pConvert*param.getXDistance(i); startingHeight = pConvert*param.getStartingHeight(i); endingHeight = pConvert*param.getEndingHeight(i); slope = (endingHeight - startingHeight) / xDistance; if (pConvert < 1) { size = (int)Math.round((size / pConvert) + 0.5); step = pConvert; } /* ensure that the x & ylocation arrays store the correct * number of elements. */ if (size < ylocation.size()) { ylocation.clear(); xlocation.clear(); } for(x=0,k=xoffset;x < size; x++, k+=step, j+=step) { if (j >= xDistance) {/* you have found the start of the next section of the line so * update the parameters to match the new line*/ if ((i+1) >= param.getSize()) break; j = j - xDistance; xDistance = pConvert*param.getXDistance(++i); startingHeight = pConvert*param.getStartingHeight(i); endingHeight = pConvert*param.getEndingHeight(i); slope = (endingHeight - startingHeight) / xDistance; } //find the height of the sine wave at that point sineWave = (xDistance/20) * Math.sin(2*Math.PI*j/xDistance); if (slope > 0) //reverse the sinewave to make the curve smoother sineWave = -sineWave; sineWave = slope*j + startingHeight + sineWave; if (x < xlocation.size()) { xlocation.set(x, new Integer((int)Math.round(k))); ylocation.set(x, new Integer((yoffset - (int)Math.round(amplitude * sineWave/pConvert)))); } else { xlocation.add(new Integer((int)Math.round(k))); ylocation.add(new Integer((yoffset - (int)Math.round(amplitude * sineWave/pConvert)))); } } return x; } 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(); while(input.available() != 0) { param.setXDistance(count, input.readFloat()); param.setStartingHeight(count, input.readFloat()); param.setEndingHeight(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; } }