Monday, 7 July 2014

-------------------------------
OSD 1
--------------------------------

#include<iostream>
#include<fstream>
#include<sys/stat.h>
using namespace std;

class inode
{
private:
    char filename[20];
    struct stat buf;
public:
    void get_filename();
    void create_file();
    void display_data();
};

void inode::get_filename()
{
    cout<<"enter the name of file";
    cin>>filename;
}

void inode::create_file()
{
    ofstream write(filename);
    if(write.is_open())
    {
        cout<<"\n\n file "<<filename<<" has been created successfully";
        write.close();
    }
    else
    {
        cout<<"\n\n file can not be created"<<filename;
    }
}
void inode::display_data()
{
    cout<<"\n\n FILE DETAILS";
    cout<<"\n FILENAME \t"<<filename;
    stat(filename,&buf);
    cout<<"\n INODE NUMBER \t"<<buf.st_ino;
    cout<<"\n SIZE \t"<<buf.st_size<<"\n";
    cout<<"\n -------------------------";
}

int main()
{
    inode i;
    i.get_filename();
    i.create_file();
    i.display_data();
    return 0;
}

/*
------------
output
------------

enter the name of file
ashwini


 file ashwini has been created successfully

 FILE DETAILS
 FILENAME     ashwini
 INODE NUMBER     1972343
 SIZE     0

 -------------------------
*/

Monday, 3 March 2014

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;
}
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: 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: 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;
}  

Monday, 24 February 2014

assignment no: 8
// program for polygon

#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;
}

Thursday, 13 February 2014


assignment no: 01

#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,"NULL");
setcolor(4);

poly[0]=50;
poly[1]=50;

poly[2]=600;
poly[3]=50;

poly[4]=600;
poly[5]=380;

poly[6]=poly[0];
poly[7]=poly[1];

rectangle(65,100,150,140);
setcolor(WHITE);
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(65,268,"REGISTER");
rectangle(65,340,200,310);
outtextxy(85,315,"INSTRUCTION");
outtextxy(85,328,"DECODER")
;
line(50,420,650,420);
line(50,450,650,450);
outtextxy(220,430,"ADDRESS AND 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(1000);
getch();
closegraph();
return 0;
}