Friday, 28 August 2015

CGG Practicals

CG Programmes....

Computer Graphics Programs for SE Computer Engineering Stud

 

Sr. No.

    

Name of Assignment

    

Software Used

Group A (Compulsory)

1.

    

Writing a C/C++ Program to emulate CPU Architecture (Central Bus) Develop register, ALU level GUI to display results.

    

MASM64x

Eclipse

TC++

2.

    

Writing a C++ class for displaying pixel or point on the screen.

3.

    

Write a C++ class for a Line drawing method using overloading DDA and Bresenham’s Algorithms, inheriting the pixel or point.

4.

    

Write a C++ class for a circle drawing inheriting line class.

Group B (At least Six)

5   

Write a program in C/C++ to draw a circle of desired radius.

    

Eclipse

TC++

VC++

6   

Write a program using C/C++ to draw a line with line styles (Thick, Thin, Dotted).

7   

Write a program in Java/ Python to draw a line with line style (Thick, Thin, Dotted).

8   

Write a C/C++ program to draw a convex polygons (Square, Rectangle, Triangle).

9   

Write a C/C++ program to draw a convex polygons (Square, Rectangle, Triangle) using programmable edges.

10  

Write a program in Java to draw a concave polygon.

11  

Write a C/C++ program to fill polygon using scan line algorithm.

EXTRA     

Write a Java/Python program to fill polygon using scan line algorithm.

EXTRA     

Write a program in C++ to test that given point is inside the polygon.

EXTRA     

Write a program in to draw a circle of desired radius using VC++ 12 or above. Use of BITBLT command is expected.

    

Group C: Advanced Technology Programming (At least One)

12  

Use OpenGL ES to draw a line for Android Mobile.

    

OpenGL ES

VRML

Direct3D

Maya

Cuda C++

EXTRA     

Use VRML to draw a line Diagram.

EXTRA     

Use Direct3D/Maya or open source equivalent to draw a Bouncing ball animation.

EXTRA     

Use Parallel programming using Cuda to draw polygon.

...............................................................................................

Assignment no: 01

//Aim:Writing a C/C++ Program to emulate CPU Architecture (Central Bus) Develop register, ALU level GUI to display results.

#include<iostream>

#include<graphics.h>

using namespace std;

int main()

{

int x,y;

int poly[10];

int gd=DETECT,gm=VGAMAX;

initgraph(&gd,&gm,NULL);

setcolor(WHITE);

poly[0]= 50;

poly[1]= 50;

poly[2]= 600;

poly[3]= 50;

poly[4]= 600;

poly[5]= 600;

poly[6]= 50;

poly[7]= 600;

poly[8]=poly[0];

poly[9]=poly[1];

drawpoly(5,poly);

rectangle(65,100,150,140);

outtextxy(95,115,"ALU");

line(150,120,180,120);

rectangle(180,100,380,140);

outtextxy(200,120,"SEQUENCE CONTROLLER");

rectangle(410,100,550,160);

outtextxy(422,107,"GENERAL");

outtextxy(422,122,"PURPOSE");

outtextxy(422,136,"REGISTER");

rectangle(410,220,550,250);

outtextxy(422,228,"CACHE MEMORY");

rectangle(210,180,350,220);

outtextxy(240,190,"PROGRAM");

outtextxy(240,205,"COUNTER");

line(280,140,280,180);

rectangle(65,280,200,250);

outtextxy(85,255,"INSTRUCTION");

outtextxy(85,268,"REGISTER");

rectangle(65,340,200,310);

outtextxy(85,315,"INSTRUCTION");

outtextxy(85,328," DECODER");

line(50,420,600,420);

line(50,450,600,450);

outtextxy(220,430,"ADDRESS & DATA BUS");

outtextxy(190,460,"BASIC ARCHITECTURE OF CPU");

line(105,140,105,250);

line(260,380,260,420);

line(320,380,320,420);

delay(10000);

closegraph();

return 0;

}

........................................................................................................

Assignment no: 02

//Aim:Writing a C++ class for displaying pixel or point on the screen.

#include<iostream>

#include<graphics.h>

using namespace std;

class pixel

{

private:

     int xc,yc,cl;

public:

      pixel()

       {

         xc=yc=0;

         cl=15;

       }

       void setcor(int x,int y)

       {

          xc=x;

          yc=y;

       }

       void setcolor(int p)

       {

        cl=p;

       }

 

       void draw()

       {

        putpixel(xc,yc,cl);

       }

};

int main()

{

int x1,y1,cl1,ch;

char a;

int gd=DETECT,gm=VGAMAX;

initgraph(&gd,&gm,NULL);

pixel p1;

do

{

 cout<<"  1.PIXEL BY HARDCOADED VALUE..";

 cout<<"\n2.ACCEPTING CO-ORDINATES..";

 cout<<"\n3.DRAWING PIXEL ON SCREEN..";

 cout<<"\nEnter your choice??";

 cin>>ch;

switch(ch)

{

case 1:

         p1.setcor(50,50);

         p1.setcolor(2);

         p1.draw();

         break;

case 2:

       cout<<"\nEnter Your X & Y co-ordinates??";

       cin>>x1>>y1;

       p1.setcor(x1,y1);

       cout<<"\nEnter Color For Pixel??";

       cin>>cl1;

       p1.setcolor(cl1);

break;

case 3:

       p1.draw();

break;

}

cout<<"\nDO U Want To Continue y OR n??";

cin>>a;

}while(a!='n');

getch();

closegraph();

return 0;

}

--------------------------------------------------------

OUTPUT:

aman@aman-VirtualBox:~/Desktop/04$ g++ a02.cpp -o a02 -lgraph

aman@aman-VirtualBox:~/Desktop/04$ ./a02

1.PIXEL BY HARDCOADED VALUE..

2.ACCEPTING CO-ORDINATES..

3.DRAWING PIXEL ON SCREEN..

Enter your choice

1

DO U Want To Continue y OR n??y

1.PIXEL BY HARDCOADED VALUE..

2.ACCEPTING CO-ORDINATES..

3.DRAWING PIXEL ON SCREEN..

Enter your choice??2

Enter Your X & Y co-ordinates??50

100

Enter Color For Pixel??4

DO U Want To Continue y OR n??y

1.PIXEL BY HARDCOADED VALUE..

2.ACCEPTING CO-ORDINATES..

3.DRAWING PIXEL ON SCREEN..

Enter your choice??3

DO U Want To Continue y OR n??n

.............................................................................................

Assignment no: 03

//Write a C++ class for a Line drawing method using overloading DDA and Bresenham’s Algorithms, inheriting the pixel or point.

#include<graphics.h>

#include<iostream>

using namespace std;

class pt   //base class

{

    protected: int xco,yco,color;

    public:

    pt()

    {

        xco=0;yco=0;color=15;

    }

    void setco(int x,int y)

    { 

        xco=x;

        yco=y;

    } 

    void setcolor(int c) 

    { 

        color=c;

    }

 

    void draw()

    {    putpixel(xco,yco,color);

    }

};

class dline: public pt   //derived class

{

    private: int x2,y2;

 

    public:

    dline():pt()

    {

        x2=0,y2=0;

    }

 

    void setline(int x, int y, int xx, int yy)

    {

        pt::setco(x,y);

        x2=xx;

        y2=yy;

    }

    void drawl()   //Bresenham's Line

    {

        float x,y,dx,dy,e,temp;

        int i,s1,s2,ex;

      

        dx=abs(x2-xco);

        dy=abs(y2-yco);

        x=xco;

        y=yco;

        pt::setco(x,y);

        pt::draw();

     

        if(x2 > xco)  //sign() function

        {

            s1=1;

        }

        if(x2 < xco)

        {

            s1=-1;

        }

                if(x2 == xco)

        {

            s1=0;

        }

        if(y2 > yco)

        {

            s2=1;

        }

        if(y2 < yco)

        {

            s2=-1;

        }

                if(y2 == yco)

        {

            s2=0;

        }

        if(dy > dx)

        {

            temp = dx;

              dx = dy;

              dy = temp;

              ex = 1; 

        }

        else

        {

            ex=0;

        }

        e=2*dy-dx;  //decision variable

        i=1;

        do

        {

            while(e>=0)

            {

                if(ex==1)

                {

                    x = x + s1;

                }

                else

                {

                    y = y + s2;

                }

            e = e + 2*dy - 2*dx;

            }

            if(ex==1)

                {

                    y = y + s2;

                }

                else

                {

                    x = x + s1;

                }

            e = e + 2*dy;

          

            pt::setco(x,y);

            pt::draw();

            i = i + 1;

     

        }while(i<=dx);

    } 

    void drawl(int colour)  //DDA Line

    {

        float x,y,dx,dy,len;

        int i;

        pt::setcolor(colour);

     

        dx=abs(x2-xco);

        dy=abs(y2-yco);

     

        if(dx >= dy)

        {

            len=dx;

        }

        else

        {

            len=dy;

        }

        dx=(x2-xco)/len;

        dy=(y2-yco)/len;

        x = xco + 0.5;

        y = yco + 0.5;     

        i=1;

        while(i<=len)

        {

            pt::setco(x,y);

            pt::draw();

            x = x + dx;

            y = y + dy;

            i = i + 1;

        cout<<"\ti"<<i;

        cout<<"\tx"<<x;

        cout<<"\ty "<<y<<endl;

     

        }

        pt::setco(x,y);

        pt::draw();

    }

};

int main()

{

    int gd=DETECT,gm=VGAMAX;

    int ch,x1,y1,x2,y2, xmax,ymax,xmid,ymid;

    char a;

    initgraph(&gd,&gm,NULL);

    pt p;

    dline dda;

 

    xmax = getmaxx();

    ymax = getmaxy();

    xmid = xmax /2;

    ymid = ymax /2;

    line(xmid,0,xmid,ymax);  //Y co-ordinate

    line(0,ymid,xmax,ymid);  //X co-ordinate

do

{

        xmax = getmaxx();

    ymax = getmaxy();

    xmid = xmax /2;

    ymid = ymax /2;

 cout<<"1.DDA LINE..";

 cout<<"\n2.BRESENHAM'S LINE..";

 cout<<"\n3.EXIT..";

 cout<<"\nEnter your choice: ";

 cin>>ch;

switch(ch)

{

case 1:

    cout<<"\n Enter x1: "; cin>>x1; 

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;      

    dda.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2); 

    dda.drawl(15);

        break;

case 2:

        cout<<"\n Enter x1: "; cin>>x1; 

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;      

    dda.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2); 

    dda.drawl();

        break;

case 3:

        exit;

    break;

}

cout<<"\nDO U Want To Continue y OR n: ";

cin>>a;

}while(a!='n');  

    delay(3000);

    getch();

    closegraph();

    return 0;

 

}

------------------------------------------------------------------------------------------------------

Assignment no: 04

//Aim:Write a C++ class for a circle drawing inheriting line class.

 

  

#include<graphics.h>

#include<iostream>

#include<stdlib.h>

#include<math.h>

using namespace std;

class dline   //base class

{

    protected: int x1,y1,x2,y2, colour;

    public:

    dline()

    {

        x1=0; y1=0; x2=0, y2=0;

    }

    void setcolor(int color)

        {

        colour =color;

    }

    void setline(float x1,float y1, float xx,float yy)

 

    {

        x1=x1; y1=y1; x2=xx, y2=yy;

    }

    

    void drawl(float x1,float y1, float xx,float yy)  //Simple DDA Line

    {

        float x,y,dx,dy,len;

        int i;    

        dx=abs(x2-x1);

        dy=abs(y2-y1);

    

        if(dx >= dy)

        {

            len=dx;

        }

        else

        {

            len=dy;

        }

        dx=(x2-x1)/len;

        dy=(y2-y1)/len;

        x = x1 + 0.5;

        y = y1 + 0.5;

        i=1;

        while(i<=len)

        {

            putpixel(x,y,colour);

            x = x + dx;

            y = y + dy;

            i = i + 1;    

        }    

    }

};

class mcircle:public dline

    {

    private: int x0,y0;

    public:

    mcircle():dline()

    {

        x0=0;y0=0;

    }

 

    void drawc(float x1, float y1, int r)

    {

        int i, x, y;

        float d;  

        x=0, y=r;

        d = 1.25 - r;            //decision variabel

        dline::setline(x,y,x,y);

        do

        {

            

             

                      dline::drawl(x1+x, y1+y,x1+x, y1+y);

                      dline::drawl(x1+y, y1+x,x1+y, y1+x);

                      dline::drawl(x1+y, y1-x,x1+y, y1-x);

                      dline::drawl(x1+x, y1-y,x1+x, y1-y);

                       dline::drawl(x1-x, y1-y,x1-x, y1-y);

                       dline::drawl(x1-y, y1-x,x1-y, y1-x);

                        dline::drawl(x1-y, y1+x,x1-y, y1+x);

                        dline::drawl(x1-x, y1+y,x1-x, y1+y);

        

            if(d<0)

            {

                x = x + 1;

                d = d + (2*x) + 3;

            }

            else

            {

                x = x + 1;

                y = y - 1;

                d = d + (2*x-2*y) + 3;

            }

        }while(x<=y);

    }

 

};

int main()

{

 

    int gd=DETECT,gm=VGAMAX;

    int i, x, y, r, xmax,ymax,xmid,ymid;

 

    initgraph(&gd,&gm,NULL);

    dline l;

    mcircle c;

 

        cout<<"\n Enter x: "; cin>>x;

    cout<<"\n Enter y: "; cin>>y;

    cout<<"\n Enter radius: "; cin>>r;

    l.setcolor(15);

    c.drawc(x,y,r);

    delay(3000);

    getch();

    closegraph();

    return 0;

}

................................................................................................................................

 

#include<graphics.h>

#include<iostream>

#include<stdlib.h>

#include<math.h>

using namespace std;

class dline   //base class

{

    protected: int x0,y0,x1,y1,x2,y2, colour;

    public:

    dline()

    {

        x1=0; y1=0; x2=0, y2=0;

    }

    void setcolor(int color)

        {

        colour =color;

    }

    void setoff1(int xx,int yy)

    {

        x0=xx;

        y0=yy;

    }

    void setline(float x1,float y1, float xx,float yy)

 

    {

        x1=x1+x0; y1=y0-y1; x2=xx+x0, y2=y0-yy;

    }

    

    void drawl(float x1,float y1, float xx,float yy)  //Simple DDA Line

    {

        float x,y,dx,dy,len;

        int i;    

        dx=abs(x2-x1);

        dy=abs(y2-y1);

    

        if(dx >= dy)

        {

            len=dx;

        }

        else

        {

            len=dy;

        }

        dx=(x2-x1)/len;

        dy=(y2-y1)/len;

        x = x1 + 0.5;

        y = y1 + 0.5;

    

          putpixel(x,y,colour);

            x = x + dx;

            y = y + dy;

         

    }

};

class mcircle:public dline

    {

    private: int x0,y0;

    public:

    mcircle():dline()

    {

        x0=0;y0=0;

    }

    void setoff(int xx,int yy)

    {

        x0=xx;

        y0=yy;

    }

 

    void drawc(float x1, float y1, int r)

    {

        int i, x, y;

        float d;   

        x=0, y=r;

        d = 1.25 - r;            //decision variabel

        dline::setline(x,y,x,y);

        do

        {

         

         

        dline::drawl(x1+x0+x, y0-y1+y,x1+x0+x, y0-y1+y);

            dline::drawl(x1+x0+y, y0-y1+x,x1+x0+y, y0-y1+x);

            dline::drawl(x1+x0+y, y0-y1-x,x1+x0+y, y0-y1-x);

            dline::drawl(x1+x0+x, y0-y1-y,x1+x0+x, y0-y1-y);

            dline::drawl(x1+x0-x, y0-y1-y,x1+x0-x, y0-y1-y);

            dline::drawl(x1+x0-y, y0-y1-x,x1+x0-y, y0-y1-x);

            dline::drawl(x1+x0-y, y0-y1+x,x1+x0-y, y0-y1+x);

            dline::drawl(x1+x0-x, y0-y1+y,x1+x0-x, y0-y1+y);

        

            if(d<0)

            {

                x = x + 1;

                d = d + (2*x) + 3;

            }

            else

            {

                x = x + 1;

                y = y - 1;

                d = d + (2*x-2*y) + 3;

            }

        }while(x<=y);

    } 

 

};

int main()

{

 

    int gd=DETECT,gm=VGAMAX;

    int i, x, y, r, xmax,ymax,xmid,ymid;

    char a;

 

    initgraph(&gd,&gm,NULL);

    dline l; 

    mcircle c;

    xmax = getmaxx();

        ymax = getmaxy();

        xmid = xmax /2;

        ymid = ymax /2;

        line(xmid,0,xmid,ymax);  //Y co-ordinate

        line(0,ymid,xmax,ymid);  //X co-ordinate

 

   do

   { 

    cout<<"\n Enter x: "; cin>>x;

    cout<<"\n Enter y: "; cin>>y;

    cout<<"\n Enter radius: "; cin>>r;

 

    c.setoff(xmid, ymid);

    l.setoff1(xmid, ymid);

    l.setcolor(15);

    c.drawc(x,y,r);

 

    cout<<"\nDO U Want To Continue y OR n: ";

    cin>>a;

    }while(a!='n');

 

    delay(3000);

    getch();

    closegraph();

    return 0;

}

------------------------------------------------------------------------------------------------------

Assignment no: 05

//Aim:Write a program in C/C++ to draw a circle of desired radius.

 

#include<graphics.h>

#include<iostream>

#include<stdlib.h>

#include<math.h>

using namespace std;

class dcircle

{

    private: int x0,y0;

    public:

    dcircle()

    {

        x0=0;y0=0;

    }

    void setoff(int xx,int yy)

    {

        x0=xx;

        y0=yy;

    }

    void drawdc(float x, float y, int r)  //DDA Circle

    {

        float x1,y1,x2,y2,startx,starty,ep;

        int i,val;  

        x1=r*cos(0); //Initialize starting point

        y1=r*sin(0);

 

        startx = x1;

        starty = y1;

        i=0;

        do

        {

            val = pow(2,i);

            i++;

        }while(val<r);

        ep = 1/pow(2,i-1);  //calculation of epsilon

    

        do

        {

            x2 = x1 + y1*ep;

            y2 = y1 - x2*ep;

            putpixel(x0+x+x2, y0-(y+y2),15);

            x1 = x2;

            y1 = y2;

        }while((y1 - starty) < ep || (startx - x1) > ep);

    }

    void drawbc(int x1, int y1, int r)  //Bresenham's Circle

    {

        int i, x, y;

        float d;  

        x=0, y=r;

        d = 3 - 2*r;            //decision variable

        do

        {

                     

            putpixel(x1+x0+x, y0-y1+y,15);

            putpixel(x1+x0+y, y0-y1+x,15);

            putpixel(x1+x0+y, y0-y1-x,15);

            putpixel(x1+x0+x, y0-y1-y,15);

            putpixel(x1+x0-x, y0-y1-y,15);

            putpixel(x1+x0-y, y0-y1-x,15);

            putpixel(x1+x0-y, y0-y1+x,15);

            putpixel(x1+x0-x, y0-y1+y,15);

        

            if(d<=0)

            {

                x = x + 1;

                d = d + (4*x) + 6;

            }

            else

            {

                x = x + 1;

                y = y - 1;

                d = d + (4*x-4*y) + 10;

            }

        }while(x<=y);

    }

    void drawmc(float x1, float y1, int r)  // Mid point Circle

    {

        int i, x, y;

        float d;  

        x=0, y=r;

        d = 1.25 - r;            //decision variable

        do

        {

                    

            putpixel(x1+x0+x, y0-y1+y,15);

            putpixel(x1+x0+y, y0-y1+x,15);

            putpixel(x1+x0+y, y0-y1-x,15);

            putpixel(x1+x0+x, y0-y1-y,15);

            putpixel(x1+x0-x, y0-y1-y,15);

            putpixel(x1+x0-y, y0-y1-x,15);

            putpixel(x1+x0-y, y0-y1+x,15);

            putpixel(x1+x0-x, y0-y1+y,15);

        

            if(d<0)

            {

                x = x + 1;

                d = d + (2*x) + 3;

            }

            else

            {

                x = x + 1;

                y = y - 1;

                d = d + (2*x-2*y) + 5;

            }

        }while(x<=y);

    }

 

};

int main()

{

    int gd=DETECT,gm=VGAMAX;

    int i, x, y, r,ch, xmax,ymax,xmid,ymid;

    float a,b; 

    char ans;

    initgraph(&gd, &gm, NULL);    

    dcircle c1;

    xmax = getmaxx();

    ymax = getmaxy();

    xmid = xmax /2;

    ymid = ymax /2;

    line(xmid,0,xmid,ymax);  //Y co-ordinate

    line(0,ymid,xmax,ymid);  //X co-ordinate

    do

    {    

     cout<<"\nEnter Cricle Drwaing algorithm";

     cout<<"\n1.DDA..";

     cout<<"\n2.BRESENHAM'S..";

     cout<<"\n3.MID POINT..";

     cout<<"\n4.EXIT..";

     cout<<"\nEnter your choice: ";

    cin>>ch;

switch(ch)

{

case 1:

    {

    cout<<"\n Enter x: "; cin>>a;

    cout<<"\n Enter y: "; cin>>b;

    cout<<"\n Enter radius: "; cin>>r;

    c1.setoff(xmid, ymid);

    setcolor(15);

    c1.drawdc(a,b,r);

        break;

    }

case 2:

        {

    cout<<"\n Enter x: "; cin>>x;

    cout<<"\n Enter y: "; cin>>y;

    cout<<"\n Enter radius: "; cin>>r;

    c1.setoff(xmid, ymid);

    setcolor(15);

    c1.drawbc(x,y,r);

        break;

    }

case 3:

    {

    cout<<"\n Enter x: "; cin>>x;

    cout<<"\n Enter y: "; cin>>y;

    cout<<"\n Enter radius: "; cin>>r;

    c1.setoff(xmid, ymid);

    setcolor(15);

    c1.drawmc(x,y,r);

        break;

    }

case 4:

        exit;

    break;

}

cout<<"\nDO U Want To Continue y OR n: ";

cin>>ans;

}while(ans!='n');

 

    delay(3000);

    getch();

    closegraph();

    return 0;

}

/*

aman@aman-VirtualBox:~/Downloads/cgprogram012$ g++ 5.cpp -o 5 -lgraph

aman@aman-VirtualBox:~/Downloads/cgprogram012$ ./5

Enter Cricle Drwaing algorithm

1.DDA..

2.BRESENHAM'S..

3.MID POINT..

4.EXIT..

Enter your choice: [xcb] Unknown sequence number while processing queue

[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called

[xcb] Aborting, sorry about that.

5: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.

1

 Enter x: 100

 Enter y: 100

 Enter radius: 50

DO U Want To Continue y OR n: y

Enter Cricle Drwaing algorithm

1.DDA..

2.BRESENHAM'S..

3.MID POINT..

4.EXIT..

Enter your choice: 2

 Enter x: -100

 Enter y: -100

 Enter radius: 60

DO U Want To Continue y OR n: y

Enter Cricle Drwaing algorithm

1.DDA..

2.BRESENHAM'S..

3.MID POINT..

4.EXIT..

Enter your choice: 3

 Enter x: -100

 Enter y: 100

 Enter radius: 80

DO U Want To Continue y OR n: n  */

------------------------------------------------------------------------------------------------------

Assignment no: 06

//Aim:Write a program using C/C++ to draw a line with line styles (Thick, Thin, Dotted).

//Line styles using DDA

#include<graphics.h>

#include<iostream>

#include<stdlib.h>

#include<math.h>

using namespace std;

class pt   //base class

{

    protected: int xco,yco,color;

    public:

    pt()

    {

        xco=0;yco=0;color=15;

    }

    void setco(int x,int y)

    {

        xco=x;

        yco=y;

    }

    void setcolor(int c)

    {

        color=c;

    }

 

    void draw()

    {    putpixel(xco,yco,color);

    }

};

class dline: public pt   //derived class

{

    private: int x2,y2;

 

    public:

    dline():pt()

    {

        x2=0,y2=0;

    }

 

    void setline(int x, int y, int xx, int yy)

    {

        pt::setco(x,y);

        x2=xx;

        y2=yy;

    }

    void drawsi(int colour)  //Simple DDA Line

    {

        float x,y,dx,dy,len;

        int i;

        pt::setcolor(colour);

    

        dx=abs(x2-xco);

        dy=abs(y2-yco);

    

        if(dx >= dy)

        {

            len=dx;

        }

        else

        {

            len=dy;

        }

        dx=(x2-xco)/len;

        dy=(y2-yco)/len;

        x = xco + 0.5;

        y = yco + 0.5;    

        i=1;

        while(i<=len)

        {

            pt::setco(x,y);

            pt::draw();

            x = x + dx;

            y = y + dy;

            i = i + 1;      

        }

        pt::setco(x,y);

        pt::draw();

    }

void drawda(int colour)  //Dash DDA Line

    {

        float x,y,dx,dy,len;

        int i,dash_pixel=0, dash_space=0;

        pt::setcolor(colour);

    

        dx=abs(x2-xco);

        dy=abs(y2-yco);

     

        if(dx >= dy)

        {

            len=dx;

        }

        else

        {

            len=dy;

        }

        dx=(x2-xco)/len;

        dy=(y2-yco)/len;

        x = xco + 0.5;

        y = yco + 0.5;    

        i=1;    

        while(i<=len)

 

        {

            dash_pixel=0;

            while(dash_pixel<5)        

            {        

            pt::setco(x,y);

            pt::draw();        

            x = x + dx;

            y = y + dy;

            i = i + 1;

            dash_pixel = dash_pixel +1;

            }

    dash_space=0; 

    while(dash_space<=2)        

            {        

                   

            x = x + dx;

            y = y + dy;

            i = i + 1;

            dash_space = dash_space +1;

            }

        

        }

    

    }

void drawdo(int colour)  //Dotted DDA Line

    {        float x,y,dx,dy,len;

        int i,dot_space;

        pt::setcolor(colour);    

        dx=abs(x2-xco);

        dy=abs(y2-yco);    

        if(dx >= dy)

        {

            len=dx;

        }

        else

        {

            len=dy;

        }

        dx=(x2-xco)/len;

        dy=(y2-yco)/len;

        x = xco + 0.5;

        y = yco + 0.5;    

        i=1;

        while(i<=len)

            {

            dot_space=0;

            while(dot_space<=1)       

            {                             

            x = x + dx;

            y = y + dy;

            i = i + 1;

            dot_space = dot_space +1;

            }

            pt::setco(x,y);

            pt::draw();          

             }

    

    }

void drawth(int x1,int y1, int x2, int y2,int colour )  //Thick DDA Line

    {

        float x,y,dx,dy,len;

        int i;        

        dx=abs(x2-x1);

        dy=abs(y2-y1);    

        if(dx >= dy)

        {            len=dx;

        }

        else    

       {            len=dy;

        }

        dx=(x2-x1)/len;

        dy=(y2-y1)/len;

        x = x1 + 0.5;

        y = y1 + 0.5; 

        i=1;

        while(i<=len)

        {            putpixel(x,y,colour);

            x = x + dx;

            y = y + dy;

            i = i + 1;          

        }

        putpixel(x,y,colour);

    }

 };

int main()

{

    int gd=DETECT,gm=VGAMAX;

    int i, ch,x1,y1,x2,y2, dx,dy,xmax,ymax,xmid,ymid,wx,wy,th;

    char a;

    initgraph(&gd,&gm,NULL);

        dline ls;

    xmax = getmaxx();

    ymax = getmaxy();

    xmid = xmax /2;

    ymid = ymax /2;

    line(xmid,0,xmid,ymax);  //Y co-ordinate

    line(0,ymid,xmax,ymid);  //X co-ordinate

do

{        xmax = getmaxx();

    ymax = getmaxy();

    xmid = xmax /2;

    ymid = ymax /2;

 cout<<"\nEnter Line Styles";

 cout<<"\n1.SIMPLE..";

 cout<<"\n2.DASH..";

 cout<<"\n3.DOTTED..";

 cout<<"\n4.THICK..";

 cout<<"\n5.EXIT..";

 cout<<"\nEnter your choice: ";

 cin>>ch;

switch(ch)

{

case 1:

    cout<<"\n Enter x1: "; cin>>x1;

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;     

    ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);

    ls.drawsi(15);

        break;

case 2:

        cout<<"\n Enter x1: "; cin>>x1;

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;     

    ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);

    ls.drawda(15);

        break;

case 3:

    cout<<"\n Enter x1: "; cin>>x1;

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;     

    ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);

    ls.drawdo(15);

        break;

case 4:

        cout<<"\n Enter x1: "; cin>>x1;

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;     

    cout<<"Enter Thickness: ";

    cin>>th;

    ls.drawth(x1+xmid,ymid-y1,x2+xmid,ymid-y2,15);       

        if((y2-y1)/(x2-x1) <1)

        {

            wy=(th-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*abs(x2-x1));

            for(i=0;i<wy;i++)

            {

                ls.drawth(x1+xmid,ymid-y1-i,x2+xmid,ymid-y2-i,15);

                ls.drawth(x1+xmid,ymid-y1+i,x2+xmid,ymid-y2+i,15);

            }

        }

        else

        {

            wx=(th-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*abs(y2-y1));

            for(i=0;i<wx;i++)

            {

                ls.drawth(x1+xmid-i,ymid-y1,x2+xmid-i,ymid-y2,15);

                ls.drawth(x1+xmid+i,ymid-y1,x2+xmid+i,ymid-y2,15);

            }

 }

        break;

case 5:

        exit;

    break;

}

cout<<"\nDO U Want To Continue y OR n: ";

cin>>a;

}while(a!='n');      

    delay(3000);

    getch();

    closegraph();

    return 0;

   }

/*

aman@aman-VirtualBox:~/Downloads/cgprogram012$ ./a05

Enter Line Styles

1.SIMPLE..

2.DASH..

3.DOTTED..

4.THICK..

5.EXIT..

Enter your choice:1

 Enter x1: 10

 Enter y1: 10

 Enter x2: 60

 Enter y2: 60

DO U Want To Continue y OR n: y

Enter Line Styles

1.SIMPLE..

2.DASH..

3.DOTTED..

4.THICK..

5.EXIT..

Enter your choice: 2

 Enter x1: -10

 Enter y1: 10

 Enter x2: -80

 Enter y2: 80

DO U Want To Continue y OR n: y

Enter Line Styles

1.SIMPLE..

2.DASH..

3.DOTTED..

4.THICK..

5.EXIT..

Enter your choice: 3

 Enter x1: -10

 Enter y1: -10

 Enter x2: -100

 Enter y2: -100

DO U Want To Continue y OR n: y

Enter Line Styles

1.SIMPLE..

2.DASH..

3.DOTTED..

4.THICK..

5.EXIT..

Enter your choice: 4

 Enter x1: 10

 Enter y1: -10

 Enter x2: 100

 Enter y2: -100

Enter Thickness: 10

DO U Want To Continue y OR n: n  */

---------------------------------------------------------------------------------------------------

     //line styles using Bresenham's algo

#include<graphics.h>

#include<iostream>

#include<stdlib.h>

#include<math.h>

using namespace std;

class pt   //base class

{

    protected: int xco,yco,color;

    public:

    pt()

    {

        xco=0;yco=0;color=15;

    }

    void setco(int x,int y)

    {

        xco=x;

        yco=y;

    }

    void setcolor(int c)

    {

        color=c;

    }

 

    void draw()

    {    putpixel(xco,yco,color);

    }

};

class dline: public pt   //derived class

{

    private: int x2,y2;

 

    public:

    dline():pt()

    {

        x2=0,y2=0;

    }

 

    void setline(int x, int y, int xx, int yy)

    {

        pt::setco(x,y);

        x2=xx;

        y2=yy;

    }

    void drawsi()   //Bresenham's Line

    {

        float x,y,dx,dy,e,temp;

        int i,s1,s2,ex;

    

        dx=abs(x2-xco);

        dy=abs(y2-yco);

        x=xco;

        y=yco;

        pt::setco(x,y);

        pt::draw();

    

        if(x2 > xco)  //sign() function

        {

            s1=1;

        }

        if(x2 < xco)

        {

            s1=-1;

        }

                if(x2 == xco)

        {

            s1=0;

        }

        if(y2 > yco)

        {

            s2=1;

        }

        if(y2 < yco)

        {

            s2=-1;

        }

                if(y2 == yco)

        {

            s2=0;

        }

        if(dy > dx)

        {

            temp = dx;

              dx = dy;

              dy = temp;

              ex = 1;

        }

        else

        {

            ex=0;

        }

        e=2*dy-dx;  //decision variable

        i=1;

        do

        {

            while(e>=0)

            {

                if(ex==1)

                {

                    x = x + s1;

                }

                else

                {

                    y = y + s2;

                }

            e = e + 2*dy - 2*dx;

            }

            if(ex==1)

                {

                    y = y + s2;

                }

                else

                {

                    x = x + s1;

                }

            e = e + 2*dy;

        

            pt::setco(x,y);

            pt::draw();

            i = i + 1;

    

        }while(i<=dx);

    }

void drawda()  //Dash Line

    {

    

        float x,y,dx,dy,e,temp;

        int i,s1,s2,ex, dash=0;

    

        dx=abs(x2-xco);

        dy=abs(y2-yco);

        x=xco;

        y=yco;

        pt::setco(x,y);

        pt::draw();

    

        if(x2 > xco)  //sign() function

        {

            s1=1;

        }

        if(x2 < xco)

        {

            s1=-1;

        }

                if(x2 == xco)

        {

            s1=0;

        }

        if(y2 > yco)

        {

            s2=1;

        }

        if(y2 < yco)

        {

            s2=-1;

        }

                if(y2 == yco)

        {

            s2=0;

        }

        if(dy > dx)

        {

            temp = dx;

              dx = dy;

              dy = temp;

              ex = 1;

        }

        else

        {

            ex=0;

        }

        e=2*dy-dx;  //decision variable

        i=1;

        do

        {

            dash=0;

            if(dash<5)        

            {

            while(e>=0)

            {

            

                if(ex==1)

                {

                    x = x + s1;

                }

                else

                {

                    y = y + s2;

                }

            e = e + 2*dy - 2*dx;

            }

                if(ex==1)

                {

                    y = y + s2;

                }

                else

                {

                    x = x + s1;

                }

            e = e + 2*dy;

        

            pt::setco(x,y);

            pt::draw();

            i = i + 1;

            dash = dash +1;

            }

            else

            {

            x = x + 2*s1;

            y = y + 2*s2;

            }

        }while(i<=dx);

    }   

void drawdo()  //Dotted Line

    {

        float x,y,dx,dy,e,temp;

        int i,s1,s2,ex, dot=0;

    

        dx=abs(x2-xco);

        dy=abs(y2-yco);

        x=xco;

        y=yco;

        pt::setco(x,y);

        pt::draw();

    

        if(x2 > xco)  //sign() function

        {

            s1=1;

        }

        if(x2 < xco)

        {

            s1=-1;

        }

                if(x2 == xco)

        {

            s1=0;

        }

        if(y2 > yco)

        {

            s2=1;

        }

        if(y2 < yco)

        {

            s2=-1;

        }

                if(y2 == yco)

        {

            s2=0;

        }

        if(dy > dx)

        {

            temp = dx;

              dx = dy;

              dy = temp;

              ex = 1;

        }

        else

        {

            ex=0;

        }

        e=2*dy-dx;  //decision variable

        i=1;

        do

        {

            dot=0;

            while(dot<5)        

            {

            while(e>=0)

            {

             

                if(ex==1)

                {

                    x = x + s1;

                }

                else

                {

                    y = y + s2;

                }

            e = e + 2*dy - 2*dx;

            }

            if(ex==1)

                {

                    y = y + s2;

                }

                else

                {

                    x = x + s1;

                }

            e = e + 2*dy;

                

            pt::setco(x,y);

            pt::draw();

            i = i + 1;

            dot = dot +1;

            }

        

        }while(i<=dx);

    }    

void drawth(int x1,int y1, int x2, int y2,int colour )  //Thick Line

    {

        float x,y,dx,dy,e,temp;

        int i,s1,s2,ex;

    

        dx=abs(x2-xco);

        dy=abs(y2-yco);

        x=xco;

        y=yco;

        pt::setco(x,y);

        pt::draw();

    

        if(x2 > xco)  //sign() function

        {

            s1=1;

        }

        if(x2 < xco)

        {

            s1=-1;

        }

                if(x2 == xco)

        {

            s1=0;

        }

        if(y2 > yco)

        {

            s2=1;

        }

        if(y2 < yco)

        {

            s2=-1;

        }

                if(y2 == yco)

        {

            s2=0;

        }

        if(dy > dx)

        {

            temp = dx;

              dx = dy;

              dy = temp;

              ex = 1;

        }

        else

        {

            ex=0;

        }

        e=2*dy-dx;  //decision variable

        i=1;

        do

        {

            while(e>=0)

            {

                if(ex==1)

                {

                    x = x + s1;

                }

                else

                {

                    y = y + s2;

                }

            e = e + 2*dy - 2*dx;

            }

            if(ex==1)

                {

                    y = y + s2;

                }

                else

                {

                    x = x + s1;

                }

            e = e + 2*dy;

        

            pt::setco(x,y);

            pt::draw();

            i = i + 1;

    

        }while(i<=dx);    }

 };

int main()

{

 

    int gd=DETECT,gm=VGAMAX;

    int i, ch,x1,y1,x2,y2, dx,dy,xmax,ymax,xmid,ymid,wx,wy,th;

    char a;

    initgraph(&gd,&gm,NULL); 

 

    dline ls; 

    xmax = getmaxx();

    ymax = getmaxy();

    xmid = xmax /2;

    ymid = ymax /2;

    line(xmid,0,xmid,ymax);  //Y co-ordinate

    line(0,ymid,xmax,ymid);  //X co-ordinate

do

{        xmax = getmaxx();

    ymax = getmaxy();

    xmid = xmax /2;

    ymid = ymax /2;

 cout<<"\nEnter Line Styles";

 cout<<"\n1.SIMPLE..";

 cout<<"\n2.DASH..";

 cout<<"\n3.DOTTED..";

 cout<<"\n4.THICK..";

 cout<<"\n5.EXIT..";

 cout<<"\nEnter your choice: ";

 cin>>ch;

switch(ch)

{

case 1:

    cout<<"\n Enter x1: "; cin>>x1;

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;     

    ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);

    ls.drawsi();

        break;

case 2:

        cout<<"\n Enter x1: "; cin>>x1;

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;     

    ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);

    ls.drawda();

        break;

case 3:

    cout<<"\n Enter x1: "; cin>>x1;

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;     

    ls.setline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);

    ls.drawdo();

        break;

case 4:

        cout<<"\n Enter x1: "; cin>>x1; 

    cout<<"\n Enter y1: "; cin>>y1;

    cout<<"\n Enter x2: "; cin>>x2;

    cout<<"\n Enter y2: "; cin>>y2;     

    cout<<"Enter Thickness: ";

    cin>>th;

    ls.drawth(x1+xmid,ymid-y1,x2+xmid,ymid-y2,15);    

        

        if((y2-y1)/(x2-x1) <1)

        {

            wy=(th-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*abs(x2-x1));

            for(i=0;i<wy;i++)

            {

                ls.drawth(x1+xmid,ymid-y1-i,x2+xmid,ymid-y2-i,15);

                ls.drawth(x1+xmid,ymid-y1+i,x2+xmid,ymid-y2+i,15);

            }

        }

        else

        {

            wx=(th-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*abs(y2-y1));

            for(i=0;i<wx;i++)

            {

                ls.drawth(x1+xmid-i,ymid-y1,x2+xmid-i,ymid-y2,15);

                ls.drawth(x1+xmid+i,ymid-y1,x2+xmid+i,ymid-y2,15);

            }

        

        }

        break;

case 5:

        exit;

    break;

}

cout<<"\nDO U Want To Continue y OR n: ";

cin>>a;

}while(a!='n');       

    delay(3000);

    getch();

    closegraph();

    return 0; 

}

------------------------------------------------------------------------------------------------------

Assignment no: 07

//Write a program in Java/ Python to draw a line with line style (Thick, Thin, Dotted).

import java.awt.*;

import javax.swing.*;

public class Lines extends JFrame{

    public Lines(){

        super("Line Styles");

        setSize(400,400);

        setVisible(true);

    }

    Stroke[] linestyles=new Stroke[]{

        new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL),

        new BasicStroke(25.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER),

