//************************************************************************
//Elif Tosun
//274b-ab
//An Applet that shows three line segments on the screen, and at the same time
//computes whether they intersect or not. The results of the computations are
//printed on the java console.
//************************************************************************

import java.applet.*;
import java.awt.*;
import java.io.*;                          //importing java libraries
                           
//************************************************
//myApplet
//used in checking if given segments cross or not
//
//************************************************



public class myApplet extends Applet
{
  private  Point A, B, C, D, E, F;         //declaring variables of type Point


//************************************************
// Init
// This function is called when the applet is 
// loaded. It initializes the variables and
// calls crossSeg to find out if the lines cross
// or not. 
//************************************************ 


  public void init()
  {
    A = new Point(-25, -75);
    B = new Point(-25, 40);
    C = new Point(25, -100);                 //initialize points
    D = new Point(-25, 100);
    E = new Point(5, -20);
    F = new Point(-100, -50);
    
 


    if (crossSeg(A, B, C, D))
      System.out.println("AB and CD are crossing segments.");
    else
      System.out.println("AB and CD are NOT crossing.");
    
                                         // checks if segments AB and CD cross


    if (crossSeg(C, D, E, F))
      System.out.println("CD and EF are crossing segments.");
    else
      System.out.println("CD and EF are NOT crossing.");

                                         // checks if segments CD and EF cross


    if (crossSeg(A, B, E, F))
       System.out.println("AB and EF are crossing segments.");
    else
      System.out.println("AB and EF are NOT crossing.");
    
                                         // checks if segments AB and EF cross


  }//end init

//************************************************
// Paint
// This function is does all the graphic work. 
// It draws the axes, the segments, the endpoints
// of segments, and their labels on screen.
//************************************************ 

  public void paint(Graphics g){

    g.setColor(Color.red);                //set color to red, to distinguish 
                                          //between the axes and the 
                                          //real segments
   
    g.drawLine(0, 150, 300, 150);         //draw axes 
    g.drawLine(150, 0, 150, 300);

    g.setColor(Color.black);              //set color back to black


    myDrawSeg(A, B, g);
    g.drawString("A("+A.x+","+A.y+")", 144+A.x, 161-A.y);
    g.drawString("B("+B.x+","+B.y+")", 135+B.x, 145-B.y); 
                                         //draw segment AB and label it

    myDrawSeg(C, D, g);
    g.drawString("C("+C.x+","+C.y+")", 145+C.x, 161-C.y);
    g.drawString("D("+D.x+","+D.y+")", 155+D.x, 146-D.y);
                                        //draw segment CD and label it

    myDrawSeg(E, F, g);
    g.drawString("E("+E.x+","+E.y+")", 155+E.x, 160-E.y);
    g.drawString("F("+F.x+","+F.y+")", 155+F.x, 160-F.y);
                                        //draw segment EF and label it
  }// end paint

//************************************************************************
// USER-DEFINED FUNCTIONS
//
//************************************************************************

//************************************************
// myDrawSeg
// This function draws the segments with their 
// end points. 
// Called in paint.
//************************************************ 

  public void myDrawSeg(Point A, Point B, Graphics g)
  {
    g.fillOval(148+A.x,150-A.y, 4, 4);               //draw endpoints
    g.fillOval(148+B.x,150-B.y, 4, 4);
    g.drawLine(150+A.x, 150-A.y, 150+B.x, 150-B.y);  //draw segment

  }//end myDrawSeg


//************************************************
// Det
// This functioncomputes the determinant of 3 points.
// It also gives twice the area of triangle formed by
// these three points. The sign of the determinant 
// gives information about left/right turns.
// Called in leftTurn and Collinear functions.
//************************************************ 


  public int Det(Point A, Point B, Point C)
  {
    return (A.x*B.y + A.y*C.x + B.x*C.y - C.x*B.y - A.x*C.y - B.x*A.y);
                                         //from definition of determinant

  }// end Det


//************************************************
// LeftTurn
// This function finds out if it is a left turn
// when you are moving on AB and turn towards C.
// Used for finding out about corssing of segments.
// Called in CrossSeg function.
//************************************************ 


  public boolean leftTurn(Point A, Point B, Point C)
  {
    if (Det(A, B, C) > 0)              //if the determenant of three points
      return true;                     //is positive then it is a left turn.
    else 
      return false;                    //else it is not.
 
  }//end leftTurn
  

//************************************************
// Collinear
// This function finds out if the given three points 
// are collinear or not.
// Called in CrossSeg and Between functions.
//************************************************ 

  public boolean Collinear(Point A, Point B, Point C)
  {
    if (Det(A, B, C) == 0)                 //the determinant of three collinear
      return true;                         //points is zero.
    
    else
      return false;
  
  }//end Collinear
  
//************************************************
// Between
// This function finds out among the given three  
// points, whether the third one is in between the
// first and the second one.
// Called in CrossSeg.
//************************************************ 

  public boolean Between(Point A, Point B, Point C)
  {
    if (!Collinear(A, B, C))      //for a point to be in between the other
      return false;               //two all three has to be collinear
    
    
    else
      {
        if (A.x != B.x)            //if A and B are not vertical 
                                   //check x coordintes
          return (((A.x <= C.x) && (C.x <= B.x)) || 
                  ((A.x >= C.x) && (C.x >= B.x)));
        
        else                       //else check y coordinates
          return (((A.y <= C.y) && (C.y <= B.y)) || 
                  ((A.y >= C.y) && (C.y >= B.y)));
        
      }
  
  }//end Between

//************************************************
// CrossSeg
// This function actually finds out if two segments 
// cross or not. It takes in the endpoints of the 
// two segments.
// Called in init.
//************************************************ 

  public boolean crossSeg(Point A, Point B, Point C, Point D)
  {
    if (!Collinear(A,B,C) && !Collinear(A,B,D) && !Collinear(C,D,A) && 
        !Collinear(C,D,B))
                                             //if they are not collienar
      {  if (((!leftTurn(A,B,D) && leftTurn(A,B,C))
              ||(!leftTurn(A,B,C)  && leftTurn(A,B,D))) && 
             ((leftTurn(C,D,A) && !leftTurn(C,D,B)) || 
              (!leftTurn(C,D,A) && leftTurn(C,D,B))))
                                             //and if the endpoints of one
                                             //segment lie on diff sides of 
                                             //the other one
        return true;                         //they cross
      else 
        return false;                        //else they don;t
      }
    

    else                                     //if they are collinear
      {
        if (Between(A, B, C) || Between(A, B, D) || Between (C, D, A) ||
            Between(C, D, B))   
                                             //check if the third one lies
                                             //between the other two.
          return true;                       //if so, they cross
        
        
        else
          return false;                       //else they don't
        
      }
    
  }//end crosseg


  
}//end myApplet
