Sutherland-Hodgeman Polygon Clipping

This program is for implementing Sutherland-Hodgeman Polygon Clipping . This is a part of Mumbai University MCA Colleges Computer Graphics CG MCA Sem 2


#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
void dda(int,int,int,int,int,int,int,int);
void dda(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
            float dx,dy;
            float steps,x=x1,y=y1;
            int i=0;
            dx=x2-x1;
            dy=y2-y1;
            if(abs(dx)>=abs(dy))
                        steps=abs(dx);
            else
                        steps=abs(dy);
            dx=dx/steps;
            dy=dy/steps;
            while(i++<=steps)
            {
            if(x>=xmin && x<=xmax && y>=ymin && y<=ymax)
            {
                        line(x,y,x2,y2);
                        return;
            }
                        x=x+dx;
                        y=y+dy;
            }
}
void main()
{
            int n,gd,gm,x1,x2,y1,y2,xRec,yRec,b1,b2,b3,b4,l,b,yMin,yMax,xMin,xMax;
            float m;
            int a[10][4],i,j, flag=0,in=0;
            gd=DETECT;
            gm=DETECT;
            initgraph(&gm,&gd,"C:\\TC\\BGI");

            printf("Enter the length and breadth of the clipping window:\n");
            scanf("%d%d",&l,&b);

            printf("Enter the starting co-ord of the rectangle\n");
            scanf("%d%d",&xRec,&yRec);
            clrscr();

            rectangle(xRec,yRec,xRec+l,yRec+b);
            getch();
            clrscr();


            printf("Enter the no of lines\n");
            scanf("%d",&n);

            for(i=0;i<n;i++)
            {
                        printf("Enter the co-ord of line %d\n",i+1);
                        for(j=0;j<2;j++)
                        {
                                    scanf("%d",&a[i][j]);
                        }
                        a[i][2]=a[0][0];
                        a[i][3]=a[0][1];
                        if(i!=0)
                        {
                               a[i-1][2]=a[i][0];
                               a[i-1][3]=a[i][1];
                        }
            }
            clrscr();
            rectangle(xRec,yRec,xRec+l,yRec+b);
            for(i=0;i<n;i++)
            {
                        line(a[i][0],a[i][1],a[i][2],a[i][3]);
            }
            getch();
            clrscr();
            rectangle(xRec,yRec,xRec+l,yRec+b);

            xMin=xRec;
            yMin=yRec;
            xMax=xRec+l;
            yMax=yRec+b;

            for(i=0;i<n;i++)
            {
                        flag=0;
                        x1=a[i][0];
                        x2=a[i][2];
                        y1=a[i][1];
                        y2=a[i][3];

                        if(x1>=xMin && x1<=xMax && y1>=yMin && y1<=yMax)
                                    flag++;
                        if(x2>=xMin && x2<=xMax && y2>=yMin && y2<=yMax)
                                    flag++;


                        switch(flag)
                        {
                                    case 0:
                                                break;

case 1: if(x2>=xMin && x2<=xMax && y2>=yMin &&    y2<=yMax)
                                                {
                                                            dda(x1,y1,x2,y2,xMin,yMin,xMax,yMax);

                                                }
                                                else
                                                {
                                                            dda(x2,y2,x1,y1,xMin,yMin,xMax,yMax);

                                                }
                                                break;
                                    case 2: line(x1,y1,x2,y2);
                                                break;
                        }
            }

            getch();

            closegraph();
}



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

2D Transformations 1 program

