> WINDOWING TO VIEWPORT MAPPING ~ Online tutorial

WINDOWING TO VIEWPORT MAPPING




WINDOWING TO VIEWPORT MAPPING

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
  {
                    float sx,sy;
                    int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
                    int gd=DETECT,gm;
                    initgraph(&gd,&gm,"c:\\tc\\bgi");
                    printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n");
                    scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
                    cleardevice();
                    w1=5;
                    w2=5;
                    w3=635;
                    w4=465;
                    rectangle(w1,w2,w3,w4);
                    line(x1,y1,x2,y2);
                    line(x2,y2,x3,y3);
                    line(x3,y3,x1,y1);
                    getch();
                    v1=425;
                    v2=75;
                    v3=550;
                    v4=250;
                    sx=(float)(v3-v1)/(w3-w1);
                    sy=(float)(v4-v2)/(w4-w2);
                    rectangle(v1,v2,v3,v4);
                    x1=v1+floor(((float)(x1-w1)*sx)+.5);
                    x2=v1+floor(((float)(x2-w1)*sx)+.5);
                    x3=v1+floor(((float)(x3-w1)*sx)+.5);
                    y1=v2+floor(((float)(y1-w2)*sy)+.5);
                    y2=v2+floor(((float)(y2-w2)*sy)+.5);
                    y3=v2+floor(((float)(y3-w2)*sy)+.5);
                    line(x1,y1,x2,y2);
                    line(x2,y2,x3,y3);
                    line(x3,y3,x1,y1);
                    getch();
                    return 0;
 }



OUTPUT

Enter The Coordinate x1,y1,x2,y2,x3,y3

100
200
300
400
500
350





 





















































 









         
        





















COHEN-SUTHERLAND 2D LINE CLIPPING


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float cxl,cxr,cyt,cyb;
code(float ,float);
void clip(float ,float,float,float);
void  rect(float ,float,float,float);
 main()
    {
                    float x1,y1,x2,y2;
                    int g=0,d;
                    initgraph(&g,&d,"c:\\tc\\bin");
                    settextstyle(1,0,1);
                    outtextxy(40,15,"BEFORE CLIPPING");
                    printf("\n Please Enter Left,Bottom,Right,Top Of Clip Window");
                    scanf("%f%f%f%f",&cxl,&cyb,&cxr,&cyt);
rect(cxl,cyb,cxr,cyt);
                    getch();
printf("\n Enter  The Line Coordinate");
                    scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
                    getch();
                    cleardevice();
                    settextstyle(1,0,1);
                    outtextxy(40,15,"AFTER CLIPPING");
                    clip(x1,y1,x2,y2);
                    getch();
                    closegraph();
   }

void clip(float x1,float y1,float x2,float y2)
 {
                    int c,c1,c2;
                    float x,y;
                    c1=code(x1,y1);
                    c2=code(x2,y2);
                    getch();
         


                    while((c1!=0)||(c2!=0))
                     {
                             if((c1&c2)!=0)
                             goto out;
                             c=c1;
                              if(c==0)
                                       c=c2;
                             if((c&1)==1)
                                {
                                       y=y1+(y2-y1)*(cxl-x1);
                                       x=cxl;
                               }
                             else
                             if((c&2)==2)
                               {
                                       y=y1+(y2-y1)*(cxl-x1)/(x2-x1);
                                       x=cxr;
                                }
                             else
                             if((c&8)==8)
                                {
                                       x=x1+(x2-x1)*(cyb-y1)/(y2-y1);
                                       y=cyb;
                              }
                             else
                             if((c&4)==4)
                              {
                                       x=x1+(x2-x1)*(cyt-y1)/(y2-y1);
                                       y=cyt;
                               }
                            if(c==c1)
                             {
                                       x1=x;
                                       y1=y;
                                       c1=code(x,y);
                             }
                             else
                             {
                                       x2=x;
                                       y2=y;
                                       c2=code(x,y);
                             }
                     }
                   



out:
                              rect(cxl,cyb,cxr,cyt);
                             line(x1,y1,x2,y2);
    }

code(float x ,float y)
 {
                    int c=0;
                    if(x<cxl)                          c=1;
                    else
                              if(x>cxr)               c=2;
                    else
                              if(y<cyb)               c=c|8;
                    else
                              if(y>cyt)               c=c|4;
                    return c;
}


void rect(float xl,float yb,float xr,float yt)
  {
                    line(xl,yb,xr,yb);
                    line(xr,yb,xr,yt);
                    line(xr,yt,xl,yt);
                    line(xl,yt,xl,yb);
 }


















OUTPUT


BEFORE CLIPPING


Please Enter Left , Bottom , Right , Top Of The Clip window

200
200
400
400
         

Please Enter The Line Coordinates (X1, Y1, X2, Y2)

150
300
400
450





 






















AFTER CLIPPING







 

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: