// (c) MIT 2003.  All rights reserved.

package rec.fun;

import java.awt.*;
import rec.util.*;
import rec.geo.*;
import rec.sys.*;
import rec.core.widgets.*;

/**
 * Tracks how many times a squiggle crosses a given square.
 */
public class HitGrid extends Grid implements Registerable
{
  
  int[][] hits; 
  public HitGrid( Surface surf, Squiggle targ )
  {
    this( surf, 4, targ );
  }
  
  public HitGrid( Surface surf, int grid_s, Squiggle targ )
  {
    super( surf, grid_s, targ );
  }

  int last_x, last_y;

  void setBounds()
  {
    hits = new int[grid_size][grid_size];
    last_x = -1;
    last_y = -1;

    super.setBounds();
  }

  protected void addHit( int tx, int ty )
  {
    hit[ tx ][ ty ] = true;

    // if in new square, then add a hit.
    if ( ( last_x != x ) || ( last_y != y ) )
      {
	hits[tx][ty]++;
	last_x = x;
	last_y = y;
      }
  }


  /**
   * return percentage with 2 lines.
   */
  public double double_density( )
  {
    int tot = 0;
    for (int i = 0; i < hit.length; i++ )
      for ( int j = 0; j < hit[i].length; j++ )
	if ( hits[i][j] > 1 ) 
	  tot++;

    return (float)tot / (hit.length * hit[0].length);
  }

}
