public class AFS2_Grid
{
public int Level { get; set; }
private int nb_cellinEarthWidth { get; set; }
public double Grid_Width { get; set; }
public PointD[] Ref_points { get; set; }
// constructor
public AFS2_Grid(int Level)
{
// Calculation based on the gross value of the Aerofly FS2 grid that is in a space between 0 and 65536
// calculation of number of cells contained in the width of the globe according to the "Level" requested
this.Level = Level;
this.nb_cellinEarthWidth = (int)Math.Pow(2, Level);
double EarthValuMax = 65536.0;
// this time the width and height is no longer expressed in degrees but in decimal units compared to the grid with conversion
this.Grid_Width = EarthValuMax / nb_cellinEarthWidth;
// initialize the array which will contain the set of points corresponding to the lower left corners of each cells of the grid
this.Ref_points = new PointD[nb_cellinEarthWidth + 1];
for (int i = 0; i < nb_cellinEarthWidth + 1; i++)
{
double point = this.Grid_Width * i;
double[] coord = LonLatFromWorldGrid(new double[2] { point, point });
this.Ref_points[i] = new PointD(coord[0], coord[1]);
}
}
// other functions
public double WorldGridConstantA = 2.3311223704144;
public int TM_PI = 180;
public string GetWorldGridName_FromLonLat(PointD point)
{
int nb_cellinEarthWidth = (int)Math.Pow(2, 16);
double x = point.X / TM_PI;
double y = point.Y / TM_PI;
y = Math.Tan(WorldGridConstantA * y) / WorldGridConstantA;
x = 0.5 + 0.5 * x;
y = 0.5 + 0.5 * y;
double[] coord = new double[2] { nb_cellinEarthWidth * x, nb_cellinEarthWidth * y };
// then convert x and y to hexadecimal to have the write corresponding to the file name
string name = Convert.ToInt32(coord[0]).ToString("X") + "_" + Convert.ToInt32(coord[1]).ToString("X");
return name;
}
public double[] GetWorldGrid_FromLonLat(Double[] point)
{
int nb_cellinEarthWidth = (int)Math.Pow(2, 16);
double x = point[0]/ TM_PI;
double y = point[1] / TM_PI;
y = Math.Tan(WorldGridConstantA * y) / WorldGridConstantA;
x = 0.5 + 0.5 * x;
y = 0.5 + 0.5 * y;
return new double[2] { nb_cellinEarthWidth * x, nb_cellinEarthWidth * y };
}
private double[] LonLatFromWorldGrid(double[] world_grid)
{
int nb_cellinEarthWidth = (int)Math.Pow(2, 16);
double x = world_grid[0] / nb_cellinEarthWidth;
double y = world_grid[1] / nb_cellinEarthWidth;
x = 2.0 * (x - 0.5);
y = 2.0 * (y - 0.5);
y = Math.Atan(WorldGridConstantA * y) / WorldGridConstantA;
x *= TM_PI;
y *= TM_PI;
return new double[2] { x, y };
}
}