Showing posts with label Computer Graphics. Show all posts
Showing posts with label Computer Graphics. Show all posts

Basic Shapes in Graphics.h - Ubuntu - Graphics & Multimedia Lab - C++ Program

Basic Shapes in Graphics.h
1. Line (x0,y0,x1,y1)
2. Rectangle(left,top,right,bottom)
3. Circle(x0,y0,radius)
4. Ellipse(x0,y0,startangle,endangle,xaxis,yaxis)

Sourcecode:


#include<graphics.h>
using namespace std;

int main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm,NULL);
    
    outtextxy(150,15, "Some Basic Shapes in Graphics.h");      
    
    line(10,50,500,50);
    outtextxy(501,60,"Line");
        
    circle(150,150,50);
    outtextxy(201,201, "Circle");
        
    rectangle(350,100,500,200);
    outtextxy(505, 211, "Rectangle");   
        
    ellipse(300, 300,0,360, 100,50);
    outtextxy(440, 330, "Ellipse");
 
    
    getch();
    closegraph();
    return 0;
}

Output:
nn@linuxmint ~ $ g++ g1.cpp -lgraph
g1.cpp: In function ‘int main()’:
g1.cpp:12: warning: deprecated conversion from string constant to ‘char*’
g1.cpp:15: warning: deprecated conversion from string constant to ‘char*’
g1.cpp:18: warning: deprecated conversion from string constant to ‘char*’
g1.cpp:21: warning: deprecated conversion from string constant to ‘char*’
g1.cpp:24: warning: deprecated conversion from string constant to ‘char*’
nn@linuxmint ~ $ ./a.out
XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
      after 121 requests (117 known processed) with 0 events remaining.
nn@linuxmint ~ $

Cohen-Sutherland Line Clipping Algorithm - Graphics & Multimedia Lab - C++ Program

Cohen-Sutherland Line Clipping Algorithm (2)

  C++ program - Graphics & Multimedia Lab
Sourcecode:
/* 
 * File:   cohsuthlineclip.cpp
 * Author: nn
 *
 * Created on 12 January, 2012, 11:30 AM
 *  
 * Cohen–Sutherland clipping algorithm clips a line from
 *P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with 
 *diagonal from (xmin, ymin) to (xmax, ymax).
 */

#include <cstdlib>
#include <graphics.h>
#define xmax 350
#define ymax 350
#define xmin 100
#define ymin 100
using namespace std;

typedef int OutCode;
  
const int INSIDE = 0; // 0000
const int LEFT = 1;   // 0001
const int RIGHT = 2;  // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8;    // 1000

OutCode ComputeOutCode(double x, double y)
{
        OutCode code;
 
        code = INSIDE;          // initialised as being inside of clip window
 
        if (x < xmin)           // to the left of clip window
                code |= LEFT;
        else if (x > xmax)      // to the right of clip window
                code |= RIGHT;
        if (y < ymin)           // below the clip window
                code |= BOTTOM;
        else if (y > ymax)      // above the clip window
                code |= TOP;
 
        return code;
}
void CohenSutherlandLineClipAndDraw(double x0, double y0, double x1, double y1)
{ 
        OutCode outcode0 = ComputeOutCode(x0, y0);
        OutCode outcode1 = ComputeOutCode(x1, y1);
        bool accept = false;
 
        while (true) {
                if (!(outcode0 | outcode1)) {//Bitwise OR is 0. Trivially accept and get out of loop
                        accept = true;
                        break;
                } else if (outcode0 & outcode1) {//Bitwise AND is not 0. Trivially reject and get out of loop
                        break;
                } 
                else {

                        double x, y;

                        OutCode outcodeOut = outcode0? outcode0 : outcode1;
                        if (outcodeOut&TOP){           // point is above the clip rectangle
                                x = x0 + (x1-x0)*(ymax-y0)/(y1-y0);
                                y = ymax;
                        } else if(outcodeOut & BOTTOM) { // point is below the clip rectangle
                                x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
                                y = ymin;
                        } else if (outcodeOut & RIGHT) {  // point is to the right of clip rectangle
                                y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
                                x = xmax;
                        } else if (outcodeOut & LEFT) {   // point is to the left of clip rectangle
                                y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
                                x = xmin;
                        }

                        if (outcodeOut == outcode0) {
                                x0 = x;
                                y0 = y;
                                outcode0 = ComputeOutCode(x0, y0);
                        } else {
                                x1 = x;
                                y1 = y;
                                outcode1 = ComputeOutCode(x1, y1);
                        }
                }
               rectangle(xmin, ymin, xmax, ymax);
                line(x0, y0, x1, y1);
                getch();
               // cleardevice();
        }
        
        if(accept) {
               
               rectangle(xmin, ymin, xmax, ymax);
               line(x0, y0, x1, y1);
               printf("\n\tpoints p0=(%d,%d) p1=(%d,%d)",(int)x0,(int)y0,(int)x1,(int)y1);
        }
}

int main(int argc, char** argv)
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);
    outtextxy(300,10,"BEFORE LINE CLIPPING");
    rectangle(xmin,ymin,xmax,ymax);
    line(10,200,350,300);
    getch();
    cleardevice();
    outtextxy(300,10,"AFTER LINE CLIPPING");
    CohenSutherlandLineClipAndDraw(10,200,350,300);
    getch();
    closegraph();
    return 0;
}




Output:

nn@linuxmint ~ $ g++ cohsuthlineclip.cpp -lgraph
nn@linuxmint ~ $ g++ cohsuthlineclip.cpp -lgraph
coh.cpp: In function ‘int main(int, char**)’:
coh.cpp:105: warning: deprecated conversion from string constant to ‘char*’
coh.cpp:110: warning: deprecated conversion from string constant to ‘char*’
nn@linuxmint ~ $ ./a.out 


Midpoint Line Algorithm - Line Drawing C++ program - Graphics & Multimedia Lab

Midpoint Line Algorithm 

Line Drawing C++ program - Graphics & Multimedia Lab


Sourcecode:



//MIDPOINT ALGORITHM



#include<graphics.h>

#include<iostream>

using namespace std;


int count,points[1000];

int i=0;

int gd=DETECT,gm;

int np;



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

 {

 int maxy=getmaxy();

 int dx=x1-x0;

 int dy=y1-y0;

 int d=2*dy-dx;

 int incE=2*dy;

 int incNE=2*(dy-dx);

 int x=x0;

 int y=y0;



 putpixel(x,(maxy-y),WHITE);

 points[i]=x;

 points[i+1]=y;

 i+=2;

 while(x<x1)

  {

  if(d<=0)

   {

   d=d+incE;

   x++;

   }

  else

   {

   d=d+incNE;

   x++;

   y++;

   }

  putpixel(x,(maxy-y),WHITE);

  points[i]=x;

  points[i+1]=y;

  i+=2;

  }

 }



main()

 {
  int x0,y0,x1,y1;

 FILE *fp;

 initgraph(&gd,&gm,NULL);



 fp=fopen("inlpoints.txt","r");

 if(fp==NULL)

  {

  printf("ERROR IN READING FILE !");

  getch();

  exit(0);

  }



 fscanf(fp,"%d",&x0);

 fscanf(fp,"%d",&y0);

 fscanf(fp,"%d",&x1);

 fscanf(fp,"%d",&y1);

 fclose(fp);



 midLine(x0,y0,x1,y1);

 fp=fopen("outlpoints.txt","w");

 fprintf(fp,"%s","Plotted points are:-\n");



 count=i;

 np=0;

 for(i=0;i<count;i+=2)

  {

  fprintf(fp,"(%d, %d) ",points[i],points[i+1]);

  np++;

  if(np==5) { fprintf(fp,"\n"); np=0; }

  }

 fclose(fp);



 getch();

 closegraph();

 return(0);

 }
Output:

nn@linuxmint ~ $ g++ lab2.cpp -lgraph
nn@linuxmint ~ $ ./a.out

"inlpoints.txt"
22 22 72 40


"outlpoints.txt"
Plotted points are:-
(22, 22) (23, 22) (24, 23) (25, 23) (26, 23) 
(27, 24) (28, 24) (29, 25) (30, 25) (31, 25) 
(32, 26) (33, 26) (34, 26) (35, 27) (36, 27) 
(37, 27) (38, 28) (39, 28) (40, 28) (41, 29) 
(42, 29) (43, 30) (44, 30) (45, 30) (46, 31) 
(47, 31) (48, 31) (49, 32) (50, 32) (51, 32) 
(52, 33) (53, 33) (54, 34) (55, 34) (56, 34) 
(57, 35) (58, 35) (59, 35) (60, 36) (61, 36) 
(62, 36) (63, 37) (64, 37) (65, 37) (66, 38) 
(67, 38) (68, 39) (69, 39) (70, 39) (71, 40) 
(72, 40) 

DDA Line Drawing Algorithm - C++ program - Graphics & Multimedia Lab

DDA Line Drawing Algorithm 

Line Drawing - C++ implementation - Graphics & Multimedia Lab


Sourcecode:


//DDA LINE DRAWING ALGORITHM

#include<graphics.h>

#include<iostream>

using namespace std;



int gd=DETECT,gm,maxy,points[1000];

float dx,dy,m,tmp,x,y;

FILE *fp;

int a0,b0,a1,b1;

int i,count,np;



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

 {

  i=0;

  dx=x1-x0;

  dy=y1-y0;

  maxy=getmaxy();



  if(dx==0)

   {

    for(y=y0;y<=y1;y++)

    {

     putpixel(x0,y,WHITE);

     points[i]=x0;

     points[i+1]=y;

     i+=2;

    }

   }



  else if(dy==0)

   {

    for(x=x0;x<=x1;x++)

     {

      putpixel(x,y0,WHITE);

      points[i]=x0;

      points[i+1]=y;

      i+=2;

     }

   }



  else if(dy<=dx)

   {

    m=dy/dx;

    y=y0;

    for(x=x0;x<=x1;x++)

     {

      putpixel(x,(int)(maxy-y),WHITE);

      points[i]=x;

      points[i+1]=(int)y;

      i+=2;

      y=y+m;

     }

   }



  else

   {

    m=dx/dy;

    x=x0;

    for(y=y0;y<=y1;y++)

     {

      putpixel((int)x,maxy-y,WHITE);

      points[i]=(int)x;

      points[i+1]=y;

      i+=2;

      x=x+m;

     }

   }

 }



main()

 {

  initgraph(&gd,&gm,NULL);



  fp=fopen("linput.txt","r");

  if(fp==NULL)

   {

    printf("eRROR !");

    getch();

    exit(0);

   }



  fscanf(fp,"%d",&a0);

  fscanf(fp,"%d",&b0);

  fscanf(fp,"%d",&a1);

  fscanf(fp,"%d",&b1);

  fclose(fp);



  DDALine(a0,b0,a1,b1);



  fp=fopen("loutput.txt","w");

  fprintf(fp,"%s","Plotted points are:-\n");



  count=i;

  np=0;

  for(i=0;i<count;i+=2)

   {

    fprintf(fp,"(%d, %d) ",points[i],points[i+1]);

    np++;

    if(np==5) { fprintf(fp,"\n"); np=0; }

   }

  fclose(fp);



  getch();

  closegraph();

  return 0;

 }
Output: 
nn@linuxmint ~ $ g++ lab1.cpp -lgraph
nn@linuxmint ~ $ ./a.out


"linput.txt"
18 22 70 40


Plotted points are:-
(18, 22) (19, 22) (20, 22) (21, 23) (22, 23) 
(23, 23) (24, 24) (25, 24) (26, 24) (27, 25) 
(28, 25) (29, 25) (30, 26) (31, 26) (32, 26) 
(33, 27) (34, 27) (35, 27) (36, 28) (37, 28) 
(38, 28) (39, 29) (40, 29) (41, 29) (42, 30) 
(43, 30) (44, 30) (45, 31) (46, 31) (47, 32) 
(48, 32) (49, 32) (50, 33) (51, 33) (52, 33) 
(53, 34) (54, 34) (55, 34) (56, 35) (57, 35) 
(58, 35) (59, 36) (60, 36) (61, 36) (62, 37) 
(63, 37) (64, 37) (65, 38) (66, 38) (67, 38) 
(68, 39) (69, 39) (70, 39) 

A Simple Program to Draw a Line - - Ubuntu (libgraph) - Graphics & Multimedia Lab

A Simple C++ Program to Draw a Line 
  Ubuntu (libgraph) - Graphics & Multimedia Lab

Program:

//
#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
 int gd=DETECT,gm,x,y;
 initgraph(&gd,&gm,NULL);
 line(0,0,100,100);
 getch();
 closegraph();
 return 0;
}


Output:

nn@linuxmint ~ $ g++ cg2.cpp -lgraph
nn@linuxmint ~ $ ./a.out


Computer Graphics - James D Foley, Van Dam A, Steven and Hughes- Computer Graphics and Multimedia



Computer Graphics
James D Foley, Van Dam A, Steven and Hughes








Related Posts Plugin for WordPress, Blogger...