Advertisement
michael_hartman_cz

PA1 - úkol 3.2 (Ořezávání úseček)

Mar 28th, 2013
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. #ifndef __PROGTEST__
  2. #include <stdio.h>
  3. #endif /* __PROGTEST__ */
  4. void swapNumbers ( double *a, double *b );
  5. void rearangeRectangle ( double *rx1, double *ry1, double *rx2, double *ry2 );
  6. int pointPosition ( double x, double y, double rx1, double ry1, double rx2, double ry2 );
  7. int clipLine ( double rx1, double ry1, double rx2, double ry2, double * ax, double * ay, double * bx, double * by );
  8. #ifndef __PROGTEST__
  9. int main ( int argc, char * argv[] )
  10.  {
  11.     double ax, ay, bx, by, rx1, ry1, rx2, ry2;
  12.     puts ( "ax, ay, bx, by, rx1, ry1, rx2, ry2" );
  13.     scanf ( "%lf %lf %lf %lf %lf %lf %lf %lf", &ax, &ay, &bx, &by, &rx1, &ry1, &rx2, &ry2 );
  14.     printf ( "\nclipline exitcode: %d\n", clipLine( rx1, ry1, rx2, ry2, &ax, &ay, &bx, &by ) );
  15.     printf ( "ax: %f\nay: %f\nbx: %f\nby: %f\n", ax, ay, bx, by );
  16.    
  17.     return 0;
  18.  }
  19. #endif /* __PROGTEST__ */
  20.  
  21. void swapNumbers ( double *a, double *b )
  22.  {
  23.     *a += *b;
  24.     *b = *a - *b;
  25.     *a = *a - *b;
  26.  }
  27.  
  28. void rearangeRectangle ( double *rx1, double *ry1, double *rx2, double *ry2 )
  29.  {
  30.     if ( *rx1 > *rx2 )
  31.         swapNumbers ( rx1, rx2 );
  32.     if ( *ry1 < *ry2 )
  33.         swapNumbers ( ry1, ry2 );
  34.  }
  35.  
  36. int pointPosition ( double x, double y, double rx1, double ry1, double rx2, double ry2 )
  37.  {
  38.     int position = 0; /* inside */
  39.     if ( y > ry1 ) /* point is above rectangle */
  40.         position += 1;
  41.     if ( y < ry2 ) /* point is bellow rectangle */
  42.         position += 2;
  43.     if ( x > rx2 ) /* point is on the right side of the rectangle */
  44.         position += 4;
  45.     if ( x < rx1 ) /* point is on the left side of the rectangle */
  46.         position += 8;
  47.     return position;
  48.  }
  49.  
  50. int clipLine ( double rx1, double ry1, double rx2, double ry2, double * ax, double * ay, double * bx, double * by )
  51.  {
  52.     int aPosition, bPosition;
  53.     int *actualPosition;
  54.     double *actualX, *actualY;
  55.     rearangeRectangle ( &rx1, &ry1, &rx2, &ry2 );
  56.     while ( 1 )
  57.      {
  58.         aPosition = pointPosition ( *ax, *ay, rx1, ry1, rx2, ry2 );
  59.         bPosition = pointPosition ( *bx, *by, rx1, ry1, rx2, ry2 );
  60.         if ( !( aPosition | bPosition ) ) /* whole line is already inside */
  61.             return 1;
  62.         if ( aPosition & bPosition ) /* whole line is outside */
  63.             return 0;
  64.         if ( !aPosition ) /* a is inside */
  65.          {
  66.             actualX = bx;
  67.             actualY = by;
  68.             actualPosition = &bPosition;
  69.          }
  70.         else
  71.          {
  72.             actualX = ax;
  73.             actualY = ay;
  74.             actualPosition = &aPosition;
  75.          }
  76.         if ( *actualPosition & 1 ) /* actual point is above rect */
  77.          {
  78.             *actualX = *ax + ( *bx - *ax ) * ( ry1 - *ay ) / ( *by - *ay );
  79.             *actualY = ry1;
  80.          }
  81.         else if ( *actualPosition & 2 ) /* actual point is bellow rect */
  82.          {
  83.             *actualX = *ax + ( *bx - *ax ) * ( ry2 - *ay ) / ( *by - *ay );
  84.             *actualY = ry2;
  85.          }
  86.         else if ( *actualPosition & 4 ) /* actual point is on the right side of the rectangle */
  87.          {
  88.             *actualY = *ay + ( *by - *ay ) * ( rx2 - *ax ) / ( *bx - *ax );
  89.             *actualX = rx2;
  90.          }
  91.         else /* actual point is on the left side of the rectangle */
  92.          {
  93.             *actualY = *ay + ( *by - *ay ) * ( rx1 - *ax ) / ( *bx - *ax );
  94.             *actualX = rx1;
  95.          }
  96.      }
  97.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement