
/*
	TryAngle2 will draw an interactive triangle of various colors.
	It uses screen coords, unlike TryAngle, and is intended to help recognize
	the properties of Medians and Altitudes
	
	version 2.0 - 24 Nov 1998 Fr. Chris Thiel, OFMCap <cct@ktb.net>
*/

import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
import java.lang.Math;

public class TryAngle2 extends java.applet.Applet {

    int activeDot=0;
    int rx[];
	int ry[];
	String colorItem;
	String vertex="ABC";
	int dist[];
	int ang[];
	
	Choice daMenu = new Choice();
	
	boolean moving=false;
	boolean showLabels=false;
	boolean mediansOn=false;
	boolean altitudesOn=false;
	int fudge=7; //fudge factor for sensitivity
	
    public void init() {
   		rx = new int[3];
		ry = new int[3];
		dist = new int[3];
		ang = new int[3];
		
		
		daMenu.addItem("Triangle Only");
		daMenu.addItem("Triangle with Medians");
		daMenu.addItem("Triangle with Altitudes");
		daMenu.addItem("Triangle with Medians and Altitudes");
		
		
		
		add(daMenu);
	

        setBackground(new Color(0xF0F080));
       
        	rx[0] = 100;rx[1] = 200;rx[2] = 200;
        	ry[0] = 200; ry[1] = 200;ry[2] = 100;
       
    }

    public void paint(Graphics g) {
     
    	switch (daMenu.getSelectedIndex()) {
    		case(0):
    			showLabels = true;
    			mediansOn=false;
    			altitudesOn=false;
    			break;
    		case(1):
    			mediansOn=true;
    			altitudesOn=false;
    			showLabels = false;
    			break;
        	case(2):
    			mediansOn=false;
    			altitudesOn=true;
    			showLabels = false;
    			break;
    		case(3):
    			mediansOn=true;
    			altitudesOn=true;
    			showLabels = false;
    			break;
    		
    		default:
    			g.setColor(Color.black);
    			showLabels = true;
    			
    	}
    	
		for (int i=0; i<3; i++){ 
		 		//vertices always red
       			g.setColor(Color.red);
				g.fillOval(rx[i]-3,ry[i]-3, 7,7);
        		//black triangle
        		g.setColor(Color.black);
        		g.drawLine(rx[i],ry[i], rx[(i+1)%3],ry[(i+1)%3]);
        		g.drawString(vertex.substring(i,i+1), rx[i],ry[i]-3);
        		
        		if (mediansOn) {
        			g.setColor(Color.cyan);
        			g.drawLine(Math.round( (rx[i]+rx[(i+1)%3])/2) , Math.round( (ry[i]+ry[(i+1)%3])/2), 
        			           rx[(i+2)%3], ry[(i+2)%3]);
        	
        		}
        		if (altitudesOn) {
        			g.setColor(Color.green);
        			g.drawLine(rx[(i+2)%3]+Math.round( (ry[i]-ry[(i+1)%3])*10) , ry[(i+2)%3]+Math.round( (rx[(i+1)%3]-rx[i])*10), 
        			           rx[(i+2)%3]-Math.round( (ry[i]-ry[(i+1)%3])*10), ry[(i+2)%3]-Math.round( (rx[(i+1)%3]-rx[i])*10));
        	
        		}
        		
       	  }
          if (moving) {
       		g.setColor(Color.blue);
       		int x = rx[activeDot];
       		int y = ry[activeDot];
        	g.drawString("Click again to place vertex "+vertex.charAt(activeDot)+" at ("+x+","+y+")", 10,290);
        	g.fillOval(rx[activeDot]-3,ry[activeDot]-3, 7,7);
        	} 
        else {
        	g.setColor(Color.black);
        	g.drawString("Click on a vertex to move it", 10,290);
        	for (int i=0; i<3; i++){ 
        		// draw lengths of sides at the midpoint
        		g.setColor(Color.blue);
        		g.drawString(Integer.toString(dist[i]), Math.round( (rx[i]+rx[(i+1)%3])/2) , Math.round( (ry[i]+ry[(i+1)%3])/2));
        		g.setColor(Color.gray);
        		g.drawString(Integer.toString(ang[i]), rx[i]-14, ry[i]+10);
        		if (showLabels) {
        			g.setColor(Color.black);
        			g.drawString(vertex.substring(i,i+1), rx[i],ry[i]-3);
        		}
        	
        	}
        }
       
    }
	public boolean action(Event e, Object arg) {
                   if (e.target instanceof Choice) {
                     repaint();
                      return true;
                  } else { return false;}
               
    }
    
    public boolean handleEvent(Event e) {
                  return super.handleEvent(e);
    }
             
    /*
     * Mouse methods
     */
    public boolean mouseDown(java.awt.Event evt, int x, int y) {
      
       
        if (!moving){ /* Find which dot to relocate */
          for ( int i=0; i<3; i++){
            if ( ( x > rx[i]-fudge && x < rx[i]+fudge)  &&
                 ( y > ry[i]-fudge && y < ry[i]+fudge) ) {
                 activeDot = i ;
                 moving=true; 
            }
          }
        } else {  /* Compute distances now that a place was picked */
  			moving=false;
  			for (int i=0; i< 3;i++){
  			    double d = Math.round(Math.sqrt( Math.pow((rx[i]-rx[(i+1)%3]),2) + Math.pow((ry[i]-ry[(i+1)%3]),2)));
  				dist[i]=(int)(d);
  					
        	
  			}
  			for (int i=0; i<3; i++){
  				/* use the law of cosines to find each angle */
  				double sideC2 = Math.pow(dist[(i+2)%3],2);
				double sideA2 = Math.pow(dist[(i)%3],2);
				double sideB2 = Math.pow(dist[(i+1)%3],2);
				double sideA  = dist[i];
				double sideC  = dist[(i+2)%3];
			
				double numerator=  sideA2 + sideC2 - sideB2 ;
				double denominator = 2 * sideA *sideC;
				double a = Math.round(  (180/Math.PI)*(Math.acos(numerator/denominator)) );
				ang[i] = (int)( a );
  				
  				
  			}
		  }

        repaint();
        return true;
    }
	
  	
  	
    public boolean mouseMove(java.awt.Event evt, int x, int y) {
        
         getAppletContext().showStatus("Location of mouse: (" + x + ", " + y + ")");
        
         if (moving){
         	requestFocus();
         	rx[ activeDot] = x;
        	ry[ activeDot] = y;  
        	repaint();
         }
        return true;
    }

    public void mouseEnter() {
        repaint();
    }

    public void mouseExit() {
       
        repaint();
    }

    /**
     * Focus methods
     */
    public void keyDown(int key) {
    }
    
    
}//end applet

