/** * stretches sim coordinates to screen coordinates * @author Michael Schuresko * @version %I%, %G% * @since 1.0 */ public class ProjectionSimpleStretch implements IProjection { int [] m_arrNScreenMin; int [] m_arrNScreenMax; double [] m_arrLfSimMin; double [] m_arrLfSimMax; double [] m_arrLfRatios; double m_lfMeanRatio; IBoundary m_bound; /** * ensures that the environment env gets displayed on * a panel of nWidth by nHeight */ public ProjectionSimpleStretch(int nWidth, int nHeight, IEnvironment env) { set(nWidth, nHeight, env.getBoundary()); } public void resize(int nNewWidth, int nNewHeight) { set(nNewWidth, nNewHeight, m_bound); } /** * ensures that the boundary bound gets displayed on * a panel of nWidth by nHeight */ public void set(int nWidth, int nHeight, IBoundary bound) { m_arrNScreenMin = new int[2]; m_arrNScreenMin[0] = 0; m_arrNScreenMin[1] = 0; m_arrNScreenMax = new int[2]; m_arrNScreenMax[0] = nWidth; m_arrNScreenMax[1] = nHeight; m_bound = bound; m_arrLfSimMin = bound.bboxMinCorner(); m_arrLfSimMax = bound.bboxMaxCorner(); // System.out.println(m_arrLfSimMin.length); setRatios(); } void setRatios() { m_lfMeanRatio = 1.0; m_arrLfRatios = new double[2]; for(int i = 0; i < 2; ++i) { m_arrLfRatios[i] = (m_arrNScreenMax[i] -m_arrNScreenMin[i]) /(m_arrLfSimMax[i]-m_arrLfSimMin[i]); m_lfMeanRatio *= m_arrLfRatios[i]; } m_lfMeanRatio = Math.sqrt(m_lfMeanRatio); } /** * Finds the ith component in screen coordinates of a vector * indicated by the portion of the array arrLfState starting * at nIdxOff; * @param i which component of screen coordinates to get (0=x, 1=y) * @param arrLfState global state vector in sim coordinates * @param nIdxOff offset index in arrLfState of the coord we're looking * for * @return ith component in screen coordinates */ public int getScreenComp(int i, double [] arrLfState, int nIdxOff) { double lfUncastRetval = ((arrLfState[nIdxOff+i]-m_arrLfSimMin[i])* m_arrLfRatios[i]); int nRetVal = (int) lfUncastRetval; nRetVal += m_arrNScreenMin[i]; return nRetVal; } /** * Scales (possibly including rotation and flip) * a vector, without applying global translation * @param arrLfVec array containing vector to scale (transform) * @param nIdxOff offset of begining of vector to scale in * arrLfVec * @return integer array containing scaled vector discretized * to pixel differences. */ public int [] scaleVector(double [] arrLfVec, int nIdxOff) { int [] arrResult = new int[2]; arrResult[0] = (int)(arrLfVec[nIdxOff ]*m_arrLfRatios[0]); arrResult[1] = (int)(arrLfVec[nIdxOff+1]*m_arrLfRatios[1]); return arrResult; } public int width() { return m_arrNScreenMax[0]-m_arrNScreenMin[0]; } public int height() { return m_arrNScreenMax[1]-m_arrNScreenMin[1]; } public int top() { return m_arrNScreenMin[1]; } public int left() { return m_arrNScreenMin[0]; } /** * Simple scale function for 2d circles etc. */ public int scaleRadius(double lfRad) throws Exception { return (int)(m_lfMeanRatio*lfRad); } public void scaleProjection(int nNewX, int nNewY) { m_arrNScreenMax[0] = m_arrNScreenMin[0]+nNewX; m_arrNScreenMax[1] = m_arrNScreenMin[1]+nNewY; setRatios(); } public void xlateProjection(int nXOff, int nYOff) { m_arrNScreenMax[0] += nXOff; m_arrNScreenMin[0] += nXOff; m_arrNScreenMax[1] += nYOff; m_arrNScreenMin[1] += nYOff; } }