This program is for implementing Composite 2D Transformations. This is a part of Mumbai University MCA Colleges Computer Graphics CG MCA Sem 2


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define pi 3.14
int p1[3][3]={0,0,0,0,0,0,0,0,0};
int p[3][3]={0,0,0,0,0,0,1,1,1};
void translate(int p[3][3],int,int);
void scaling(int p[3][3],int,int);
void rotate(int p[3][3],double);
void main()
{
            int gd,gm,x1,x2,y1,y2,i,j,choice;
            int tx,ty,sx,sy;
            double theta;
            clrscr();
            detectgraph(&gd,&gm);
            initgraph(&gd,&gm,"c:\\tc\\bgi");
            printf("Enter the co-ordinates of the line\n");
            printf("Values of x1 and y1\n");
            scanf("%d%d",&p[0][0],&p[1][0]);
            printf("Values of x2 and y2\n");
            scanf("%d%d",&p[0][1],&p[1][1]);
            line(p[0][0],p[1][0],p[0][1],p[1][1]);
            while(1)
            {
                        printf("Enter your choice\n");
                        printf("1. Translation\n2. Rotation\n3. Scaling\n4. Exit\n");
                        scanf("%d",&choice);
                        switch(choice)
                        {
                                    case 1:
                                                printf("Enter value of tx and ty\n");
                                                scanf("%d%d",&tx,&ty);
                                                translate(p,tx,ty);
                                                printf("%d %d %d %d",p[0][0],p[0][1],p[1][0],p[1][1]);
                                                line(p[0][0],p[1][0],p[0][1],p[1][1]);
                                                break;
                                    case 2:
                                                printf("Enter values of angle of rotation\n");
                                                scanf("%lf",&theta);
                                                rotate(p,theta);
                                                line(p[0][0],p[1][0],p[0][1],p[1][1]);
                                                break;
                                    case 3:
                                                printf("Enter values of scaling factors\n");
                                                scanf("%d%d",&sx,&sy);
                                                scaling(p,sx,sy);
                                                line(p[0][0],p[1][0],p[0][1],p[1][1]);
                                                break;
                                    case 4:
                                                exit(0);
                                    default:
                                                printf("You entered wrong choice\n");
                        }
            }
}
void copy(int p1[3][3])
{
            int i,j;
            for(i=0;i<3;i++)
            {
                        for(j=0;j<3;j++)
                        {
                                    p[i][j]=p1[i][j];
                        }
            }
}
//Scaling
void scaling(int p[3][3],int sx,int sy)
{
            int i,j,k;
            int s[3][3]={0,0,0,0,0,0,0,0,1};
            s[0][0]=sx;
            s[1][1]=sy;
            for(i=0;i<3;i++)
            {
                        for(j=0;j<3;j++)
                        {
                                    p1[i][j]=0;
                                    for(k=0;k<3;k++)
                                    {
                                                p1[i][j]+=s[i][k]*p[k][j];
                                    }
                        }
            }
            copy(p1);
}


//Translation
void translate(int p[3][3],int tx,int ty)
{
            int i,j,k;
            int t[3][3]={1,0,0,0,1,0,0,0,1};
            t[0][2]=tx;
            t[1][2]=ty;
            for(i=0;i<3;i++)
            {
                        for(j=0;j<3;j++)
                        {
                                    p1[i][j]=0;
                                    for(k=0;k<3;k++)
                                    {
                                                p1[i][j]+=t[i][k]*p[k][j];
                                    }
                                    printf("%d\t",p1[i][j]);
                        }
                        printf("\n");
            }
            copy(p1);
}
//Rotation
void rotate(int p[3][3],double theta)
{
            int i,j,k;
            double st,ct;
            double r[3][3]={0,0,0,0,0,0,0,0,1};
            st=sin((theta*pi)/180);
            ct=cos((theta*pi)/180);
            r[0][0]=r[1][1]=ct;
            r[0][1]=-st;
            r[1][0]=st;
            for(i=0;i<3;i++)
            {
                        for(j=0;j<3;j++)
                        {
                                    p1[i][j]=0;
                                    for(k=0;k<3;k++)
                                    {
                                                p1[i][j]+=r[i][k]*p[k][j];
                                    }
                        }
            }
            copy(p1);
}



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Mid Point Ellipse Drawing Algorithm

This program is for implementing Mid Point Ellipse Drawing Algorithm. This is a part of Mumbai University MCA Colleges Computer Graphics CG MCA Sem 2


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

void plot(int xc,int yc,int x,int y)
{
            putpixel(x+xc,y+yc,1);
            putpixel(x+xc,-y+yc,1);
            putpixel(-x+xc,y+yc,1);
            putpixel(-x+xc,-y+yc,1);
}

