Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

2/2/2014

Share

Just Programming: Computer Graphics


0

More

Next Blog

Create Blog

Just Programming
Home C Programs C++ Programs Data Structure Programming Q&A
SEARCH TOPICS HERE... CATEGORIES

C++ (25) Java (20) C Program (18) Programming Skills (16) Data Structure (15) Computer Graphics (8) Security Tips (7) UML Modeling (7) Actionscript 3.0 (1) PHP (1) PL/SQL (1)

Showing posts with label Computer Graphics. Show all posts

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 { p=p+2*(x-y)+1; y=y-1; } x=x+1; } if(x==y) plotcircle(xcenter,ycenter,x,y);

POPULAR POSTS

To Implement Graph using Adjacency List in C Program to implement Graph using Adjacency List and traversing using DFS and BFS in C #include<stdio.h> #include<conio.h&... C++ program to check whether a string is palindrome or not Write a function that checks whether a string is Palindrome or not. Use this function to check user-entered strings. Also count and displa... Sparse Matrix using Array in C Program to implement Sparse Matrix using Array in C language #include<stdio.h> #include<conio.h> int spa[10][10]; void... Use case Diagram of Online Airline Reservation System

Use Case Diagram for Online Book Store

FOLLOW BY EMAIL Email address...

FOLLOWERS Join this site


w ith Google Friend Connect

Members (3)

Already a member? Sign in

BLOG ARCHIVE

2013 (11) 2012 (92) November (1) October (4) Mid Point Circle Algorithm Implementation in C

http://keviv03.blogspot.in/search/label/Computer%20Graphics

1/13

2/2/2014
return; }

Just Programming: Computer Graphics


Bresenham Circle Algorithm Implementation in C Cohen Sutherland Line Clipping in C++ How to Lock Windows Desktop using Java September (13) August (8) July (3) June (7) May (39) April (14) January (3) 2011 (1) 2010 (3)

plotcircle(int xcenter,int ycenter,int x,int y) { putpixel(xcenter+x,ycenter+y,10); putpixel(xcenter-x,ycenter+y,10); putpixel(xcenter+x,ycenter-y,10); putpixel(xcenter-x,ycenter-y,10); putpixel(xcenter+y,ycenter+x,10); putpixel(xcenter-y,ycenter+x,10); putpixel(xcenter+y,ycenter-x,10); putpixel(xcenter-y,ycenter-x,10); // asterik lines (2 cross lines) putpixel(xcenter+x,ycenter-x,10); putpixel(xcenter-x,ycenter+x,10); putpixel(xcenter-x,ycenter-x,10); putpixel(xcenter+x,ycenter+x,10); return; }

Posted by vivek kumar


funny (0)

0 comments
interesting (0) cool (0)

Recommend this on Google

Labels:Sci-fi Computer Graphics Reactions:

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

http://keviv03.blogspot.in/search/label/Computer%20Graphics

2/13

2/2/2014
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(); }
Posted by vivek kumar
funny (0)

Just Programming: Computer Graphics

0 comments
interesting (0) cool (0)

Recommend this on Google

Labels:Sci-fi Computer Graphics Reactions:

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;

http://keviv03.blogspot.in/search/label/Computer%20Graphics

3/13

2/2/2014
break; } else if(outcode1 & outcode2) break; else { int x,y;

Just Programming: Computer Graphics

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(outcodeout & RIGHT) { y=y1+(y2-y1)*(xmax-x1)/(x2-x1); x=xmax; } else { y=y1+(y2-y1)*(xmin-x1)/(x2-x1); x=xmin; } if(outcodeout == outcode1) { x1=x; y1=y; outcode1=code(x1,y1,xmin,ymin,xmax,ymax); } else { x2=x; y2=y; outcode2=code(x2,y2,xmin,ymin,xmax,ymax); } } } if(accept) { rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); } } int main() { int x1,y1,x2,y2,tx,ty,xmin,ymin,xmax,ymax; xmin=150; ymin=150; xmax=350; ymax=350; /*initwindow(1000,700,"Cohen Sutherland");*/ cout<<"\n Enter the initial points of the line : "; cout<<"\n X-axis : "; cin>>x1; cout<<" Y-axis : "; cin>>y1; cout<<"\n Enter the final points of the line : "; cout<<"\n X-axis : ";

http://keviv03.blogspot.in/search/label/Computer%20Graphics

4/13

2/2/2014

Just Programming: Computer Graphics


cin>>x2; cout<<" Y-axis : "; cin>>y2; if(x1>x2) { tx=x1,ty=y1; x1=x2,y1=y2; x2=tx,y2=ty; } initwindow(1000,700,"Cohen Sutherland"); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); while(!kbhit()){} cleardevice(); clip(x1,y1,x2,y2,xmin,ymin,xmax,ymax); getch(); while(!kbhit()){} return 0; }
Posted by vivek kumar
funny (0)

0 comments
interesting (0) cool (0)

Recommend this on Google

Labels:Sci-fi Computer Graphics Reactions:

C++ program to implement scaling w.r.t origin


#include"graphics.h" #include<iostream> using namespace std; int main() { int count=0,y,factor[3][3],sx,sy; cout<<"Enter No. of cordinates: "; cin>>y; int **matrix, **m2; matrix=new int*[3]; m2=new int*[3]; for(int i=0;i<y;i++) { matrix[i]=new int[y]; m2[i]=new int[y]; } //initailization for(int i=0;i<y;i++) { for(int j=0;j<3;j++) { if(j==1) { cout<<"\n\nEnter y-axis value for cordinate-"<<i+1<<" = "; cin>>matrix[j][i]; } else if(j==2) matrix[j][i]=1; else { cout<<"\n\nEnter x-axis value for cordinate-"<<i+1<<" = "; cin>>matrix[j][i]; } } }

http://keviv03.blogspot.in/search/label/Computer%20Graphics

5/13

2/2/2014

Just Programming: Computer Graphics


cout<<"\n\nEnter scaling factor for x: "; cin>>sx; cout<<"\n\nEnter scaling factor for y: "; cin>>sy; for(int i=0;i<3;i++) { for(int j=0;j<y;j++) { if(i==0 && j==i) factor[i][j]=sx; else if(i==1 && j==i) factor[i][j]=sy; else if(i==2 && j==i) factor[i][j]=1; else factor[i][j]=0; } } //display cout<<"Object Matrix: \n"; for(int i=0;i<y;i++) { for(int j=0;j<3;j++) { cout<<matrix[i][j]<<" "; } cout<<"\n"; } //display Factor matix cout<<"\n\nFactor Matrix:\n"; for(int i=0;i<3;i++) { for(int j=0;j<y;j++) cout<<factor[i][j]<<" "; cout<<"\n"; }

initwindow(1000,700,"Scaling"); int i,z; for(i=0;i<y-1;i++) { line(matrix[0][i],matrix[1][i],matrix[0][i+1],matrix[1][i+1]); } line(matrix[0][i],matrix[1][i],matrix[0][0],matrix[1][0]); cout<<"\n\nScaled Matrix:\n"; for(int j=0;j<3;j++) { for(int k=0;k<y;k++) { m2[j][k]=0; for(int l=0;l<3;l++) m2[j][k]+=factor[j][l]*matrix[l][k]; cout<<m2[j][k]<<" "; } cout<<"\n"; } for(z=0;z<y-1;z++) { line(m2[0][z],m2[1][z],m2[0][z+1],m2[1][z+1]); } line(m2[0][z],m2[1][z],m2[0][0],m2[1][0]); while(!kbhit()){}

http://keviv03.blogspot.in/search/label/Computer%20Graphics

6/13

2/2/2014
return 0; }

Just Programming: Computer Graphics

Posted by vivek kumar


funny (0)

0 comments
interesting (0) cool (0)

Recommend this on Google

Labels:Sci-fi C++, Computer Graphics Reactions:

C++ program to implement Translation w.r.t origin


#include"graphics.h" #include<iostream> using namespace std; int main() { int count=0,y,factor[3][3],tx,ty; cout<<"Enter No. of cordinates: "; cin>>y; int **matrix, **m2; matrix=new int*[3]; m2=new int*[3]; for(int i=0;i<y;i++) { matrix[i]=new int[y]; m2[i]=new int[y]; } //initailization for(int i=0;i<y;i++) { for(int j=0;j<3;j++) { if(j==1) { cout<<"\n\nEnter y-axis value for cordinate-"<<i+1<<" = "; cin>>matrix[j][i]; } else if(j==2)

http://keviv03.blogspot.in/search/label/Computer%20Graphics

7/13

2/2/2014

Just Programming: Computer Graphics


matrix[j][i]=1; else { cout<<"\n\nEnter x-axis value for cordinate-"<<i+1<<" = "; cin>>matrix[j][i]; } } } cout<<"\n\nEnter translation factor for x: "; cin>>tx; cout<<"\n\nEnter translation factor for y: "; cin>>ty; for(int i=0;i<3;i++) { for(int j=0;j<y;j++) { if(i==j) factor[i][j]=1; else if(i==0 && j==2) factor[i][j]=tx; else if(i==1 && j==2) factor[i][j]=ty; else factor[i][j]=0; } } //display cout<<"Object Matrix: \n"; for(int i=0;i<y;i++) { for(int j=0;j<3;j++) { cout<<matrix[i][j]<<" "; } cout<<"\n"; } //display Factor matix cout<<"\n\nFactor Matrix:\n"; for(int i=0;i<3;i++) { for(int j=0;j<y;j++) cout<<factor[i][j]<<" "; cout<<"\n"; }

initwindow(1000,700,"Translation"); int i,z; for(i=0;i<y-1;i++) { line(matrix[0][i],matrix[1][i],matrix[0][i+1],matrix[1][i+1]); } line(matrix[0][i],matrix[1][i],matrix[0][0],matrix[1][0]); cout<<"\n\nTranslated Matrix:\n"; for(int j=0;j<3;j++) { for(int k=0;k<y;k++) { m2[j][k]=0; for(int l=0;l<3;l++) m2[j][k]+=factor[j][l]*matrix[l][k]; cout<<m2[j][k]<<" "; } cout<<"\n"; }

