Trekant.java [Last ned]

import java.awt.*;
import java.io.*;
 
 
/*
 * Klasse for å representere trekant.
 *
 */
public class Trekant extends Figur {
 
  private int x1,x2;
  private int y1,y2;
 
 
  public Trekant(Color f, int[] x, int[] y) {
    super(f, x[0], y[0]);
    x1 = x[1]; y1 = y[1];
    x2 = x[2]; y2 = y[2];
  }
 
 
  public void vis(Graphics g) {
    g.setColor(farge);
    g.drawLine(x, y, x1, y1);
    g.drawLine(x, y, x2, y2);
    g.drawLine(x1, y1, x2, y2);
  }
 
 
  public void merk(Graphics g) {
    g.setColor(Tegneprogram.MERKEFARGE);
    g.drawLine(x, y, x1, y1);
    g.drawLine(x, y, x2, y2);
    g.drawLine(x1, y1, x2, y2);
  }
 
 
  public boolean(Punkt p) {
    Punkt A = new Punkt(x,y);
    Punkt B = new Punkt(x1,y1);
    Punkt C = new Punkt(x2,y2);
    int ABn = (A.x-p.x)*(B.y-p.y)-(B.x-p.x)*(A.y-p.y);
    int BCn = (B.x-p.x)*(C.y-p.y)-(C.x - p.x)*(B.y-p.y);
    int CAn = (C.x-p.x)*(A.y-p.y)-(A.x - p.x)*(C.y-p.y);
    return (ABn*BCn > 0) && (BCn*CAn > 0);
  }
 
 
  public String egenskaper() {
    return "Trekant: " + super.egenskaper() + ". " +
           "Areal: " + areal();
  }
 
 
  public int areal() {
  Punkt v1 = new Punkt(x1-x,y1-y);
  Punkt v2 = new Punkt(x2-x,y2-y);
  double d = Math.abs( v1.x*v2.y - v2.x*v1.y );
    return (int)Math.round(d/2);
  }
 
 
  public void skrivTilFil(PrintWriter fil) throws IOException {
    String tgn = Tegneprogram.SKILLETEGN;
    fil.print("T" + tgn);
    super.skrivTilFil(fil);
    fil.println(x1 + tgn + y1 + tgn + x2 + tgn + y2 + tgn);
  }
 
}
Kildekode blir vist ved hjelp av GeSHi.