Friday, April 4, 2014

Design suitable data structures and implement pass-I of a two-pass assembler for 8 bit microprocessor/ pseudo-machine. Implementation should consist of a few instructions from each category and few assembler directives

Software Laboratory

You will require a.txt in current folder where your program is located 

contents of a.txt
    start 200
    mover areg,one
    mover breg,='4'
    mover creg,='5'
    mover breg,='6'
    mover creg,='7'
    add breg,areg
    sub breg,creg
    movem res,breg
    one dc 1
    res ds 5
    print res
    ltorg
        ='5'
        ='4'
loop equ one
    end
Main program code (ap1.c)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct mottab
{
    char name[8];
    int class;
    char info[4];
};

struct symtab
{
    char sym[8];
    int address;
    int len;
}st[20];

struct littab
{
    int lit;
    int address;
}lt[20];

int pt[5]={0};

struct ino
{
    int lc;
    int class1,no1;
    int class2,no2;
    int class3,no3;
}ic[50];

int ipt=0,ilt=0,ist=0,iic=0;

static struct mottab mot[30]={{"STOP",1,"00"},{"ADD",1,"01"},{"SUB",1,"02"},{"MULT",1,"03"},{"MOVEM",1,"04"},{"MOVER",1,"05"},{"COMP",1,"06"},{"BC",1,"07"},{"DIV",1,"08"},{"READ",1,"09"},{"PRINT",1,"10"},{"START",3,"01"},{"END",3,"02"},{"ORIGIN",3,"03"},{"EQU",3,"04"},{"LTORG",3,"05"},{"DS",2,"01"},{"DC",2,"02"},{"AREG",4,"01"},{"BREG",4,"02"},{"CREG",4,"03"},{"EQ",5,"01"},{"LT",5,"02"},{"GT",5,"03"},{"LE",5,"04"},{"GE",5,"05"},{"NE",5,"06"},{"ANY",5,"07"}};

