/*
 * MercatorImageProjection.java  1.0  98/06/03  Carl Burke
 *
 * Provides a mercator projection of data on a sphere.
 *
 * Copyright (c) 1998 Carl Burke.
 *
 * Derived from code in planet.c Copyright 1998 Torben AE. Mogensen
 */

public class MercatorImageProjection extends ImageProjection
{
    public MercatorImageProjection(int w, int h, SolidNoiseGenerator sng)
    {
	super(w, h, sng);
    }
    public void renderTerrain()
    {
	double x,y,scale1,cos2,theta1;
	int i,j,k;

	// render the Mercator projection of a spherical section of the noise

	y = Math.sin(lat);
	y = (1.0+y)/(1.0-y);
	y = 0.5*Math.log(y);
	k = (int)(0.5*y*Width*scale/Math.PI);
	for (j = 0; j < Height; j++)
	{
	    y = Math.PI*(2.0*(j-k)-Height)/Width/scale;
	    y = Math.exp(2.*y);
	    y = (y-1.)/(y+1.);
	    cos2 = Math.sqrt(1.0-y*y);
	    scale1 = (scale*((double)Width)/((double)Height)/(cos2)/(Math.PI));
	    SNG.setScaling(scale1, (double)Width, (double)Height);
	    for (i = 0; i < Width ; i++)
	    {
	      theta1 = longi-0.5*Math.PI+Math.PI*(2.0*i-Width)/Width/scale;
	      pixels[j*Width+i] = SNG.color(Math.cos(theta1)*cos2,y,-Math.sin(theta1)*cos2);
	    }
	}
    }
    public void renderLatitudes()
    {
	double y,theta1;
	int i,j,k;

	y = Math.sin(lat);
	y = (1.0+y)/(1.0-y);
	y = 0.5*Math.log(y);
	k = (int)(0.5*y*Width*scale/Math.PI);

	    for (theta1 = 0.0; theta1>-90.0; theta1-=hgrid);
	    for (theta1 = theta1; theta1<90.0; theta1+=hgrid)
	    {
		y = Math.sin(DEG2RAD*theta1);
		y = (1.0+y)/(1.0-y);
		y = 0.5*Math.log(y);
		j = Height/2+(int)(0.5*y*Width*scale/Math.PI)+k;
		if (j>=0 && j-360.0; theta1-=vgrid);
	    for (theta1 = theta1; theta1<360.0; theta1+=vgrid)
	    {
		i = (int)(0.5*Width*(1.0+scale*(DEG2RAD*theta1-longi)/Math.PI));
		if (i>=0 && i