//--file: CIUSuperEllipse.java------------------------------------- // // An IU SuperEllipse // package starwave.app.poll.imageutils; import java.math.*; /** * An IU SuperEllipse primitive. The super ellipse has a * bounding box (m_w and m_h), B/A values, a "buldge" value, * and a color. Values for the first quadrant of the ellipse are * calculated and then "mirrored" (copied) around to the other * quadrants. * */ public class CIUSuperEllipse extends CIUPrimitive { /** The color of the super ellipse */ protected byte m_color; /** The extra storage necessary to create the primitive */ protected CIUBitArray m_data = null; /** The height/width values of the ellipse */ protected int m_A, m_B; /** The "bulge" of the superellipse */ protected double m_n; /** ************************************************************* * * Construct a superellipse represented internally as a 1-D * array of bits. Pixel values are calculated for 1 quadrant * and then this quadrant is mirrored to the other 3. * * Note: The method chosen is the least computationally * intensive at the expense of storing the bits for all * quadrants in memory. * * @param A The height/2 * @param B The width/2 * @param n The "bulge" of the ellipse * @param c the super ellipses color * */ public CIUSuperEllipse( int B, int A, double n, byte color ) { super( B*2, A*2 ); m_data = new CIUBitArray( m_w, m_h ); m_color = color; // Set the height/width/bulge m_A = A; m_B = B; m_n = ( (n>0) ? (n) : (1.0) ); // Calculate Quadrant I and mirror this to the other 3 calculatePixels(); return; } /** ************************************************************* * * Construct a superellipse of the given color, which must be * mapped to an index in the LUT. * * @param A The height/2 * @param B The width/2 * @param n The "bulge" of the ellipse * @param c The super ellipses color as a CIUColor * @param cc The color cube of LUT colors * */ public CIUSuperEllipse( int B, int A, double n, CIUColor c, CIUNetscapeColorCube cc ) { this( B, A, n, (byte)(cc.toLUTIndex(c)) ); } /** ************************************************************* * * Construct a superellipse of the given color, given as a * string in HEX chars which must be mapped to an index in the LUT. * * @param A The height/2 * @param B The width/2 * @param n The "bulge" of the ellipse * @param c The super ellipses color as a HEX string * @param cc The color cube of LUT colors * */ public CIUSuperEllipse( int B, int A, double n, String c, CIUNetscapeColorCube cc ) { this( B, A, n, (byte)(cc.toLUTIndex(c)) ); } /** ************************************************************ * * calculate the pixels of the superellipse after having * set the necessary parameters. then mirror these values * to the other quardants. * */ private void calculatePixels() { double dP = 2.0/m_n; double dA = (double)m_A; double dB = (double)m_B; // Calculate quadrant 1 and mirror it to the other 3. for ( int y=0; y quadrant mirror exception. " ); System.err.println("CIUSuperEllipse::calculatePixels> " + e ); } } /** ************************************************************ * * pixelOn: Indicate if the pixel is on at position x,y * * @param x The x position of the pixel of interest. * @param y The y position of the pixel of interest. * @returns boolean Indicates if pixel is "on" or "off" * * @throws Exception on array-out-of-bounds pixel request * @exception Exception on array-out-of-bounds pixel request * */ public boolean pixelOn( int x, int y ) throws Exception { if ( x>=m_w || y>=m_h || x<0 || y<0 ) throw new Exception("CIUSuperEllipse::pixelOn> Invalid x/y request: " + x + "/" + y ); if ( m_data.bitSet( y*m_w + x ) ) return true; return false; } /** ************************************************************* * * Color accessor - given an X/Y pair, return the IUColor * at that coordinate. * * @param x The x-coordinate of interest * @param x The y-coordinate of interest * @returns byte The color at the x/y pair * * @throws Exception on array-out-of-bounds pixel request * @exception Exception on array-out-of-bounds pixel request * */ public byte color( int x, int y ) throws Exception { if ( x>=m_w || y>=m_h || x<0 || y<0 ) throw new Exception("CIUSuperEllipse::color> Invalid x/y request: " + x + "/" + y ); return m_color; } /** ************************************************************* * * Rotate the superellipse. Return a CIUPrimitive * * @param angle The angle to rotate through * @returns this * */ public CIUPrimitive rotate( double angle ) { return this; } /** ************************************************************* * * Uniformly scale the superellipse. Recalculate it's pixel * values. Return a CIUPrimitive * * @param scalar The scalar * @returns this * */ public CIUPrimitive scale( double scalar ) { m_A *= scalar; m_B *= scalar; m_w = ( m_B*2 ); m_h = ( m_A*2 ); m_data = null; m_data = new CIUBitArray( m_w, m_h ); calculatePixels(); return this; } /** ************************************************************* * * Convert the superellipse to a string representation. * * @returns String The superellipse * */ public String toString() { String ret = ""; for ( int i=0; i