http://keviv03.blogspot.in/search/label/Computer%20Graphics

8/13

2/2/2014

Just Programming: Computer Graphics


for(z=0;z<y-1;z++) { line(m2[0][z],m2[1][z],m2[0][z+1],m2[1][z+1]); } line(m2[0][z],m2[1][z],m2[0][0],m2[1][0]); while(!kbhit()){} return 0; }

Posted by vivek kumar


funny (0)

0 comments
interesting (0) cool (0)

Recommend this on Google

Labels:Sci-fi C++, Computer Graphics Reactions:

C program to implement bresenham's line drawing algorithm


#include<graphics.h> #include<conio.h> #include<math.h> void bress(float x1,float y1,float x2,float y2) { int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; if(dx>dy) { p=2*dy-dx; if(x1<x2) { x=x1;y=y1;end=x2; if(y1<y2)inc=1; if(y1>y2)inc=-1; } else { x=x2;y=y2;end=x1; if(y2<y1)inc=1; if(y2>y1)inc=-1; } while(x<=end) { putpixel(x,y,15); if(p<0) p=p+2*dy;

http://keviv03.blogspot.in/search/label/Computer%20Graphics

9/13

2/2/2014
else { y=y+inc;p=p+2*(dy-dx); } x++; } } else { p=2*dx-dy; if(y1<y2) { y=y1;x=x1;end=y2; if(x1<x2)inc=1; if(x1>x2)inc=-1; } else { y=y2;x=x2;end=y1; if(x2<x1)inc=1; if(x2>x1)inc=-1; } while(y<=end) { putpixel(x,y,15); if(p<0)p=p+2*dx; else { x=x+inc;p=p+2*(dx-dy); } y++; if(current==0&&c==10) { current=1;c=-1; } if(current==1&&c==6) { current=0;c=-1; } c++; } } } void main() { int gdriver=DETECT,gmode,i,j,c=0; initgraph(&gdriver,&gmode,"..\\bgi"); for(i=1;i<=5;i++) { bress(100+c,100+c,300+c,100+c); bress(100+c,100+c,100+c,200+c); bress(300+c,100+c,300+c,200+c); bress(100+c,200+c,300+c,200+c); c+=10; } getch(); closegraph(); getch(); }
Posted by vivek kumar
funny (0)

Just Programming: Computer Graphics

0 comments
interesting (0) cool (0)

Recommend this on Google

Labels:Sci-fi Computer Graphics Reactions:

http://keviv03.blogspot.in/search/label/Computer%20Graphics

10/13

2/2/2014

Just Programming: Computer Graphics

C program to implement DDA algorithm


#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #define round(a) ((int) (a+0.5)) void lineDDA(int xa,int ya,int xb,int yb); int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int xa,ya,xb,yb; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "..\\bgi"); lineDDA(0,0,640,480); lineDDA(640,0,0,480); lineDDA(320,0,320,480); lineDDA(0,240,640,240); getch(); closegraph(); } void lineDDA(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya,steps,k; float xinc,yinc,x=xa,y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float) steps; yinc=dy/(float) steps; putpixel(round(x),round(y),5); for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(round(x),round(y),2); } }

Posted by vivek kumar


funny (0)

0 comments
interesting (0) cool (0)

Recommend this on Google

Labels:Sci-fi Computer Graphics Reactions:

http://keviv03.blogspot.in/search/label/Computer%20Graphics

11/13

2/2/2014

Just Programming: Computer Graphics

C++ program to show simple animation in Visual Studio 2008


#include "graphics.h" int main( ) { initwindow(1000, 700, "Animation"); for(int i=0;i<47;i++) //for +x-axis { line(500,0,500,1000); line(0,350,1000,350); circle(530+(i*10),320,30); delay(100); clearviewport(); } for(int i=0;i<47;i++) //for -x-axis { line(500,0,500,1000); line(0,350,1000,350); circle(0+(i*10),320,30); delay(100); clearviewport(); } for(int i=0;i<32;i++) //for +y-axis { line(500,0,500,1000); line(0,350,1000,350); circle(470,320-(i*10),30); delay(100); clearviewport(); } for(int i=0;i<32;i++) //for -y-axis { line(500,0,500,1000); line(0,350,1000,350); circle(470,700-(i*10),30); delay(100); clearviewport(); } while(!kbhit()) { outtextxy(500,350,"Press any key to exit..."); delay(100); } return 0; }

http://keviv03.blogspot.in/search/label/Computer%20Graphics

12/13

2/2/2014

Just Programming: Computer Graphics

Posted by vivek kumar


funny (0)

0 comments
interesting (0) cool (0)

Recommend this on Google

Labels:Sci-fi Computer Graphics Reactions:

Home
Subscribe to: Posts (Atom)

Older Posts

COPYRIGHT PROTECTION

Vivek Kumar. Powered by Blogger.

http://keviv03.blogspot.in/search/label/Computer%20Graphics

13/13

You might also like