int searchmot(char s[])
{
    int i;
    for(i=0;i<28;i++)
        if(strcmp(s,mot[i].name)==0)
            return(i);
    return(-1);
}
int searchsym(char s[])
{
    int i;
    for(i=0;i<ist;i++)
        if(strcmp(s,st[i].sym)==0)
            return(i);
    return(-1);
}
int searchlit(int sno)
{
    int i;
    for(i=0;i<ist;i++)
        if(sno==lt[i].lit)
            return(i);
    return(-1);
}
int insertsym(char s[],int addr,int len)
{
    strcpy(st[ist].sym,s);
    st[ist].address=addr;
    st[ist++].len=len;
    return(ist-1);
}
int insertlit(int lit,int addr)
{
    lt[ilt].lit=lit;
    lt[ilt++].address=addr;
    return(ilt-1);
}
void display();
int main()
{
    FILE * fp;
    char s1[10],s2[10],s3[10],s4[10],label[8],fname[8],buffer[50];
    int i,j,index,cnt,lc,flag=0;
    printf("\n\tEnter file name to be read : ");
    scanf("%s",fname);
    fp=fopen(fname,"r");
    strcpy(buffer,"");
    while(fgets(buffer,40,fp)!=0)
    {
        for(i=0;i<strlen(buffer);i++)
            if(!isalnum(buffer[i]))
                buffer[i]=' ';
            else
                buffer[i]=toupper(buffer[i]);
    jump:
        printf("\n\t*** %s ****",buffer);
        cnt=sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
        if(strcmp(s1,"END")==0)
        {
            ic[iic].lc=lc++;
            ic[iic].class1=mot[searchmot(s1)].class;
            ic[iic++].no1=atoi(mot[searchmot(s1)].info);
            break;
        }
        if(searchmot(s1)==-1)
        {
            printf("\n\tSymbol found %s ",s1);
            cnt=sscanf(buffer,"%s%s%s%s%s",label,s1,s2,s3,s4);
            index=searchsym(label);
            if(index==-1)
                insertsym(label,lc,1);
        }
        switch(mot[searchmot(s1)].class)
        {
            case 1:    printf("\n\tImperative statement");
                    ic[iic].lc=lc++;
                    ic[iic].class1=mot[searchmot(s1)].class;
                    ic[iic].no1=atoi(mot[searchmot(s1)].info);
                    if(cnt>1)
                    {
                        if(searchmot(s2)!=-1)        //if regester
                        {
                        ic[iic].class2=mot[searchmot(s2)].class;
                        ic[iic].no2=atoi(mot[searchmot(s2)].info);
                        }
                        else    //may be var (from dc or ds statement)
                        {
                            index=searchsym(s2);
                            if(index==-1)
                            {
                                index=insertsym(s2,0,0);
                            }
                            ic[iic].class2=7;        // 7 for variable
                            ic[iic].no2=index;
                        }
                    }
                    if(cnt>2)
                    {
                        if(searchmot(s3)!=-1)
                        {
                        ic[iic].class3=mot[searchmot(s3)].class;
                        ic[iic].no3=atoi(mot[searchmot(s3)].info);
                        }
                        else
                        {
                            if(isdigit(*s3))
                            {
                                printf("\n\t Literal found ");
                                index=searchlit(atoi(s3));
                                if(index==-1)
                                {
                                    index=insertlit(atoi(s3),0);
                                }
                                ic[iic].class3=8;        // 8 for literal
                                ic[iic].no3=index;
                            }
                            else
                            {   
                                index=searchsym(s3);
                                if(index==-1)
                                {
                                    index=insertsym(s3,0,0);
                                }
                                ic[iic].class3=7;        // 7 for variable
                                ic[iic].no3=index;
                            }
                        }
                    }
                    iic++;
                break;
            case 2:    printf("\n\tDeclaration statement");
                    if(strcmp(s1,"DC")==0)
                    {
                        ic[iic].lc=lc;
                        ic[iic].class1=mot[searchmot(s1)].class;
                        ic[iic].no1=atoi(mot[searchmot(s1)].info);

                        ic[iic].class2=6;            //6 for constant
                        ic[iic++].no2=atoi(s2);
                        st[searchsym(label)].address=lc++;    //back patching
                            st[searchsym(label)].len=1;
                    }
                    if(strcmp(s1,"DS")==0)
                    {
                        ic[iic].lc=lc;
                        ic[iic].class1=mot[searchmot(s1)].class;
                        ic[iic].no1=atoi(mot[searchmot(s1)].info);

                        ic[iic].class2=6;            //6 for constant
                        ic[iic++].no2=atoi(s2);

                        st[searchsym(label)].address=lc;    // back patching
                        st[searchsym(label)].len=atoi(s2);
                        lc+=atoi(s2);
                    }
                break;
            case 3:    printf("\n\tAssembler Directive ");
                if(strcmp(s1,"START")==0 || strcmp(s1,"ORIGIN")==0)
                {
                    lc=atoi(s2);
                    ic[iic].lc=lc;
                    ic[iic].class1=mot[searchmot(s1)].class;
                    ic[iic].no1=atoi(mot[searchmot(s1)].info);
                    ic[iic].class2=6;
                    ic[iic++].no2=atoi(s2);
                }
                if(strcmp(s1,"LTORG")==0)
                {
                    printf("processing ltorg....");
                    ic[iic].lc=lc++;
                    ic[iic].class1=mot[searchmot(s1)].class;
                    ic[iic++].no1=atoi(mot[searchmot(s1)].info);
                    while(1)
                    {
                        fgets(buffer,40,fp);
                        for(i=0;i<strlen(buffer);i++)
                            if(!isalnum(buffer[i]))
                                buffer[i]=' ';
                            else
                                buffer[i]=toupper(buffer[i]);

                        printf("\n\t$$ %s $$",buffer);
                        cnt=sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
                        if(isdigit(*s1))
                        {
                            lt[searchlit(atoi(s1))].address=lc++;
                            pt[ipt]++;
                        }
                        else
                        {
                            ipt++;
                            goto jump;
                            break;
                        }
                    }
                }
                if(strcmp(s1,"EQU")==0)
                {
                    index=searchsym(label);
                    st[index].address=st[searchsym(s2)].address;

                    ic[iic].lc=lc++;
                    ic[iic].class1=mot[searchmot(s1)].class;
                    ic[iic++].no1=atoi(mot[searchmot(s1)].info);
                }
                break;   
       
        }
        strcpy(buffer,"");
    }
//**************Lit process ***********
    for(i=pt[ipt-1];i<ilt;i++)
    {
        lt[i].address=lc++;
        pt[ipt]++;
        flag=1;
    }
    if(flag==1)
    {
        pt[ipt]+=pt[ipt-1];
        ipt++;
    }
    display();
    return(0);
}
void display()
{
    int i;
    FILE * fp;
    fp = fopen("st.txt","w");
    printf("\n\t **************Sym tab ***************** ");
    for(i=0;i<ist;i++)
    {
        printf("\n\t%s\t%d\t%d",st[i].sym,st[i].address,st[i].len);
        fprintf(fp,"%s\t%d\t%d\n",st[i].sym,st[i].address,st[i].len);
    }
    close(fp);
    fp = fopen("lt.txt","w");
    printf("\n\t **************Lit tab ******************");
    for(i=0;i<ilt;i++)
    {
        printf("\n\t%d\t%d",lt[i].lit,lt[i].address);
        fprintf(fp,"%d\t%d\n",lt[i].lit,lt[i].address);
    }
    close(fp);
    printf("\n\t **************Pool tab ******************");
    for(i=0;i<ipt;i++)
    {
        printf("\n\t%d",pt[i]);
    }
    printf("\n\t **************IC tab ****************** ");
    fp=fopen("ic.txt","w");
    for(i=0;i<iic;i++)
    {
        if(ic[i].class1==1)
        {
            printf("\n%d)\t(IS,%d)",ic[i].lc,ic[i].no1);
        }
        else
        {
            if(ic[i].class1==2)
            {
                printf("\n%d)\t(DL,%d)",ic[i].lc,ic[i].no1);
            }
            else
            {
                printf("\n%d)\t(AD,%d)",ic[i].lc,ic[i].no1);
            }
        }
       
// *********class 2******************
        if(ic[i].class2==4)
            printf("\t(Reg,%d)",ic[i].no2);
        else
            if(ic[i].class2==7)
                    printf("\t(S,%d)",ic[i].no2);
            else
                if(ic[i].class2==6)
                    printf("\t(C,%d)",ic[i].no2);
//*****************class 3*************
        if(ic[i].class3==4)
            printf("\t(Reg,%d)",ic[i].no3);
        else
            if(ic[i].class3==6)
                printf("\t(C,%d)",ic[i].no3);
            else   
                if(ic[i].class3==7)
                    printf("\t(Sym,%d)",ic[i].no3);
                else
                    if(ic[i].class3==8)
                        printf("\t(Lit,%d)",ic[i].no3);

        fprintf(fp,"%d\t%d\t%d\t%d\t%d\t%d\t%d\n",ic[i].lc,ic[i].no1,ic[i].class1,ic[i].no2,ic[i].class2,ic[i].no3,ic[i].class3);           
    }
    close(fp);
}
Execute in this way ->
  1) Go to that folder through terminal
  2) Compile that .c file (gcc file_name.c)
  3) run it . (./a.out)
