Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __PROGTEST__
- #include <stdio.h>
- #endif /* __PROGTEST__ */
- void swapNumbers ( double *a, double *b );
- void rearangeRectangle ( double *rx1, double *ry1, double *rx2, double *ry2 );
- int pointPosition ( double x, double y, double rx1, double ry1, double rx2, double ry2 );
- int clipLine ( double rx1, double ry1, double rx2, double ry2, double * ax, double * ay, double * bx, double * by );
- #ifndef __PROGTEST__
- int main ( int argc, char * argv[] )
- {
- double ax, ay, bx, by, rx1, ry1, rx2, ry2;
- puts ( "ax, ay, bx, by, rx1, ry1, rx2, ry2" );
- scanf ( "%lf %lf %lf %lf %lf %lf %lf %lf", &ax, &ay, &bx, &by, &rx1, &ry1, &rx2, &ry2 );
- printf ( "\nclipline exitcode: %d\n", clipLine( rx1, ry1, rx2, ry2, &ax, &ay, &bx, &by ) );
- printf ( "ax: %f\nay: %f\nbx: %f\nby: %f\n", ax, ay, bx, by );
- return 0;
- }
- #endif /* __PROGTEST__ */
- void swapNumbers ( double *a, double *b )
- {
- *a += *b;
- *b = *a - *b;
- *a = *a - *b;
- }
- void rearangeRectangle ( double *rx1, double *ry1, double *rx2, double *ry2 )
- {
- if ( *rx1 > *rx2 )
- swapNumbers ( rx1, rx2 );
- if ( *ry1 < *ry2 )
- swapNumbers ( ry1, ry2 );
- }
- int pointPosition ( double x, double y, double rx1, double ry1, double rx2, double ry2 )
- {
- int position = 0; /* inside */
- if ( y > ry1 ) /* point is above rectangle */
- position += 1;
- if ( y < ry2 ) /* point is bellow rectangle */
- position += 2;
- if ( x > rx2 ) /* point is on the right side of the rectangle */
- position += 4;
- if ( x < rx1 ) /* point is on the left side of the rectangle */
- position += 8;
- return position;
- }
- int clipLine ( double rx1, double ry1, double rx2, double ry2, double * ax, double * ay, double * bx, double * by )
- {
- int aPosition, bPosition;
- int *actualPosition;
- double *actualX, *actualY;
- rearangeRectangle ( &rx1, &ry1, &rx2, &ry2 );
- while ( 1 )
- {
- aPosition = pointPosition ( *ax, *ay, rx1, ry1, rx2, ry2 );
- bPosition = pointPosition ( *bx, *by, rx1, ry1, rx2, ry2 );
- if ( !( aPosition | bPosition ) ) /* whole line is already inside */
- return 1;
- if ( aPosition & bPosition ) /* whole line is outside */
- return 0;
- if ( !aPosition ) /* a is inside */
- {
- actualX = bx;
- actualY = by;
- actualPosition = &bPosition;
- }
- else
- {
- actualX = ax;
- actualY = ay;
- actualPosition = &aPosition;
- }
- if ( *actualPosition & 1 ) /* actual point is above rect */
- {
- *actualX = *ax + ( *bx - *ax ) * ( ry1 - *ay ) / ( *by - *ay );
- *actualY = ry1;
- }
- else if ( *actualPosition & 2 ) /* actual point is bellow rect */
- {
- *actualX = *ax + ( *bx - *ax ) * ( ry2 - *ay ) / ( *by - *ay );
- *actualY = ry2;
- }
- else if ( *actualPosition & 4 ) /* actual point is on the right side of the rectangle */
- {
- *actualY = *ay + ( *by - *ay ) * ( rx2 - *ax ) / ( *bx - *ax );
- *actualX = rx2;
- }
- else /* actual point is on the left side of the rectangle */
- {
- *actualY = *ay + ( *by - *ay ) * ( rx1 - *ax ) / ( *bx - *ax );
- *actualX = rx1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement