1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <Alib.h>
int ClipLine (w, x1, y1, x2, y2)
AWindow *w;
int *x1, *y1, *x2, *y2; {
register int dx, dy;
/*
* Top clip
*/
dx = *x2 - *x1;
dy = *y2 - *y1;
if (*y1 < w->clip.y1 || *y2 < w->clip.y1) {
if (*y1 < w->clip.y1)
if (*y2 < w->clip.y1)
return 1;
else {
*x1 = *x1 + dx * (w->clip.y1 - *y1) / dy;
*y1 = w->clip.y1;
}
else {
*x2 = *x1 + dx * (w->clip.y1 - *y1) / dy;
*y2 = w->clip.y1;
}
}
/*
* Bottom clip
*/
if (*y1 > w->clip.y2 || *y2 > w->clip.y2) {
if (*y1 > w->clip.y2)
if (*y2 > w->clip.y2)
return 1;
else {
*x1 = *x1 + dx * (w->clip.y2 - *y1) / dy;
*y1 = w->clip.y2;
}
else {
*x2 = *x1 + dx * (w->clip.y2 - *y1) / dy;
*y2 = w->clip.y2;
}
}
/*
* Left clip
*/
if (*x1 < w->clip.x1 || *x2 < w->clip.x1) {
if (*x1 < w->clip.x1)
if (*x2 < w->clip.x1)
return 1;
else {
*y1 = *y1 + dy * (w->clip.x1 - *x1) / dx;
*x1 = w->clip.x1;
}
else {
*y2 = *y1 + dy * (w->clip.x1 - *x1) / dx;
*x2 = w->clip.x1;
}
}
/*
* Right clip
*/
if (*x1 > w->clip.x2 || *x2 > w->clip.x2) {
if (*x1 > w->clip.x2)
if (*x2 > w->clip.x2)
return 1;
else {
*y1 = *y1 + dy * (w->clip.x2 - *x1) / dx;
*x1 = w->clip.x2;
}
else {
*y2 = *y1 + dy * (w->clip.x2 - *x1) / dx;
*x2 = w->clip.x2;
}
}
#ifdef DEBUG
if (*y1 < w->clip.y1 || *y1 > w->clip.y2
|| *y2 < w->clip.y1 || *y2 > w->clip.y2
|| *x1 < w->clip.x1 || *x1 > w->clip.x2
|| *x2 < w->clip.x1 || *x2 > w->clip.x2)
printf ("clip error\n");
#endif
return 0;
}
|