Output :
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/ass pass1# gcc ap1.c
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/ass pass1# ./a.out

    Enter file name to be read : a.txt

    ***  START 200  ****
    Assembler Directive
    ***  MOVER AREG ONE  ****
    Imperative statement
    ***  MOVER BREG   4   ****
    Imperative statement
     Literal found
    ***  MOVER CREG   5   ****
    Imperative statement
     Literal found
    ***  MOVER BREG   6   ****
    Imperative statement
     Literal found
    ***  MOVER CREG   7   ****
    Imperative statement
     Literal found
    ***  ADD BREG AREG  ****
    Imperative statement
    ***  SUB BREG CREG  ****
    Imperative statement
    ***  MOVEM RES BREG  ****
    Imperative statement
    ***  ONE DC 1  ****
    Symbol found ONE
    Declaration statement
    ***  RES DS 5  ****
    Symbol found RES
    Declaration statement
    ***  PRINT RES  ****
    Imperative statement
    ***  LTORG  ****
    Assembler Directive processing ltorg....
    $$     5   $$
    $$     4   $$
    $$ LOOP EQU ONE  $$
    *** LOOP EQU ONE  ****
    Symbol found LOOP
    Assembler Directive
    ***  END  ****
     **************Sym tab *****************
    ONE    208    1
    RES    209    5
    LOOP    208    1
     **************Lit tab ******************
    4    217
    5    216
    6    220
    7    221
     **************Pool tab ******************
    2
    4
     **************IC tab ******************
