code: protected double computeUnweighted( double tl_x, double tl_y, 
  										double samplePeriod ,
  										SparseImageGradient<T,G> g)
  	{
  		// add 0.5 to c_x and c_y to have it round
  		tl_x += 0.5;
  		tl_y += 0.5;
  
  		double Dx=0,Dy=0;
  		for(int y = 0; y < sampleWidth; y++ ) {
  			int pixelsY = (int)(tl_y + y * samplePeriod);
  
  			for(int x = 0; x < sampleWidth; x++ ) {
  				int pixelsX = (int)(tl_x + x * samplePeriod);
  
  				GradientValue v = g.compute(pixelsX,pixelsY);
  				Dx += v.getX();
  				Dy += v.getY();
  			}
  		}
  		return Math.atan2(Dy,Dx);
  	}
docs: Compute the gradient while checking for border conditions