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

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

	// render the Peter projection

      y = 2.0*Math.sin(lat);
      k = (int)(0.5*y*Width*scale/Math.PI);
      for (j = 0; j < Height; j++) {
	y = 0.5*Math.PI*(2.0*(j-k)-Height)/Width/scale;
	if (Math.abs(y)>1.0) for (i = 0; i < Width ; i++) pixels[j*Width+i] = SNG.background();
	else {
	  cos2 = Math.sqrt(1.0-y*y);
	  if (cos2>0.0) {
	    SNG.setScaling((scale*Width/Height/cos2/Math.PI), Width, 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 = 2.0*Math.sin(lat);
	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 = 2.0*Math.sin(DEG2RAD*theta1);
	  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



1