        new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND),};

    Stroke thindashed=new BasicStroke(2.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,1.0f, new float[]                                   {8.0f,3.0f,2.0f,3.0f},0.0f); 

    final static float dash1[]={10.0f};

    final static BasicStroke dashed =new BasicStroke(1.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);

 

    final static float thick1[]={10.0f};

    final static BasicStroke thickdash =new BasicStroke(10.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, thick1, 0.0f);

    public void paint(Graphics g){

        int xpoints[]={220,350,350,220};

        int ypoints[]={220,220,320,320};

        int npoints=4;

        int xpoints1[]={60,120,180};

        int ypoints1[]={320,220,320};

        int npoints1=3;

        Graphics2D g2d = (Graphics2D) g;

        super.paint(g);

     

        ((Graphics2D)g).setStroke(dashed);

        g.setColor(Color.red);

        g2d.drawLine(50,50,200,50);

        g2d.setColor(Color.black);

        g.drawString("Dashed Line", 210,55);

 

        ((Graphics2D)g).setStroke(thindashed);

        g.setColor(Color.blue);

        g2d.drawLine(50,90,200,90);

        g2d.setColor(Color.black);

        g.drawString("Thin Dashed Line", 210,95);

        ((Graphics2D)g).setStroke(thickdash);

        g.setColor(Color.green);

        g2d.drawLine(50,130,200,130);

        g2d.setColor(Color.black);

        g.drawString("Thick Dashed Line", 210,135);

        ((Graphics2D)g).setStroke(linestyles[0]);

        g.setColor(Color.red);

        g2d.drawLine(50,170,200,170);

        g2d.setColor(Color.black);

        g.drawString("Line", 210,175);

        } 

    public static void main(String[] args){

        Lines application = new Lines();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

------------------------------------------------------------------------------------------------------------

Assignment no: 08

//Write a C/C++ program to draw a convex polygons (Square, Rectangle, Triangle).

 

#include<iostream>

#include<graphics.h>

using namespace std;

class dline

{

    protected: int x1,y1,x2,y2;

    public:

    dline()

    {

        x1=0,y1=0,x2=0,y2=0;

    }

    void step1()

    {

        cout<<"\n Enter x1: ";cin>>x1;

        cout<<"\n Enter y1: ";cin>>y1;

    }

    void step2()

    {

        cout<<"\n Enter x2: ";cin>>x2;

        cout<<"\n Enter y2: ";cin>>y2;

    }

    void drawl()

    {

        float x,y,dx,dy,len;

        int i;

         

        dx=abs(x2-x1);

        dy=abs(y2-y1);

     

        if(dx >= dy)

        {

            len=dx;

        }

        else

        {

            len=dy;

        }

        dx=(x2-x1)/len;

        dy=(y2-y1)/len;

        x = x1 + 0.5;

        y = y1 + 0.5;     

        i=1;

        while(i<=len)

        {

            putpixel(x,y,15);

            x = x + dx;

            y = y + dy;

            i = i + 1;     

        }

        putpixel(x,y,15);

    }

};

class rect: public dline  //Rectangle

{

    private: int w,l;

    public:

    void setrect()

    {

        dline::step1();

        cout<<"\n Enter width: ";cin>>w;

        cout<<"\n Enter length: ";cin>>l;

    } 

    void drawrect()

    {

        x2=x1;

        y2=y1+w; 

        dline::drawl();  //left

        x2=x1+l;

        y2=y1; 

        dline::drawl();  //top

        x1=x1;

        y1=y1+w;     

        x2=x1+l;

        y2=y1; 

        dline::drawl();  //bottom

        x1=x1+l;

        y1=y1-w;     

        x2=x1;

        y2=y1+w;     

        dline::drawl();  //right

    }

}; 

class square: public dline  //Square

{

    private: int l;

    public:

    void setsquare()

    {

        dline::step1();

        cout<<"\n Enter length: ";cin>>l;

    } 

    void drawsquare()

    {

        x2=x1;

        y2=y1+l; 

        dline::drawl();  //left

        x2=x1+l;

        y2=y1; 

        dline::drawl();  //top

        x1=x1;

        y1=y1+l;     

        x2=x1+l;

        y2=y1; 

        dline::drawl();  //bottom

        x1=x1+l;

        y1=y1-l;     

        x2=x1;

        y2=y1+l;     

        dline::drawl();  //right

    }

}; 

 

class triangle: public dline  //Triangle

{

    private: int x3,y3;

    public:

    void settri()

    {

        dline::step1();

        dline::step2();

        cout<<"\n Enter x3: ";cin>>x3;

        cout<<"\n Enter y3: ";cin>>y3;

    } 

    void drawtri()

    {

        int tempx,tempy;

        dline::drawl();

        tempx=x2;

        x2=y2;

        x2=x3;

        y2=y3;

        dline::drawl();

        x1=tempx;

        y1=tempy;

        dline::drawl();

    }

}; 

int main()

{

 

    int gd=DETECT,gm=VGAMAX;

    int i, x, y, r,ch, xmax,ymax,xmid,ymid;

    char a;

    initgraph(&gd,&gm,NULL);

     

    rect r1;

    square s;

    triangle t;

    do

    {      

     cout<<"\nChoose polygon to draw";

     cout<<"\n1.Rectangle..";

     cout<<"\n2.Square..";

     cout<<"\n3.Triangle..";

     cout<<"\n4.EXIT..";

     cout<<"\nEnter your choice: ";

    cin>>ch;

switch(ch)

{

case 1:

    { 

    r1.setrect();

    r1.drawrect();

    break;

    }

case 2:

        { 

    s.setsquare();

    s.drawsquare();

    break;

    }

case 3:

    { 

    t.settri();

    t.drawtri();

    delay(3000);

    break;

    }

case 4:

        exit;

    break;

}

cout<<"\nDO U Want To Continue y OR n: ";

cin>>a;

}while(a!='n'); 

    delay(3000);

    getch();

    closegraph();

    return 0;

}

------------------------------------------------------------------------------------------------------

Assignment no: 09

//Write a C/C++ program to draw a convex polygons (Square, Rectangle, Triangle) using programmable edges.

#include<iostream>

#include<graphics.h>

using namespace std;

class dline

{

    protected: int x1,y1,x2,y2;

    public:

    dline()

    {

        x1=0,y1=0,x2=0,y2=0;

    }

 

    void drawl()

    {

        float x,y,dx,dy,len;

        int i;

        

        dx=abs(x2-x1);

        dy=abs(y2-y1);

    

        if(dx >= dy)

        {

            len=dx;

        }

        else

        {

            len=dy;

        }

        dx=(x2-x1)/len;

        dy=(y2-y1)/len;

        x = x1 + 0.5;

        y = y1 + 0.5;    

        i=1;

        while(i<=len)

        {

            putpixel(x,y,15);

            x = x + dx;

            y = y + dy;

            i = i + 1;    

        }

        putpixel(x,y,15);

    }

};

class poly: public dline  //poly

{

    private: int a[10][2],p;

    public:

    void setpts(int i)

    {

        int j;

        for(j=0;j<i;j++)

        {

        cout<<"\n Enter x-coordinate: "<<j<<":";cin>>a[j][0];

        cout<<"\n Enter y-coordinate: "<<j<<":";cin>>a[j][1];

        }

    }

    void drawpoly(int i)

    {

        int j;

        x1=a[0][0];

        y1=a[0][1];    

        x2=a[1][0];

        y2=a[1][1];

        dline::drawl();

    

        for(j=0;j<i-1;j++)

        {

        x1=a[j][0];

        y1=a[j][1];    

        x2=a[j+1][0];

        y2=a[j+1][1];

        dline::drawl();

        }

        x1=a[j][0];

        y1=a[j][1];    

        x2=a[0][0];

        y2=a[0][1];

        dline::drawl();

    }

};

int main()

{

    int n ,gd=DETECT,gm=VGAMAX;

    initgraph(&gd,&gm,NULL);

    

    poly shape;

          

     cout<<"\nEnter number of slides: "; cin>>n;

     shape.setpts(n);

    shape.drawpoly(n);

    delay(3000);

    getch();

    closegraph();

    return 0;

}

------------------------------------------------------------------------------------------------------

Assignment no: 10

//Aim:Write a program in Java to draw a concave polygon.

import java.awt.*;

import javax.swing.*;

public class Polygon extends JFrame{

    public Polygon(){

        super("Draw Square, Rectangle, Polygon");

        setSize(400,400);

        setVisible(true);

    }

    public void paint(Graphics g){

        int xpoints[]={55,145,55,145,55};

        int ypoints[]={55,55,145,145,55};

        int npoints=5;

        int xpoints1[]={250,300,350};

        int ypoints1[]={100,50,100};

        int npoints1=3;

        Graphics2D g2d = (Graphics2D) g;

        super.paint(g);

        g.setColor(Color.red);

        g.drawRect(55,150,90,150);

        g.drawRect(150,150,200,200);

        g2d.setColor(Color.blue);

        g2d.drawLine(150,150,150,150);

        g.drawPolygon(xpoints,ypoints,npoints);

        g.drawPolygon(xpoints1,ypoints1,npoints1);

        }

    public static void main(String[] args){

        Polygon application = new Polygon();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

------------------------------------------------------------------------------------------------------

Assignment no: 11

// Write a C/C++ program to fill polygon using scan line algorithm.

//Scan line algorithm for filling polygon

#include<iostream>

#include<graphics.h>

#include<stdlib.h>

using namespace std;

struct edge

{

    int x1,y1,x2,y2,flag;

};

int main()

{ 

    int n,i,j,k,gd=DETECT,gm=VGAMAX, x[10],y[10],ymax=0,ymin=480,yy,temp;

    struct edge ed[10],temped;

    float dx,dy,m[10],x_int[10],inter_x[10];

    initgraph(&gd,&gm,NULL);     

           

     cout<<"\n Enter the number of vertices of the graph: "; cin>>n;

     cout<<"\n Enter the vertices: \n";

    for(i=0;i<n;i++)

    {

        cout<<"x"<<i<<":"; cin>>x[i];

        cout<<"y"<<i<<":"; cin>>y[i];

        if(y[i]>ymax)

            ymax=y[i];

        if(y[i]<ymin)

            ymin=y[i];

        ed[i].x1=x[i];

        ed[i].y1=y[i];

    }

    for(i=0;i<n-1;i++)  //store the edge information

    {

        ed[i].x2=ed[i+1].x1;

        ed[i].y2=ed[i+1].y1;

        ed[i].flag=0;

    }

        ed[i].x2=ed[0].x1;

        ed[i].y2=ed[0].y1;

        ed[i].flag=0;

    for(i=0;i<n-1;i++)  //check for y1>y2 if not interchange it

    {

        if(ed[i].y1<ed[i].y2)

        {     

        temp=ed[i].x1;

        ed[i].x1=ed[i].x2;     

        ed[i].x2=temp;

        temp=ed[i].y1;

        ed[i].y1=ed[i].y2;     

        ed[i].y2=temp;

        }

    }

    for(i=0;i<n;i++)  //draw polygon

    { 

        line(ed[i].x1,ed[i].y1,ed[i].x2,ed[i].y2);

    }

 

    for(i=0;i<n-1;i++)  //storing the edges as y1,y2,x1

    {

        for(j=0;j<n-1;j++)

        {

            if(ed[j].y1<ed[j+1].y1)

            {     

            temped=ed[j];

            ed[j]=ed[j+1];     

            ed[j+1]=temped;

            }

            if(ed[j].y1==ed[j+1].y1)

            {

                if(ed[j].y2<ed[j+1].y2)

                {     

                    temped=ed[j];

                    ed[j]=ed[j+1];     

                    ed[j+1]=temped;

                }

                if(ed[j].y2==ed[j+1].y2)

                {

                    if(ed[j].x1<ed[j+1].x1)

                    {     

                    temped=ed[j];

                    ed[j]=ed[j+1];     

                    ed[j+1]=temped;

                    }

                }

            }

        }

    }

    for(i=0;i<n;i++)  //calculate 1/slope

    {

        dx=ed[i].x2-ed[i].x1;

        dy=ed[i].y2-ed[i].y1;

        if(dy==0)

            m[i]=0;

        else

            m[i]=dx/dy;

        inter_x[i]=ed[i].x1;

    }

    yy=ymax;

    while(yy>ymin)  //Mark active edges

    {

        for(i=0;i<n;i++)

        {

            if(yy>ed[i].y2 && yy<=ed[i].y1 && ed[i].y1!=ed[i].y2)

                ed[i].flag=1;

            else

                ed[i].flag=0;

        }     

 

    j=0;

    for(i=0;i<n;i++)  //Finding x intersections

    {

        if(ed[i].flag==1)

        {

            if(yy==ed[i].y1)

            {

                x_int[j]=ed[i].x1;

                j++;

                if(ed[i-1].y1==yy&&ed[i-1].y1<yy)

                {

                    x_int[j]=ed[i].x1;

                    j++;

                }

                if(ed[i+1].y1==yy&&ed[i+1].y1<yy)

                {

                    x_int[j]=ed[i].x1;

                    j++;

                }

            }

            else

            {

                x_int[j]=inter_x[i]+(-m[i]);

                inter_x[i]=x_int[j];

                j++;

            }

        }

    }

 

    for(i=0;i<j;i++)  //sorting the x intersections

    {

        for(k=0;k<j-1;k++)

        {

            if(x_int[k]>x_int[k+1])

            {     

            temp=x_int[k];

            x_int[k]=x_int[k+1];     

            x_int[k+1]=temp;

            }

        }

    }

    for(i=0;i<j;i+=2)  //Extracting x values to draw a line

    {

        line(x_int[i],yy,x_int[i+1],yy);

    }

    yy--;

   }  //end of while loop

    delay(3000);

    getch();

    closegraph();

    return 0;

}

------------------------------------------------------------------------------------------------------

Assignment no: 12

//Aim: Draw a line using OpenGL

#include <GL/glut.h>

#include <math.h>

#include <stdio.h>

const float PI=3.14;

void LineWithDDA(int x0,int y0,int x1,int y1)

{

    glPointSize(2.0);

    glBegin(GL_POINTS);

    glColor3f(0.0,0.0,0.0);

    int dx,dy,steps,i;

    float x,y;

    float xinc,yinc;

    dy=y1-y0;

    dx=x1-x0;

    y=y0;

    x=x0;

    if(abs(dx)>=abs(dy))

    {

        steps=dx;

    }

    else

    {

        steps=dy;

    }

    xinc=(float)dx/steps;

    yinc=(float)dy/steps;

    glVertex2d(x,y);

    for(i=1;i<steps;i++)

    {

        x+=xinc;

        y+=yinc;

        x0=floor(x);

        y0=floor(y);

        glVertex2d(x,y);

    }

    glEnd();

}

void init(void)

{

    glClearColor(1.0,1.0,1.0,0.0);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    glOrtho(0,800,0,600,0,600);

}

void display(void)

{

    glClear(GL_COLOR_BUFFER_BIT);

    LineWithDDA(0,0,800,600);

    LineWithDDA(0,600,800,0);

    LineWithDDA(0,300,800,300);

    LineWithDDA(400,0,400,600);

    LineWithDDA(0,150,800,450);

    LineWithDDA(0,450,800,150);

    LineWithDDA(200,0,600,600);

    LineWithDDA(600,0,200,600);

    glutSwapBuffers();

}

int main(int argc, char** argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

    glutInitWindowSize(800,600);

    glutInitWindowPosition(100,100);

    glutCreateWindow("DDA Line Drawing!");

    init();

    glutDisplayFunc(display);

    glutMainLoop();

    return 0;

}

---------------------------------------------------------------------------------------------------------------------------------

#include <GL/glut.h>

#include <math.h>

#include <stdio.h>

const float PI=3.14;

int sign(float arg)

{

    if(arg<0)

    {

        return -1;

    }

    else if(arg==0)

    {

        return 0;

    }

    else

    {

        return 1;

    }

}

void LineWithBrasenham(int x1,int y1,int x2,int y2)

{

    glPointSize(2.0);

    glBegin(GL_POINTS);

    glColor3f(0.0,0.0,0.0);

    int s1,s2,exchange,y,x,i;

    float dx,dy,g,temp;

    dx=abs(x2-x1);

    dy=abs(y2-y1);

    x=x1;

    y=y1;

    s1=sign(x2-x1);

    s2=sign(y2-y1);

    if(dy>dx)

    {

        temp=dx;

        dx=dy;

        dy=temp;

        exchange=1;

    }

    else

    {

        exchange=0;

    }

    g=2*dy-dx;

    i=1;

    while(i<=dx)

    {

        glVertex2d(x,y);

        while(g>=0)

        {

            if(exchange==1)

            {

                x=x+s1;

            }

            else

            {

                y=y+s2;

            }

            g=g-2*dx;

        }

        if(exchange==1)

        {

            y=y+s2;

        }

        else

        {

            x=x+s1;

        }

        g=g+2*dy;

        i++;

    }

    glEnd();

}

void init(void)

{

    glClearColor(1.0,1.0,1.0,0.0);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    glOrtho(0,800,0,600,0,600);

}

void display(void)

{

    glClear(GL_COLOR_BUFFER_BIT);

    LineWithBrasenham(100,300,700,300);

    LineWithBrasenham(400,100,400,500);

    glutSwapBuffers();

}

int main(int argc, char** argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

    glutInitWindowSize(800,600);

    glutInitWindowPosition(100,100);

    glutCreateWindow("Brasenham Line Drawing!");

    init();

    glutDisplayFunc(display);

    glutMainLoop();

    return 0;


No comments:

Post a Comment