200)    (AD,1)    (C,200)
200)    (IS,5)    (Reg,1)    (Sym,0)
201)    (IS,5)    (Reg,2)    (Lit,0)
202)    (IS,5)    (Reg,3)    (Lit,1)
203)    (IS,5)    (Reg,2)    (Lit,2)
204)    (IS,5)    (Reg,3)    (Lit,3)
205)    (IS,1)    (Reg,2)    (Reg,1)
206)    (IS,2)    (Reg,2)    (Reg,3)
207)    (IS,4)    (S,1)    (Reg,2)
208)    (DL,2)    (C,1)
209)    (DL,1)    (C,5)
214)    (IS,10)    (S,1)
215)    (AD,5)
218)    (AD,4)
219)    (AD,2)root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/ass pass1#

#################################################

After this execution you will see 1)ic.txt ie intermediate code 2) st.txt ie symbol table and 3) lt.txt ie literal table
ic.txt
200    1    3    200    6    0    0
200    5    1    1    4    0    7
201    5    1    2    4    0    8
202    5    1    3    4    1    8
203    5    1    2    4    2    8
204    5    1    3    4    3    8
205    1    1    2    4    1    4
206    2    1    2    4    3    4
207    4    1    1    7    2    4
208    2    2    1    6    0    0
209    1    2    5    6    0    0
214    10    1    1    7    0    0
215    5    3    0    0    0    0
218    4    3    0    0    0    0
219    2    3    0    0    0    0
st.txt
ONE    208    1
RES    209    5
LOOP    208    1
lt.txt
4    217
5    216
6    220
7    221
Above files will require for assembler  pass 2...

Tuesday, March 25, 2014

C++ program to implement 2D Transformations

Description :Transformations are used to position objects, to shape objects, to change viewing positions, and even to change how something is viewed.
There are 4 main types of transformations that one can perform in 2 dimensions:-translations,scaling,rotation,shearing.


Code :
#include<iostream.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>

