// joonistab joone, nii et kolmandiku osa pealt kummasti otspunktist murdub.
// teeb selle nagu 2D oleks :p

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Helves2 extends Applet{
	
	public Helves2(){
		init();	
	}
	
	public void init(){
		TextField info = new TextField("Vali hiirega joone otspunktid");
     	add(info);
		GraphicsMap line = new GraphicsMap(this);
		addMouseListener(line);
        addMouseMotionListener(line);
  	}

	public static void main (String argumendid[]){
   		Frame f = new Frame ("Joon2");
   		f.add(new Helves2());
   		f.setSize(400, 400);
   		f.setVisible(true);
	}
}

class GraphicsMap extends MouseAdapter implements MouseMotionListener{
	
	Helves2 joon;

	Point startP = new Point(0, 0);
    Point endP   = new Point(0, 0);
    int counter = 0;
    int nurkadearv = 1;
    int pikkus;
                
                
    public GraphicsMap(Helves2 j){
    	joon = j;
    }
                
    public void mousePressed(MouseEvent e){		// kounter loeb mitu klikki on tehtud ja 
      	counter++;								// kui kaks on tehtud siis joonistab kogu kupatuse
        Graphics g = joon.getGraphics();                  
        if(counter == 1){
           	startP.setLocation(e.getPoint());	// siit saab alguspunkti koordinaadid kesksirgele
            g.setColor(Color.blue);
            g.fillOval((int)startP.getX(), (int)startP.getY(), 6, 6);
        }
        else if(counter == 2){
        	endP.setLocation(e.getPoint());
            g.setColor(Color.blue);
            g.fillOval((int)endP.getX(), (int)endP.getY(), 6, 6);   // siit lopp koordinaadid                 
                    
                         
            Point p1 = new Point((int)((startP.getX() + (endP.getX()*0.5))/(1 + 0.5)), 		// arvutab kolmandiku punktid
            					 (int)((startP.getY() + (endP.getY()*0.5))/(1 + 0.5)));
            					 
      		Point p2 = new Point((int)((startP.getX() + (endP.getX()*2))/(1 + 2)), 
            					 (int)((startP.getY() + (endP.getY()*2))/(1 + 2)));
            					 
            Point p_kesk = new Point((int)((startP.getX() + endP.getX())/2),  	// pikima joone kesk
            					 	 (int)((startP.getY() + endP.getY())/2));
			

            pikkus = (int)(Math.sqrt(Math.pow((p1.getX() - startP.getX()), 2) + 			// kolmandiku pikkus
                         			 Math.pow((p1.getY() - startP.getY()), 2)));
                         
            
            double vector = Math.PI*2 / nurkadearv;		// siit arvutab nurgavektori
            
            int p3x=(int)(p_kesk.getX()+pikkus*Math.sin(vector));	// siin arvutab poole joone koordinaatide asukoha 
        	int p3y=(int)(p_kesk.getY()-pikkus*Math.cos(vector));	// kolmandiku pikkuse kaugusele
        	
        	Point p3 = new Point(p3x, p3y);
			
			g.setColor(Color.cyan);								// ja joonistab ekraanile
            g.fillOval((int)p3.getX(), (int)p3.getY(), 6, 6);
            //g.fillOval((int)p_kesk.getX(), (int)p_kesk.getY(), 6, 6);
			g.setColor(Color.red);
            g.drawLine((int)startP.getX(), (int)startP.getY(), (int)p1.getX(), (int)p1.getY());
            g.drawLine((int)p1.getX(), (int)p1.getY(), (int)p3.getX(), (int)p3.getY());
            g.drawLine((int)p3.getX(), (int)p3.getY(), (int)p2.getX(), (int)p2.getY());
            g.drawLine((int)p2.getX(), (int)p2.getY(), (int)endP.getX(), (int)endP.getY());
                         
		}
	}
	
	public void mouseDragged(MouseEvent e){}
    public void mouseMoved(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
	
}