Skip to main content

Posts

Showing posts from October, 2012

Mid Point Circle Algorithm Implementation in C

#include<graphics.h> #include<stdio.h> #include<conio.h> #include<dos.h> void main() {  int i,x,y,r,p,midx,midy;  int gd=DETECT,gm,xcenter,ycenter;  printf("Enter the radius  :");  scanf("%d",&r);  initgraph(&gd,&gm,"..\\bgi");  midx=getmaxx()/2;  midy=getmaxy()/2;  xcenter=midx;  ycenter=midy;  x=0;  y=r;  p=1-r;  midpoint(xcenter,ycenter,x,y,p); // to draw circle  //to draw asterik (2 horizontal n vertical lines)  //inside the circle.  for(i=0;i<=r;i++)  { putpixel(xcenter+i,ycenter,10);    putpixel(xcenter-i,ycenter,10);    putpixel(xcenter,ycenter+i,10);    putpixel(xcenter,ycenter-i,10);  }  getch();  closegraph(); }  midpoint(int xcenter,int ycenter,int x,int y,int p)  {  while(x < y)  {  plotcircle(xcenter,ycenter,x,y);  if(p<0)  p=p+2*x+1;  else  { ...

Bresenham Circle Algorithm Implementation in C

# include<stdio.h> # include<conio.h> # include<graphics.h> void main() {  int gd=DETECT,gm;  int r,x,y,p,xc=320,yc=240,i;  initgraph(&gd,&gm,"..\\BGI");  printf("Enter the radius ");  scanf("%d",&r);  for(i=0;i<10;i++);  {  x=0;  y=r;  //putpixel(xc+x,yc-y,15);  p=3-(2*r);  for(x=0;x<=y;x++)  {  if (p<0)  {   y=y;   p=(p+(4*x)+6);  }  else  {   y=y-1;   p=p+((4*(x-y)+10));  }  putpixel(xc+x,yc-y,15);  putpixel(xc-x,yc-y,15);  putpixel(xc+x,yc+y,15);  putpixel(xc-x,yc+y,15);  putpixel(xc+y,yc-x,15);  putpixel(xc-y,yc-x,15);  putpixel(xc+y,yc+x,15);  putpixel(xc-y,yc+x,15);  }  r=r-10;  //xc=xc-5;  //yc=yc-5;  }  getch();  closegraph(); }

Cohen Sutherland Line Clipping in C++

#include<iostream> using namespace std; #include "graphics.h" #define INSIDE 0 #define LEFT 1 #define RIGHT 2 #define BOTTOM 4 #define TOP 8 int code(int x,int y,int xmin,int ymin,int xmax,int ymax) { int outcode; if(x<xmin) outcode=LEFT; else if(x>xmax) outcode=RIGHT; else if(y<ymin) outcode=BOTTOM; else if(y>xmax) outcode=TOP; else outcode=INSIDE; return outcode; } void clip(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax) { int outcode1=code(x1,y1,xmin,ymin,xmax,ymax); int outcode2=code(x2,y2,xmin,ymin,xmax,ymax); bool accept=false; while(true) { if(!(outcode1 | outcode2)) { accept=true; break; } else if(outcode1 & outcode2) break; else { int x,y; int outcodeout=outcode1?outcode1:outcode2; if(outcodeout & TOP) { x=x1+(x2-x1)*(ymax-y1)/(y2-y1); y=ymax; } else if(outcodeout & BOTTOM) { x=x1+(x2-x1)*(ymin-y1)/(y2-y1); y=ymin; } else if...