#include<graphics.h>
#include<math.h>
class POLYGON
{
private:
int p[10][10],Result[10][10],X[10][10];
public:
int accept_poly(int [][10]);
void draw_poly(int [][10],int);
void draw_polyfloat(float [][10],int);
void matmult(int [][10],int [][10],int,int,int,int [][10]);
void matmultfloat(float [][10],int [][10],int,int,int,float [][10]);
void shearing(int [][10],int);
void scaling(int [][10],int);
void rotation(int [][10],int);
void translation(int [][10],int);
void reflection(int [][10],int);
};
int POLYGON :: accept_poly(int p[][10])
{
int i,n;
cout << “\n\n\t\tEnter no.of vertices:”;
cin >> n;
for(i=0;i<n;i++)
{
cout << “\n\n\t\tEnter (x,y)Co-ordinate of point P” << i << “: “;
cin >> p[0][i] >> p[1][i];
p[2][i] = 1;
}
p[0][n] = p[0][0];
p[1][n] = p[1][0];
p[2][n] = 1;
for(i=0;i<n;i++)
{
cout<<”\n”;
for(int j=0;j<3;j++)
{
cout<<p[i][j]<<”\t”;
}
}
getch();
return n;
}
void POLYGON :: draw_poly(int p[][10], int n)
{
int i,gd = DETECT,gm;
initgraph(&gd,&gm,”e:\\TC\\BGI”);
line(320,0,320,480);
line(0,240,640,240);
for(i=0;i<n;i++)
{
if(i!=n-1)
line(p[0][i]+320, -p[1][i]+240, p[0][i+1]+320, -p[1][i+1]+240);
else
line(p[0][i]+320, -p[1][i]+240, p[0][0]+320, -p[1][0]+240);
}
getch();
closegraph();
}
void POLYGON :: draw_polyfloat(float p[][10], int n)
{
int i,gd = DETECT,gm;
initgraph(&gd,&gm,”e:\\TC\\BGI”);
line(320,0,320,480);
line(0,240,640,240);
for(i=0;i<n;i++)
{
if(i!=n-1)
line(int(p[0][i])+320, -int(p[1][i])+240, int(p[0][i+1])+320, -int(p[1][i+1])+240);
else
line(int(p[0][i])+320, -int(p[1][i])+240, int(p[0][0])+320, -int(p[1][0])+240);
}
getch();
closegraph();
}
void POLYGON :: matmult(int mat1[][10],int mat2[][10],int r1,int c1,int c2,int mat3[][10])
{
int i,j,k;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
mat3[i][j] = 0;
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)
mat3[i][j] = mat3[i][j]+(mat1[i][k] * mat2[k][j]);
mat3[c2][0] = mat3[0][0];
mat3[c2][1] = mat3[0][1];
mat3[c2][2] = mat3[0][2];
}
void POLYGON :: matmultfloat(float mat1[][10],int mat2[][10],int r1,int c1,int c2,float mat3[][10])
{
int i,j,k;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
mat3[i][j] = 0;
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)
mat3[i][j] = mat3[i][j]+(mat1[i][k] * mat2[k][j]);
mat3[c2][0] = mat3[0][0];
mat3[c2][1] = mat3[0][1];
mat3[c2][2] = mat3[0][2];
}
void POLYGON :: translation(int p[10][10],int n)
{
int tx,ty,i,j;
cout << “\n\n\t\tEnter X-Translation tx: “;
cin >> tx;
cout << “\n\n\t\tEnter Y-Translation ty: “;
cin >> ty;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
X[i][j] = 0;
X[0][0] = X[1][1] = X[2][2] = 1;
X[0][2] = tx;
X[1][2] = ty;
matmult(X,p,3,3,n,Result);
cout << “\n\n\t\tPolygon after Translation…”;
draw_poly(Result,n);
}
void POLYGON :: reflection(int p[][10],int n)
{
int type,i,j;
cout << “\n\n **** Reflection Types ****”;
cout << “\n\n\t\t1.About X-Axis \n\n\t\t2.About Y-Axis \n\n\t\t3.About Origin \
\n\n\t\t4.About Line y = x \n\n\t\t5.About Line y = -x \
\n\n\t\tEnter your choice(1-5): “;
cin >> type;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(i == j)
X[i][j] = 1;
else
X[i][j] = 0;
}
switch(type)
{
case 1:
X[1][1] = -1;
break;
case 2:
X[0][0] = -1;
break;
case 3:
X[1][1] = -1;
X[0][0] = -1;
break;
case 4:
X[1][1] = 0;
X[0][0] = 0;
X[1][0] = 1;
X[0][1] = 1;
break;
case 5:
X[1][1] = 0;
X[0][0] = 0;
X[1][0] = -1;
X[0][1] = -1;
break;
}
matmult(X,p,3,3,n,Result);
cout << “\n\n\t\tPolygon after Reflection…”;
draw_poly(Result,n);
}
void POLYGON :: rotation(int p[][10],int n)
{
float type,rotate[10][10],result[10][10],i,j,Ang,Sinang,Cosang;
cout << “\n\n\t\tEnter the angle of rotation in degrees: “;
cin >> Ang;
cout << “\n\n **** Rotation Types ****”;
cout << “\n\n\t\t1.Clockwise Rotation \n\n\t\t2.Anti-Clockwise Rotation “;
cout << “\n\n\t\tEnter your choice(1-2): “;
cin >> type;
Ang = (Ang * 6.2832)/360;
Sinang = sin(Ang);
Cosang = cos(Ang);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
rotate[i][j] = 0;
rotate[0][0] = rotate[1][1] = Cosang;
rotate[0][1] = rotate[1][0] = Sinang;
rotate[2][2] = 1;
if(type == 1)
rotate[1][0] = -Sinang;
else
rotate[0][1] = Sinang;
matmultfloat(rotate,p,3,3,n,result);
cout << “\n\n\t\tPolygon after Rotation…”;
draw_polyfloat(result,n);
}
void POLYGON :: scaling(int p[][10],int n)
{
float Sx,Sy,result[10][10],scale[10][10],i,j;
cout << “\n\n\t\tEnter X-Scaling Sx: “;
cin >> Sx;
cout << “\n\n\t\tEnter Y-Scaling Sy: “;
cin >> Sy;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scale[i][j] = 0;
scale[0][0] = Sx;
scale[1][1] = Sy;
scale[2][2] = 1;
matmultfloat(scale,p,3,3,n,result);
cout << “\n\n\t\tPolygon after Scaling…”;
draw_polyfloat(result,n);
}
void POLYGON :: shearing(int p[][10],int n)
{
int Sx,Sy,type,i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(i == j)
X[i][j] = 1;
else
X[i][j] = 0;
}
cout << “\n\n **** Shearing Types ****”;
cout << “\n\n\t\t1.X-Direction Shear \n\n\t\t2.Y-Direction Shear “;
cout << “\n\n\t\tEnter your choice(1-2): “;
cin >> type;
if(type == 1)
{
cout << “\n\n\t\tEnter X-Shear Sx: “;
cin >> Sx;
X[0][1] = Sx;
}
else
{
cout << “\n\n\t\tEnter Y-Shear Sy: “;
cin >> Sy;
X[1][0] = Sy;
}
matmult(X,p,3,3,n,Result);
cout << “\n\n\t\tPolygon after Shearing…”;
draw_poly(Result,n);
}
int menu()
{
int ch;
clrscr();
cout << “\n\n **** 2-D TRANSFORMATION ****”;
cout << “\n\n\t\t1.Translation \n\n\t\t2.Scaling \n\n\t\t3.Rotation \
\
n\n\t\t4.Reflection \n\n\t\t5.Shearing \n\n\t\t6.Exit”;
cout<<”\n\n\tEnter your choice(1-6): “;
cin >> ch;
return ch;
}
void main()
{
int ch,n,p[10][10];
POLYGON p1;
clrscr();
cout << “\n\n **** 2-D TRANSFORMATION ****”;
n = p1.accept_poly(p);
clrscr();
cout << “\n\n\t\tOriginal Polygon …”;
p1.draw_poly(p,n);
do
{
ch = menu();
switch(ch)
{
case 1:
p1.translation(p,n);
break;
case 2:
p1.scaling(p,n);
break;
case 3:
p1.rotation(p,n);
break;
case 4:
p1.reflection(p,n);
break;
case 5:
p1.shearing(p,n);
break;
case 6:
exit(0);
}
}while(ch!=6);
getch();
}

