bridge/class-master/depend/pari/src/graph/plotport.c
/* A simpler way is to clip by 4 half-planes */
A simpler way is to clip by 4 half-planes
A simpler way is to clip by 4 half - planes
static int
clipline(double xmin, double xmax, double ymin, double ymax,
         double *x1p, double *y1p, double *x2p, double *y2p)
{
  int xy_exch = 0, rc = CLIPLINE_NONEMPTY;
  double t, sl;
  double xi, xmn, xmx;
  double yi, ymn, ymx;
  int x1_is_ymn, x1_is_xmn;
  double x1 = *x1p, x2 = *x2p, y1 = *y1p, y2 = *y2p;
  if ((x1 < xmin &&  x2 < xmin) || (x1 > xmax && x2 > xmax))
    return 0;
  if (fabs(x1 - x2) < fabs(y1 - y2)) { 
    xy_exch = 1;
    dswap(xmin, ymin); dswap(x1, y1);
    dswap(xmax, ymax); dswap(x2, y2);
  }
  xi = x1;
  yi = y1;
  sl = x1==x2? 0: (y2 - yi)/(x2 - xi);
  if (x1 > x2) {
    x1_is_xmn = 0;
    xmn = x2;
    xmx = x1;
  } else {
    x1_is_xmn = 1;
    xmn = x1;
    xmx = x2;
  }
  if (xmn < xmin) {
    xmn = xmin;
    rc |= x1_is_xmn? CLIPLINE_CLIP_1: CLIPLINE_CLIP_2;
  }
  if (xmx > xmax) {
    xmx = xmax;
    rc |= x1_is_xmn? CLIPLINE_CLIP_2: CLIPLINE_CLIP_1;
  }
  if (xmn > xmx) return 0;
  ymn = yi + (xmn - xi)*sl;
  ymx = yi + (xmx - xi)*sl;
  if (sl < 0) t = ymn, ymn = ymx, ymx = t;
  if (ymn > ymax || ymx < ymin) return 0;
  if (rc & CLIPLINE_CLIP_1) x1 = x1_is_xmn? xmn: xmx;
  if (rc & CLIPLINE_CLIP_2) x2 = x1_is_xmn? xmx: xmn;
  x1_is_ymn = ((sl >= 0) == (x1 < x2));
  if (ymn < ymin) {
    double x = (ymin - yi)/sl + xi; 
    if (x1_is_ymn) x1 = x, rc |= CLIPLINE_CLIP_1;
    else           x2 = x, rc |= CLIPLINE_CLIP_2;
  }
  if (ymx > ymax) {
    double x = (ymax - yi)/sl + xi; 
    if (x1_is_ymn) x2 = x, rc |= CLIPLINE_CLIP_2;
    else           x1 = x, rc |= CLIPLINE_CLIP_1;
  }
  if (rc & CLIPLINE_CLIP_1) y1 = yi + (x1 - xi)*sl;
  if (rc & CLIPLINE_CLIP_2) y2 = yi + (x2 - xi)*sl;
  if (xy_exch) 
    *x1p = y1, *x2p = y2, *y1p = x1, *y2p = x2;
  else
    *x1p = x1, *x2p = x2, *y1p = y1, *y2p = y2;
  return rc;
}
static int clipline ( double xmin , double xmax , double ymin , double ymax , double * x1p , double * y1p , double * x2p , double * y2p ) { int xy_exch = 0 , rc = CLIPLINE_NONEMPTY ; double t , sl ; double xi , xmn , xmx ; double yi , ymn , ymx ; int x1_is_ymn , x1_is_xmn ; double x1 = * x1p , x2 = * x2p , y1 = * y1p , y2 = * y2p ; if ( ( x1 < xmin && x2 < xmin ) || ( x1 > xmax && x2 > xmax ) ) return 0 ; if ( fabs ( x1 - x2 ) < fabs ( y1 - y2 ) ) { xy_exch = 1 ; dswap ( xmin , ymin ) ; dswap ( x1 , y1 ) ; dswap ( xmax , ymax ) ; dswap ( x2 , y2 ) ; } xi = x1 ; yi = y1 ; sl = x1 == x2 ? 0 : ( y2 - yi ) / ( x2 - xi ) ; if ( x1 > x2 ) { x1_is_xmn = 0 ; xmn = x2 ; xmx = x1 ; } else { x1_is_xmn = 1 ; xmn = x1 ; xmx = x2 ; } if ( xmn < xmin ) { xmn = xmin ; rc |= x1_is_xmn ? CLIPLINE_CLIP_1 : CLIPLINE_CLIP_2 ; } if ( xmx > xmax ) { xmx = xmax ; rc |= x1_is_xmn ? CLIPLINE_CLIP_2 : CLIPLINE_CLIP_1 ; } if ( xmn > xmx ) return 0 ; ymn = yi + ( xmn - xi ) * sl ; ymx = yi + ( xmx - xi ) * sl ; if ( sl < 0 ) t = ymn , ymn = ymx , ymx = t ; if ( ymn > ymax || ymx < ymin ) return 0 ; if ( rc & CLIPLINE_CLIP_1 ) x1 = x1_is_xmn ? xmn : xmx ; if ( rc & CLIPLINE_CLIP_2 ) x2 = x1_is_xmn ? xmx : xmn ; x1_is_ymn = ( ( sl >= 0 ) == ( x1 < x2 ) ) ; if ( ymn < ymin ) { double x = ( ymin - yi ) / sl + xi ; if ( x1_is_ymn ) x1 = x , rc |= CLIPLINE_CLIP_1 ; else x2 = x , rc |= CLIPLINE_CLIP_2 ; } if ( ymx > ymax ) { double x = ( ymax - yi ) / sl + xi ; if ( x1_is_ymn ) x2 = x , rc |= CLIPLINE_CLIP_2 ; else x1 = x , rc |= CLIPLINE_CLIP_1 ; } if ( rc & CLIPLINE_CLIP_1 ) y1 = yi + ( x1 - xi ) * sl ; if ( rc & CLIPLINE_CLIP_2 ) y2 = yi + ( x2 - xi ) * sl ; if ( xy_exch ) * x1p = y1 , * x2p = y2 , * y1p = x1 , * y2p = x2 ; else * x1p = x1 , * x2p = x2 , * y1p = y1 , * y2p = y2 ; return rc ; }
A simpler way is to clip by 4 half-planes
A simpler way is to clip by 4 half - planes
/* Exchange x and y */ /* Build y as a function of x */ /* Now we know there is an intersection, need to move x1 and x2 */ /* slope != 0  ! */ /* slope != 0  ! */ /* Exchange x and y */