void myellipse(int xc,int yc,int rx,int ry)
{
            long rx2,ry2,px,py,tworx2,twory2,p;
            float x,y;
            x=0;
            y=ry;
            rx2=rx*rx;
            ry2=ry*ry;
            tworx2=2*rx2;
            twory2=2*ry2;
            px=0;
            py=tworx2*y;
//Printing the initial point
            plot(xc,yc,x,y);
            p=(int)(ry2-(rx2*ry)+(0.25*rx2));
            while(px<py)
            {
                        x++;
                        px+=twory2;
                        if(p<0)
                                    p+=px+ry2;
                        else
                        {
                                    y=y-1;
                                    py-=tworx2;
                                    p+=ry2+px-py;
                        }
                        plot(xc,yc,x,y);
            }
            p=(int)((ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2));
            while(y>0)
            {
                        y--;
                        py-=tworx2;
                        if(p>0)
                                    p+=rx2-py;
                        else
                        {
                                    x++;
                                    px+=twory2;
                                    p+=rx2-py+px;
                        }
                        plot(xc,yc,x,y);
            }
}

void main()
{
            int gd,gm;
            int xc,yc,rx,ry;
            clrscr();
            gd=DETECT;
            detectgraph(&gd,&gm);
            printf("Enter values of centre of ellipse\n");
            scanf("%d%d",&xc,&yc);
            printf("Enter the x radius and y radius\n");
            scanf("%d%d",&rx,&ry);
            initgraph(&gd,&gm,"c:\\tc\\bgi");
            myellipse(xc,yc,rx,ry);
            getch();
}



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Character Generation Computer Graphics