A program for filling a polygon using scan-fill method.

Description :For each scan-line:
– Locate the intersection of the scan-line with the edges
– Sort the intersection points from left to right.
– Draw the interiors intersection points pairwise.


Code :

#include <conio.h>
#include <iostream.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
//Declaration of class point
class point
{
public:
int x,y;
};
//class for polygon
class poly
{
private:
point p[20];
int inter[20],x,y;
int v,xmin,ymin,xmax,ymax;
public:
int c;
void read();
void calcs();
void display();
void ints(float);
void intsx(float);
void sort(int);
void sort(int,int);
};
//DEFINE READ FUNCTION
void poly::read()
{
cout<<”\n\t SCAN_FILL ALGORITHM”;
cout<<”\n Enter the no of vertices of polygon:”;
cin>>v;
if(v>2)
{
for(int i=0;i<v; i++) //ACCEPT THE VERTICES
{
cout<<”\nEnter the co-ordinate no. – “<<i+1<<” : “;
cout<<”\n\tx”<<(i+1)<<”=”;
cin>>p[i].x;
cout<<”\n\ty”<<(i+1)<<”=”;
cin>>p[i].y;
}
p[i].x=p[0].x;
p[i].y=p[0].y;
xmin=xmax=p[0].x;
ymin=ymax=p[0].y;
}
else
cout<<”\n Enter valid no. of vertices.”;
}
//FUNCTION FOR FINDING
void poly::calcs()
{ //MAX,MIN
for(int i=0;i<v;i++)
{
if(xmin>p[i].x)
xmin=p[i].x;
if(xmax<p[i].x)
xmax=p[i].x;
if(ymin>p[i].y)
ymin=p[i].y;
if(ymax<p[i].y)
ymax=p[i].y;
}
}
//DISPLAY FUNCTION
void poly::display()
{
int ch1;
char ch=’y';
float s,s2;
do
{
cout<<”\n\nMENU:”;
cout<<”\n\n\t1 . Solid Fill “;
cout<<”\n\n\t2 . Exit “;
cout<<”\n\nEnter your choice:”;
cin>>ch1;
switch(ch1)
{
case 1:
s=ymin+0.5;
delay(10);
cleardevice();
while(s<=ymax)
{
ints(s);
sort(s,1);
s++;
}
break;
case 2:
exit(0);
}
cout<<”\n\nDo you want to continue?: “;
cin>>ch;
}while(ch==’y’ || ch==’Y');
}
void poly::ints(float z) //DEFINE FUNCTION INTS
{
int x1,x2,y1,y2,temp;
c=0;
for(int i=0;i<v;i++)
{
x1=p[i].x;
y1=p[i].y;
x2=p[i+1].x;
y2=p[i+1].y;
if(y2<y1)
{
temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
}
if(z<=y2&&z>=y1)
{
if((y1-y2)==0)
x=x1;
else
{
x=((x2-x1)*(z-y1))/(y2-y1);
x=x+x1;
}
if(x<=xmax && x>=xmin)
inter[c++]=x;
}
}
}
void poly::sort(int z,int w) //SORT FUNCTION
{
int temp,j;
for(int i=0;i<c;i++)
{
for(j=i+1;j<c;j++)
{
if(inter[i]>inter[j])
{
temp=inter[i];
inter[i]=inter[j];
inter[j]=temp;
}
}
}
if(w==1)
{
for(i=0;i<v;i++)
{
line(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
}
delay(10);
for(i=0; i<c;i+=2)
{
delay(10);
line(inter[i],z,inter[i+1],z);
}
}
}
void main() //START OF MAIN
{
int gd=DETECT,gm,cl;
initgraph(&gd,&gm,”e:\\tc\\BGI”);
cleardevice();
poly x;
x.read();
x.calcs();
cleardevice();
cout<<”\n\tEnter the colour u want:(0-15)–>”; //Selecting colour
cin>>cl;
cleardevice();
setcolor(cl);
x.display();
closegraph(); //CLOSE OF GRAPH
cout<<”\n\nC=”<<x.c;
getch();
}

PROGRAM TO DRAW LINE & CIRCLE USING DDA & BRESENHAM’S ALGORITHM USING C++

Bresenham’s algorithm: It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction ,bit shifting all of which are very cheap operations in standard computer architectures.
DDA: Digital Differential Analyzer is a scan conversion line algorithm based on calculating either dy or dx. We sample the line at unit intervals in one coordinate & determine corresponding integer values nearest to the line path for the other coordinate. DDAs are used for rasterization of lines, triangles and polygons.
  
Code :
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
#include<stdio.h>
#include<process.h>
class draw
{
float x,y,x1,y1,x2,y2,dx,dy,xinc,yinc,g,temp,m;
int steps;
public:
void ddaline();
void bline();
void ddacircle();
void bcircle();
};
void draw::ddaline()
{
int i;
cout<<”\n Please enter x1: “;
cin>>x1;
cout<<”\n Please enter y1: “;
cin>>y1;
cout<<”\n Please enter x2: “;
cin>>x2;
cout<<”\n Please enter y2: “;
cin>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
clrscr();
if(dx>dy)
steps=dx;
else
steps=dy;
x=x1;
y=y1;
xinc=dx/steps;
yinc=dy/steps;
for(i=0;i<steps;i++)
{
putpixel(x+5.0,y+5.0,RED);
delay(10);
x=x+xinc;
y=y+yinc;
}
}
void draw::bline()
{
int i;
cout<<”\n Please enter x1: “;
cin>>x1;
cout<<”\n Please enter y1: “;
cin>>y1;
cout<<”\n Please enter x2: “;
cin>>x2;
cout<<”\n Please enter y2: “;
cin>>y2;
dx=x2-x1;
dy=y2-y1;
g=(2*dy)-dx;
x=x1;
y=y1;
m=dy/dx;
x=x1;
y=y1;
if(m>1)
{
temp=x;
x=y;
y=temp;
}
for(i=0;i<dx;i++)
{
putpixel(x,y,19);
delay(10);
x++;
if(g>0)
{
y++;
g=g+(2*dy)-(2*dx);
}
else
g=g+(2*dy);
}
}
void draw::ddacircle()
{
float rad,start_x=rad,start_y=0,e;
int p,n=0;
cout<<”\n\nEnter the radius of circle: “;
cin>>rad;
x1=0;
x2=rad;
x1=start_x;
y1=start_y;
do
{
p=pow(2,n);
n++;
}while(p<rad);
e=1/(pow(2,n-1));
do
{
x2=x1+e*y1;
y2=y1-e*x2;
delay(10);
putpixel(200+x2,200+y2,3);
x1=x2;
y1=y2;
}while( (y1-start_y)<e || (start_x-x1)>e );
}
void draw::bcircle()
{
float rad;
float d=3-(2*rad);
cout<<”\n\nEnter the radius of circle: “;
cin>>rad;
x2=0;
y2=rad;
do
{
delay(15);
putpixel(200+x2,200+y2,1);
putpixel(200+y2,200+x2,2);
putpixel(200-y2,200+x2,3);
putpixel(200+x2,200-y2,4);
putpixel(200-x2,200-y2,5);
putpixel(200-y2,200-x2,6);
putpixel(200+y2,200-x2,7);
putpixel(200-x2,200+y2,8);
if(d<0)
{
d=d+(4*x2)+6;
}
else
{
d=d+(4*(x2-y2))+10;
y2–;
}
x2++;
}while(x2<y2);
}
void main()
{
draw obj;
int gd=DETECT,gm;
int choice;
char ans;
float rad;
clrscr();
initgraph(&gd,&gm,”C:\\TC\\BGI”);
do
{
cout<<”\n\n MENU:”;
cout<<”\n\n\t1.DDA Line\n\n\t2.Bresenham’s Line\n\n\t3.DDA Circle\n\n\t4.Bresenham’s Circle”;
cout<<”\n\nEnter your choice: “;
cin>>choice;
switch(choice)
{
case 1:
obj.ddaline();
break;
case 2:
obj.bline();
break;
case 3:
obj.ddacircle();
break;
case 4:
obj.bcircle();
break;
default:
cout<<”\n\nPlease Enter Correct Choice: “;
break;
}
cout<<”\n\nDo you want to continue?(y/n): “;
fflush(stdin);
cin>>ans;
}while(ans==’y’ || ans==’Y');
getch();
closegraph();
}