This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Showing posts with label computer graphics using c program. Show all posts
Showing posts with label computer graphics using c program. Show all posts

Sunday, November 25, 2012

DRAW A CIRCLE USING MIDPOINT ALGORITHM


DRAW A CIRCLE USING MIDPOINT ALGORITHM


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>

void main()
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int x1,x2,x3,x4,xc,yc,r,y1,y2;
   char msg[15];
   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");
   /* read result of initialization */
   errorcode = graphresult();
   /* an error occurred */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   }
   printf("Enter the value for xc,yc,r:");
   scanf("%d%d%d",&xc,&yc,&r);
   circlemidpoint(xc,yc,r);
   getch();
   closegraph();
}
void circlemidpoint(int xc,int yc,int r)
{
int x=0,y=r,p=1-r;
void circleplotpoints(int,int,int,int);
circleplotpoints(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
circleplotpoints(xc,yc,x,y);
}
}
 void circleplotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,YELLOW);
putpixel(xc-x,yc+y,YELLOW);
putpixel(xc+x,yc-y,GREEN);
putpixel(xc-x,yc-y,GREEN);
putpixel(xc+y,yc+x,GREEN);
putpixel(xc-y,yc+x,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-y,yc-x,RED);
}


































OUTPUT:-



Enter the value for xc, yc, r:
250
150
125


         




DRAW A LINE USING DDA ALGORITHM


DRAW A LINE USING DDA ALGORITHM

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
#define ROUND(a)((int)(a+0.5))
int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int xa,xb,ya,yb;
  // int y=0,x=0,i;
   char msg[25];
   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   /* an error occurred */
   if (errorcode != grOk)
  {
       printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
  }
 printf("Enter the value for x1,y1:\n");
 scanf("%d%d",&xa,&ya);
 printf("Enter the value for x2,y2:\n");
 scanf("%d%d",&xb,&yb);
 linedda(xa,ya,xb,yb);
 getch();
 closegraph();
  return 0;
}


void linedda(int xa,int ya,int xb,int yb)
{
int dx=xb-xa,dy=yb-ya,steps,k;
float xincr,yincr,x=xa,y=ya;
if(abs(dx)>abs(dy))
steps=abs(dy);
else
steps=abs(dy);
xincr=dx/(float)steps;
yincr=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),GREEN);
for(k=0;k<steps;k++)
{
x+=xincr;
y+=yincr;
putpixel(ROUND(x),ROUND(y),GREEN);
}
}







































OUTPUT:-


Enter the value for x1,y1:
100
100
Enter the value for x2,y2:
250
300





DRAW A LINE USING BRESENHAM ALGORITHM


DRAW A LINE USING BRESENHAM ALGORITHM

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>

void main()
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int xa,xb,ya,yb,x,y;
   char msg[25];
   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   /* an error occurred */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
  }
  printf("Enter the x1 and y1 value:\n");
  scanf("%d%d",&xa,&ya);
  printf("Enter the x2 and y2 value:\n");
  scanf("%d%d",&xb,&yb);
  linebres(xa,ya,xb,yb);
  getch();
  closegraph();
}
           
 void linebres(int xa,int ya,int xb,int yb)
{
            int dx=abs(xa-xb),dy=abs(ya-yb);
            int p=2*dy-dx;
            int twody=2*dy,twodxdy=2*(dy-dx);
            int x,y,xend;
            if(xa>xb)
            {
                    x=xb;y=yb;xend=xa;
            }
           

            else
            {
                        x=xa;y=ya;xend=xb;
            }
            putpixel(x,y,YELLOW);
            while(x<xend)
            {
                        x++;
                        if(p<0)
                                    p+=twody;
                        else
                        {
                                    y++;
                                    p+=twodxdy;
                        }
                        putpixel(x,y,YELLOW);
            }
}






























OUTPUT:-


Enter the x1 and y1 value:
100
100
Enter the x2 and y2value:
250
300














DRAW A HOUSE USING GRAPHIC FUNCTIONS


DRAW A HOUSE USING GRAPHIC FUNCTIONS


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void main()
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int xmax, ymax;
   int midx, midy;
   int radius = 10;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");
   /* read result of initialization */
   errorcode = graphresult();
   /* an error occurred */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   }

   setcolor(getmaxcolor());
   xmax = getmaxx();
   ymax = getmaxy();

   /* draw a diagonal line */

   line(150,150,400,150);
   line(150,300,400,300);
   line(150,150,150,300);
   line(400,150,400,300);
   line(150,150,275,50);

   line(400,150,275,50);
   line(275,50,500,50);

   line(400,150,625,150);
   line(500,50,625,150);
   line(625,150,625,300);
   line(400,300,625,300);

   //door
   line(250,300,250,250);
   line(300,300,300,250);
   line(250,250,300,250);
   line(250,300,275,290);
   line(250,250,275,260);
   line(275,290,275,260);

   //window
   line(450,200,550,200);
   line(450,250,550,250);
   line(450,200,450,250);
   line(550,200,550,250);
   line(450,225,550,225);
   line(500,200,500,250);
   circle(275,100,15);
   /* clean up */
   getch();
   closegraph();
}



























OUTPUT:-






























PRINT A TEXT USING GRAPHIC FUNCTIONS


PRINT A TEXT USING GRAPHIC FUNCTIONS


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
            int gd=DETECT,gm,errorcode;
            int y=0,x=0;
            int i;
            char msg[80];
            initgraph(&gd,&gm,"");
            errorcode=graphresult();
            if(errorcode!=grOk)
            {
                        printf("Graphics error %s\n",grapherrormsg(errorcode));
                        printf("Press any key to halt");
                        getch();
                        exit(1);
            }

            for(i=1;i<=6;i++)
            {
                        settextstyle(TRIPLEX_FONT,HORIZ_DIR,i);
                        sprintf(msg,"Text");
                        outtextxy(x,y,msg);
                        y+=textheight(msg);
                        x+=textwidth(msg);
            }
            getch();
            closegraph();
}













OUTPUT:-


Text
       Text
            Text
               Text
                 Text
                Text