This program is for Character Generation. This is a part of Mumbai University MCA Colleges Computer Graphics CG MCA Sem 2

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
            int gd,gm,i,j;

            /* Save character map of letter A */
           // You can make your changes in the below array
            int a[13][9] = {
                                    { 0, 0, 0, 0, 1, 0, 0, 0, 0},
                                    { 0, 0, 0, 1, 0, 1, 0, 0, 0},
                                    { 0, 0, 1, 0, 0, 0, 1, 0, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                                    { 0, 1, 1, 1, 1, 1, 1, 1, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                                    { 0, 1, 0, 0, 0, 0, 0, 1, 0},
                              };

            /* Initialise graphics mode */
            detectgraph(&gd,&gm);
            initgraph(&gd,&gm,"c:\\tc\\bgi");
           
            for(i=0;i<13;i++)
            {
                        for(j=0;j<9;j++)
                        {
                                    putpixel(200+j,200+i,15*a[i][j]);
                        }
            }
            getch();
            closegraph();
}





Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Liang-Barsky Algorithm CG

This program is for implementing Liang Barsky Algorithm. This is a part of Mumbai University MCA Colleges Computer Graphics CG MCA Sem 2

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

main()
{
 int i, gm,gd;
 int x1, x2, y1, y2, xmin, xmax, ymin, ymax, xx1, xx2, yy1, yy2;
 float t1, t2, p[4], q[4], temp;
 detectgraph(&gd,&gm);
 initgraph(&gd,&gm,"C:/tc/bgi/");
 xmin=150;
 xmax=450;
 ymin=100;
 ymax=400;
 rectangle(xmin,ymin,xmax,ymax);

 printf("Enter the starting point: ");
 scanf("%d%d",&x1,&y1);
 printf("Enter the ending point: ");
 scanf("%d%d",&x2,&y2);
 printf("Line before clipping:");
 line(x1,y1,x2,y2);
 getch();
 clrscr();

 printf("Line after clipping:");
 rectangle(xmin,ymin,xmax,ymax);

 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++)
 {
            if(p[i]==0)
            {
                        printf("\nLine is parallel to one of the clipping boundary");
                        if(q[i]>=0)
                        {
                                    if(i<2)
                                    {
                                         if(y1<ymin)
                                         {
                                                y1=ymin;
                                         }
                                         if(y2>ymax)
                                         {
                                                y2=ymax;
                                         }
                                         line(x1,y1,x2,y2);
                                    }
                                    if(i>1)
                                    {
                                         if(x1<xmin)
                                         {
                                                x1=xmin;
                                         }
                                         if(x2>xmax)
                                         {
                                                x2=xmax;
                                         }
                                         line(x1,y1,x2,y2);
                                    }
                        }
                        getch();
                        return(0);
             }
 }



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Cohen-Sutherland Algorithm

This program is for implementing Cohen-Sutherland Line Clipping. This is a part of Mumbai University MCA Colleges Computer Graphics CG MCA Sem 2

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>

typedef unsigned char BYTE;

BYTE pointcheck(int x, int y);
BYTE bit1check(int p);
BYTE bit2check(int p);
BYTE bit3check(int p);
BYTE bit4check(int p);
int endprog(void);

int main (void)
{
            int gd=VGA,gm=VGAHI;
            float x,y,xmin,ymin,xmax,ymax,xn1,xn2,yn1,yn2,x1,y1,x2,y2, m;
            int bit1,bit2,bit3,bit4, p1,p2, pointcount = 0;
            initgraph(&gd, &gm, "c:\\tc\\bgi");
            xmax = 540; ymax = 380; xmin = 100; ymin = 100;
            printf("Enter start point :");
            scanf("%f %f",&x1, &y1);
            printf("Enter end point :");
            scanf("%f %f",&x2, &y2);
            setcolor(1);
            rectangle(xmin,ymin,xmax,ymax);
            setcolor(2);
            line(x1,y1,x2,y2);
            m = (y2-y1)/(x2-x1);
            p1 = pointcheck(x1,y1);
            p2 = pointcheck(x2,y2);
            bit1=bit1check(p1);
            bit2=bit2check(p1);
            bit3=bit3check(p1);
            bit4=bit4check(p1);
            printf("Point p1 is %d = %d%d%d%d",p1,bit4,bit3,bit2,bit1);
            bit1=bit1check(p2);
            bit2=bit2check(p2);
            bit3=bit3check(p2);
            bit4=bit4check(p2);
            printf("\nPoint p2 is %d = %d%d%d%d",p2,bit4,bit3,bit2,bit1);
            if (bit1check(p1)&bit1check(p2)==1) endprog();
            if (bit2check(p1)&bit2check(p2)==1) endprog();
            if (bit3check(p1)&bit3check(p2)==1) endprog();
            if (bit4check(p1)&bit4check(p2)==1) endprog();
            if(p1 == 0 & p2 == 0)
            {
                        printf("\nLine lies within the window");
                        getch();
                        return 0;
            }
            else
                        printf("\nClipping......");
            if (pointcheck(x1,y1)==0)
            {
                        xn1 = x1; yn1 = y1;
                        pointcount = pointcount + 1;
                        p1 = p2;
            }
            if (bit1check(p1) == 1)
{
                        x = xmin;
                        y = y1 + m * (xmin - x1);
                        putpixel(x,y,3);
                        setcolor(3);
                        circle(x,y,4);
                        if (pointcount > 0)
                                    xn2 = x; yn2 = y;
                        else
                        {
                                    xn1 = x; yn1 = y;
                                    pointcount = pointcount +1;
                        }
                        p1 = p2;
            }
            if (bit2check(p1) == 1)
            {
                        x = xmax;
                        y = y1 + m * (xmax - x1);
                        putpixel(x,y,3);
                        setcolor(3);
                        circle(x,y,4);
                        if (pointcount > 0)
                                    xn2 = x; yn2 = y;
                        else
                        {
                                    xn1 = x; yn1 = y;
                                    pointcount = pointcount + 1;
                        }
                        p1 = p2;
            }
            if (bit3check(p1) == 1) //it crosses ymin
            {
                        y = ymin;
                        x = x1 + (ymin - y1)/m;
                        putpixel(x,y,3);
                        setcolor(3);
                        circle(x,y,4);
                        if (pointcount > 0)
                                    xn2 = x; yn2 = y;
                        else
                        {
                                    xn1 = x; yn1 = y;
                                    pointcount = pointcount + 1;
                        }
                        p1 = p2;
            }
            if (bit4check(p1) == 1) //it crosses ymax
            {
                        y = ymax;
                        x = x1 + (ymax - y1)/m;
                        putpixel(x,y,3);
                        setcolor(3);
                        circle(x,y,4);
                        if (pointcount > 0)
                                    xn2 = x; yn2 = y;
                        else
                        {
                                    xn1 = x; yn1 = y;
                                    pointcount = pointcount + 1;
                        }
            }
            getch();
            setcolor(0);
            line (x1,y1,x2,y2);
            setcolor(4);
            line (xn1,yn1,xn2,yn2);
            getch();
            closegraph();
}
int endprog(void)
{
            printf("\nLine lies outside the window");
            getch();
            closegraph();
            return 0;
}
BYTE pointcheck(int x, int y)
{
            int val=0x00;
            if ((x - 100) < 0) val = val|0x01;            //make bit 1 --> 1
            if ((540 - x) < 0) val = val|0x02;            //make bit 2 --> 1
            if ((y - 100) < 0) val = val|0x04;            //make bit 3 --> 1
            if ((380 - y) < 0) val = val|0x08;      //make bit 4 --> 1
            return val;
}
BYTE bit1check(int a)
{
            a = a & 1;        //AND operation with 0001, making 2,3,4 bit 0
            return a;
}
BYTE bit2check(int b)
{
            b = b & 2;        //AND with 0010 i.e making 1,3,4 bit 0
            b = b >> 1;     //Right shift by 1 bit i.e cutting last digit
            return b;
}
BYTE bit3check(int c)
{
            c = c & 4;        //AND with 0100 i.e making 1,2,4 bit 0
            c = c >> 2;       //Right shift 2 dights i.e cutting last two digit
            return c;
}
BYTE bit4check(int d)
{
            d = d >> 3;      //Right shift 3 dights i.e cutting last three digit
            return d;
}





Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Cohen-Sutherland Line Clipping


This program is for implementing Cohen-Sutherland Line Clipping. This is a part of Mumbai University MCA Colleges Computer Graphics CG MCA Sem 2
#include"stdio.h"

#include"conio.h"
#include"graphics.h"
#include"math.h"
void dda(int,int,int,int,int,int,int,int);
void dda(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
            float dx,dy;
            float steps,x=x1,y=y1;
            int i=0;
            dx=x2-x1;
            dy=y2-y1;
            if(abs(dx)>=abs(dy))
                        steps=abs(dx);
            else
                        steps=abs(dy);
            dx=dx/steps;
            dy=dy/steps;
            while(i++<=steps)
            {
            if(x>=xmin && x<=xmax && y>=ymin && y<=ymax)
            {
                        line(x,y,x2,y2);
                        return;
            }
                        x=x+dx;
                        y=y+dy;
            }
}
void main()
{
int n,gd,gm,x1,x2,y1,y2,xRec,yRec,b1,b2,b3,b4,l,b,yMin,yMax,xMin,xMax;
            float m;
int a[10][4],i,j flag=0,in=0;
            gd=DETECT;
            gm=DETECT;
            initgraph(&gm,&gd,"C:\\TC\\BGI");
            printf("Enter the length and breadth of the clipping window:\n");
            scanf("%d%d",&l,&b);

            printf("Enter the starting co-ord of the rectangle\n");
            scanf("%d%d",&xRec,&yRec);
            clrscr();

            rectangle(xRec,yRec,xRec+l,yRec+b);
            getch();
            clrscr();


            printf("Enter the no of lines\n");
            scanf("%d",&n);

            for(i=0;i<n;i++)
            {
                        printf("Enter the co-ord of line %d\n",i+1);
                        for(j=0;j<2;j++)
                        {
                                    scanf("%d",&a[i][j]);
                        }
                        a[i][2]=a[0][0];
                        a[i][3]=a[0][1];
                        if(i!=0)
                        {
                               a[i-1][2]=a[i][0];
                               a[i-1][3]=a[i][1];
                        }
            }
            clrscr();
            rectangle(xRec,yRec,xRec+l,yRec+b);
            for(i=0;i<n;i++)
            {
                        line(a[i][0],a[i][1],a[i][2],a[i][3]);
            }
            getch();
            clrscr();
            rectangle(xRec,yRec,xRec+l,yRec+b);

            xMin=xRec;
            yMin=yRec;
            xMax=xRec+l;
            yMax=yRec+b;

            for(i=0;i<n;i++)
            {
                        flag=0;
                        x1=a[i][0];
                        x2=a[i][2];
                        y1=a[i][1];
                        y2=a[i][3];

                        if(x1>=xMin && x1<=xMax && y1>=yMin && y1<=yMax)
                                    flag++;
                        if(x2>=xMin && x2<=xMax && y2>=yMin && y2<=yMax)
                                    flag++;


                        switch(flag)
                        {
                                    case 0:
                                                break;

                                    case 1:
if(x2>=xMin && x2<=xMax && y2>=yMin && y2<=yMax)
                                    {
                                                dda(x1,y1,x2,y2,xMin,yMin,xMax,yMax);
                                     }
                                    else
                                    {
                                                dda(x2,y2,x1,y1,xMin,yMin,xMax,yMax);
                                     }
                                    break;

                                    case 2: line(x1,y1,x2,y2);
                                    break;
                        }
                        getch();
            }
            getch();
            closegraph();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.