// (C)2007 Attila Kovacs <attila@submm.caltech.edu>


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ProjectionTest {

	public static void main(String[] args) {
		try {
			SphericalProjection projection = new SansonFlamsteed();	
			projection.setReference(new SphericalCoordinates(0.0, 0.0));
			
			/*
			projection.rightAnglePole = false;
			projection.pole.set(0.0 * Unit.deg, 45.0*Unit.deg);
			projection.poleNative.setLongitude(75.0*Unit.deg);
			*/
		
			drawGrid(projection, Color.BLACK);
			
			System.err.println(projection.getDeprojected(new Vector2D(0.1, 0.1)).toString(Util.f3));
			
			
		}
		catch(Exception e) { System.err.println(e.getMessage()); }
	}
	
	public static void drawGrid(final SphericalProjection projection, final Color color) {
		//System.out.println(projection.getClass().getSimpleName());
		
		JFrame frame = new JFrame("Projection");
		
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			    System.exit(0);
			}
		});	
		
		frame.setSize(600,300);
		JPanel panel = new JPanel() {
			int height, width;
			double xRange = 8.0;
			double yRange = 4.0;
			
			public void paintComponent(Graphics g) {
				//super.paintComponent(g);
				
				SphericalCoordinates coords = new SphericalCoordinates();
				
				Vector2D offset = new Vector2D();
				Vector2D prevX = new Vector2D();
				Vector2D prevY = new Vector2D();
				
				height = getHeight();
				width = getWidth();
				
				g.setColor(color);
				
				
				for(int iTheta = -80; iTheta <= 90; iTheta += 10)
					for(int iPhi=-170; iPhi <= 180; iPhi += 10) {
				
				/*
				for(int iTheta = 0; iTheta <= 40; iTheta += 10)
					for(int iPhi=0; iPhi <= 40; iPhi += 10) {
					*/	
						
						
						//System.out.println(iPhi + ", " + iTheta);
						
						coords.set(iPhi * Unit.deg, iTheta * Unit.deg);
						projection.project(coords, offset);
						
						//System.out.println(offset);
						
						coords.set((iPhi-9.99) * Unit.deg, iTheta * Unit.deg);
						projection.project(coords, prevX);
						coords.set(iPhi * Unit.deg, (iTheta-9.99) * Unit.deg);
						projection.project(coords, prevY);
						
						g.drawLine(getX(prevX), getY(prevX), getX(offset), getY(offset));
						g.drawLine(getX(prevY), getY(prevY), getX(offset), getY(offset));
					}
				
			}
			
			public int getX(Vector2D offset) {
				return (int) Math.round((offset.x / xRange + 0.5) * width);
			}
			
			public int getY(Vector2D offset) {
				return (int) Math.round((0.5 - offset.y / yRange) * height);
			}
			
		};
		frame.add(panel);
		frame.setVisible(true);
		
	}
	
}
