import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Color;
public class seg2 extends java.applet.Applet{
  Color col1,col2,col3;
  int N,taka,haba;
  int k,i,j,l;
  String NS,habaS,takaS;
  double Nd,takad,habad;
  double dxs,dxe,dys,dye;
  double dslope,dintercept;
  int br;
  double intersectionx,minx,maxx;
  double minx2;
  double maxx2;

  public double dou(String dous){
    double dou1;
    dou1 = (Double.valueOf(dous)).doubleValue();
    return dou1;
  }

  public double rand(){
    double rand1;
    rand1=Math.random();
    return rand1;
  }

  public void init(){
    col1=Color.black;
    col2=Color.pink;
    col3=Color.yellow;
    takaS=getParameter("takap");
    habaS=getParameter("habap");
    NS=getParameter("Np");
    habad=dou(habaS);
    takad=dou(takaS);
    Nd=dou(NS);
    haba=(int)habad;
    taka=(int)takad;
    N=(int)Nd;
    if(N==20){
      N=4+(int)(46*rand());
    }
  }
  double xs[]=new double[100];
  double ys[]=new double[100];
  double xe[]=new double[100];
  double ye[]=new double[100];
  int xsI[]=new int[100];
  int ysI[]=new int[100];
  int xeI[]=new int[100];
  int yeI[]=new int[100];
  double s[]=new double[100];
  String sss[]=new String[100];

  double slope[]=new double[100];
  double intercept[]=new double[100];


  public double jou(double a,double b){
    double jou1;
    jou1=Math.pow(a,b);
    return jou1;
  }



  //main function
  //main֐
  public void paint(java.awt.Graphics g){
    g.setColor(col1);
    g.fillRect(1,1,haba,taka);
    g.setColor(col3);
    g.drawString("N="+N,15,15);
    k=0;
    while(k<N){
      dxs=rand()*(haba-30)+15;
      dys=rand()*(taka-30)+15;
      dxe=rand()*(haba-30)+15;
      dye=rand()*(taka-30)+15;
      dslope=(dye-dys)/(dxe-dxs);
      dintercept=dys-dslope*dxs;
      minx=dxs;
      maxx=dxe;
      if(dxs>dxe){
          minx=dxe;
          maxx=dxs;
      }
      br=0;
      for(i=0;i<k;i++){
          minx2=xs[i];
          maxx2=xe[i];
          if(xs[i]>xe[i]){
              minx2=xe[i];
              maxx2=xs[i];
          }
          intersectionx=(dintercept-intercept[i])/(slope[i]-dslope);
          if(intersectionx>minx && intersectionx<maxx){
              if(intersectionx>minx2 && intersectionx<maxx2){
                 br=1;
              }
          }

      }

      if(br==0){
          xs[k]=dxs;
          ys[k]=dys;
          xsI[k]=(int)(xs[k]+0.5);
          ysI[k]=(int)(ys[k]+0.5);
          xe[k]=dxe;
          ye[k]=dye;
          xeI[k]=(int)(xe[k]+0.5);
          yeI[k]=(int)(ye[k]+0.5);
          g.setColor(col3);
          g.drawLine(xsI[k],ysI[k],xeI[k],yeI[k]);
          g.setColor(col2);
          g.drawString(""+k,(xsI[k]+xeI[k])/2,(ysI[k]+yeI[k])/2);
          slope[k]=dslope;
          intercept[k]=dintercept;
          k++;
      }//br==0
    }//while k<n
  }
}