Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <graphics.h>
- #define X1 20
- #define Y1 20
- #define X2 80
- #define Y2 110
- #define XMIN 40
- #define YMIN 40
- #define XMAX 100
- #define YMAX 90
- #define M 3
- int main(){
- int i, gd = DETECT,gm;
- initgraph(&gd, &gm, NULL);
- line(M*X1, M*Y1, M*X2, M*Y2);
- delay(5000);
- float p[4], q[4], r, t1 = 0, t2 = 1, xx1, yy1, xx2, yy2;
- p[0] = -(X2-X1); p[1] = (X2-X1);
- p[2] = -(Y2-Y1); p[3] = (Y2-Y1);
- q[0] = (X1-XMIN); q[1] = (XMAX-X1);
- q[2] = (Y1-YMIN); q[3] = (YMAX-Y1);
- for(i=0; i < 4; i++){
- r = q[i]/p[i];
- if(p[i] == 0 && q[i] < 0){
- printf("Line is completly outside.\n");
- return;
- }
- else if(r > t1 && p[i] < 0) t1 = r;
- else if(r < t2 && p[i] > 0) t2 = r;
- }
- cleardevice();
- if(t1 > t2){
- printf("Line is completly outside.\n");
- return 0;
- } else{
- printf("t1 = %f, t2 = %f\n", t1, t2);
- xx1 = X1 + t1 * (X2 - X1);
- yy1 = Y1 + t1 * (Y2 - Y1);
- xx2 = X1 + t2 * (X2 - X1);
- yy2 = Y1 + t2 * (Y2 - Y1);
- printf("(%.2f, %.2f) to (%.2f, %.2f)\n", xx1, yy1, xx2, yy2);
- rectangle(M*XMIN, M*YMIN, M*XMAX, M*YMAX);
- line(M*xx1, M*yy1, M*xx2, M*yy2);
- delay(5000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement