import java.awt.*;

public class Segment {  

protected float slope, b;
protected Point p, p1;  

   
public Segment(int x, int y, int x1, int y1) {
   slope = (float)(y-y1)/(float)(x-x1);
   b = Math.round(y - (slope*x)); 
   p = new Point(x,y);
   p1 = new Point(x1, y1);
}

   
public int getX()       { return p.x;  }
public int getY()       { return p.y;  }
public int getX1()      { return p1.x; }
public int getY1()      { return p1.y; }

public int getY(int x)  { return Math.round(slope*x + b); }
public int getX(int y)  { return Math.round((y-b)/slope); }
public int getB()       { return (int)b; }

public float getSlope() { return slope; }
public int getAngle()   { return (int)MyMath.getAngle(p, p1); }

public Point getStart() { return p; }
public Point getStop()  { return p1; }
   

public void draw(Graphics g) {  
   g.drawLine(p.x, p.y, p1.x, p1.y);
}


public void drawThick(Graphics g, int thickness) {  
   for (int i= (-thickness/2+1); i<=(thickness/2); i++)
      g.drawLine(p.x, p.y+i, p1.x, p1.y+i);
}
      

public boolean inside(int x, int y) {  
   float newb = y-(slope*x);
   if(Math.abs(b-newb) < 5) {
      if(y > p.y && y > p1.y) return false;
      if(y < p.y && y < p1.y) return false;
      return true;
   }
   return false;  
}
         

public void drawDashed(Graphics g, int dash) {                          
   float x, y, x1, y1, stop, theta, dx, dy;
   theta = (float)Math.atan(slope);
   if (p.x < p1.x)      { x = p.x; y = p.y; stop = p1.x;  }
   else if (p.x > p1.x) { x = p1.x; y = p1.y; stop = p.x; }
   else return;
      
   dx = dash * (float)Math.cos(theta);
   dy = dash * (float)Math.sin(theta);
   while (x <= stop) {  
      x1 = x + dx;          
      y1 = y + dy;
      if(x1>stop) { 
        x1 = stop;
        y1 = (stop==p1.x) ? p1.y : p.y;
      }
      g.drawLine(Math.round(x), Math.round(y), 
                 Math.round(x1), Math.round(y1));
      x += 2*dx;
      y += 2*dy;
   }
}


   
public static void drawVertDashed(Graphics g, int x, int y, int x1, int y1, int dash) {  
   if (y > y1) {  
      int tempy = y;     int tempx = x;
      y = y1;            x = x1;
      y1 = tempy;        x1 = tempx;
   }
   for (int i = y; i <= y1; i += dash+3) {
      if (i+dash >= y1) g.drawLine(x, i, x1, y1);
      g.drawLine(x, i, x1, i+dash);
   }
}
   
    
public void moveTo(int absX, int absY, int absX1, int absY1) {  
   p.x  = absX;
   p.y  = absY;
   p1.x = absX1;
   p1.y = absY1;
   slope = (absY-absY1)/(float)(absX-absX1);
   b = Math.round(absY - (slope*absX));
}
   

public void moveBy(int relX, int relY, int relX1, int relY1) {  
   p.x  += relX;
   p.y  += relY;
   p1.x += relX1;
   p1.y += relY1;
   slope = (float)(p.y-p1.y)/(float)(p.x-p1.x);
   b = Math.round(p.y - (slope*p.x));
}    


public int length() {  
   return MyMath.round( Math.sqrt( 
                 (p.x-p1.x)*(p.x-p1.x) + 
                 (p.y-p1.y)*(p.y-p1.y) ));
}
   

public static int Length(int x, int y, int x1, int y1) {  
   float f = (float)Math.sqrt( (x-x1)*(x-x1) +
                               (y-y1)*(y-y1) );
   return Math.round(f);
}
   
}

