Showing posts with label Line Drawing. Show all posts
Showing posts with label Line Drawing. 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 ~ $

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


Related Posts Plugin for WordPress, Blogger...