import java.awt.Graphics;
import java.awt.Point;
/**
 * Title:        Sierpinski Gasket<p>
 * Description:  Class supports the functionality required for
 *    a triangle to be used for a Sierpinski gasket <p>
 * Copyright:    Copyright (c) Anne Denton<p>
 * @author Anne Denton
 * @version 1.0
 */
public class Triangle
{  public Triangle(Point cpt, Point cpl, Point cpr)
   {  pt = new Point(cpt);
      pl = new Point(cpl);
      pr = new Point(cpr);
   }

/** Draws the triangle represented by this object.
   @param g Graphics context for drawing
*/
   public void draw(Graphics g)
   {  g.drawLine((int)pt.x,(int)pt.y,(int)pl.x,(int)pl.y);
      g.drawLine((int)pt.x,(int)pt.y,(int)pr.x,(int)pr.y);
      g.drawLine((int)pl.x,(int)pl.y,(int)pr.x,(int)pr.y);
   }

/** Returns the size of the triangle represented by this object.
   @return baseline of the triangle
*/
   public int getWidth()
   {  return (int) (pr.x - pl.x);
   }

/** Creates a new Triangle object that has half
   the length in each dimension compared with the current 
   triangle and shares the point at the top.
   @return created Triangle
*/
   public Triangle topTriangle()
   {  Point ptNew = pt;
      Point plNew = midPoint(pt,pl);
      Point prNew = midPoint(pt,pr);
      return new Triangle(ptNew,plNew,prNew);
   }

/** Creates a new Triangle object that has half
   the length in each dimension compared with the current 
   triangle and shares the point at the bottom left.
   @return created Triangle
*/
   public Triangle leftTriangle()
   {  Point ptNew = midPoint(pl,pt);
      Point plNew = pl;
      Point prNew = midPoint(pl,pr);
      return new Triangle(ptNew,plNew,prNew);
   }

/** Creates a new Triangle object that has half
   the length in each dimension compared with the current 
   triangle and shares the point at the bottom right.
   @return created Triangle
*/
   public Triangle rightTriangle()
   {  Point ptNew = midPoint(pt,pr);
      Point plNew = midPoint(pl,pr);
      Point prNew = pr;
      return new Triangle(ptNew,plNew,prNew);
   }

/** midPoint determines the point that splits the line
   between two points into two halfs of equal length.
   @param p1  one end point of line
   @param p2  other end point of line
*/
   private Point midPoint(Point p1, Point p2)
   {  return new Point((int)(p1.x + p2.x)/2,
         (int)(p1.y + p2.y)/2);
   }

   private Point pt;
   private Point pl;
   private Point pr;
}
