package org.lucci.madhoc.bining;

import org.lucci.up.data.Point;

/*
 * Created on Jul 24, 2004
 */

/**
 * @author luc.hogie
 */
public class Grid
{
    private double edgeSize;
    private Cel[][] cels;

    public Grid(double surface)
    {
        if (surface < 10000)
            throw new IllegalArgumentException("surface <= 10.000. surface= " + surface);

        if (surface % 10000 != 0)
            throw new IllegalArgumentException("surface must be divisible by 10.000");

        this.edgeSize = (int) Math.sqrt(surface);

        // each cel is 10x10m
        int celCount = (int) (edgeSize / 10 + 1);
        cels = new Cel[celCount][celCount];
        
        for (int i = 0; i < celCount; ++i)
        {
            for (int j = 0; j < celCount; ++j)
            {
                cels[i][j] = new Cel(i, j); 
            }
        }
    }
    
    public double getEdgeLenght()
    {
        return edgeSize;
    }

    public Cel getCelAt(Point point)
    {
        return cels[(int) (point.getX() / 10f)][(int) (point.getY() / 10f)];
    }

    public Cel[][] getCels()
    {
        return cels;
    }
}