graphics programs

Post on 20-Feb-2015

305 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Program: - To Draw line using Bresenham’s line Algorithm

#include<graphics.h>#include<stdio.h>#include<conio.h>#include<math.h>void line_bres(int,int,int,int);void main(){ int gd=DETECT,gm; int d,m,x1,x2,y2,y1; clrscr(); initgraph(&gd,&gm," "); printf("enter the value of x1,y1,x2,y2\n"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); line_bres(x1,y1,x2,y2); getch(); closegraph();}

void line_bres(int x1,int y1,int x2,int y2){ int dx,dy,x,y,xend,p; dx=abs(x2-x1); dy=abs(y2-y1); p=2*(dy)-dx; if(x1>x2) { x=x2; y=y2; xend=x1; } else { x=x1; y=y1; xend=x2; } while(x<xend) { x=x+1; if(p<0) { p=p+(2*dy); }

2

else { y++; p=p+(2*(dy-dx)); } putpixel(x,y,4); } getch();}

OUTPUT

3

Program: - To Draw Circle using Bresenham’s Circle algorithm

#include<graphics.h>#include<stdio.h>#include<conio.h>#include<math.h>void brescircle(int xc,int yc,int rad);void main(){ int gd=DETECT,gm; int r,x1,y1; clrscr(); initgraph(&gd,&gm," "); printf("enter the value of x1,y1,r\n"); scanf("%d%d%d",&x1,&y1,&r); brescircle(x1,y1,r); getch(); closegraph();}

void brescircle(int xc,int yc,int rad){ float d=3-(2*rad),x=0,y=rad; while(x<=y) { x=x+1; putpixel(xc+x,yc+y,4); putpixel(xc+y,yc-x,4); putpixel(xc+y,yc+x,4); putpixel(xc-y,yc-x,4); putpixel(xc-y,yc+x,4); putpixel(xc-x,yc-y,4); putpixel(xc+x,yc-y,4); putpixel(xc-x,yc+y,4); if(d<0) { d=d+((4*x)+6); } else { d=d+((4*(x-y))+10); y--; } x++;

4

}}OUTPUT

5

To Draw a line using DDA Algorithm

#include<graphics.h>#include<stdio.h>#include<conio.h>void main(){ int gd=DETECT,gm; int x0,x,y0,y,m,x1,y1,dy,dx; clrscr(); initgraph(&gd,&gm," "); printf("enter the value of x0,x1,y0,y1\n"); scanf("%d%d%d%d",&x0,&y0,&x1,&y1); dy=y1-y0; dx=x1-x0; m=dy/dx; x=x0; y=y0; if(m<=1) { for(;x<=x1;x++) { putpixel(x,y,2); y=y+m; } } else if(m>1) { for(;y<=y1;y++) { putpixel(x,y,4); x=x+m; } } setcolor(4); getch(); closegraph();}

6

OUTPUT

7

Program:- To Draw Circle using Midpoint Algorithm

#include<conio.h>#include<iostream.h>#include<graphics.h>void main(){ int g=DETECT,d; initgraph(&g,&d," "); int x1,y1,x,y,p,r; x=0; printf("enter the value of x1,y1,r\n"); scanf("%d%d%d",&x1,&y1,&r); y=r; p=1-r; while(x<=y) { putpixel(x1+x,y1+y,4); putpixel(x1+y,y1-x,4); putpixel(x1+y,y1+x,4); putpixel(x1-y,y1-x,4); putpixel(x1-y,y1+x,4); putpixel(x1-x,y1-y,4); putpixel(x1+x,y1-y,4); putpixel(x1-x,y1+y,4); if(p<0) { p=p+2*x+3; } else { p=p+2*(x-y)+5; y--; } x++; } getch();}

8

OUTPUT

9

Program: - To Draw Circle Using Polynomial Equation

#include<graphics.h>#include<stdio.h>#include<conio.h>#include<math.h>void polycircle(int x1,int y1,int rad);void main(){ int gd=DETECT,gm; int x1,y1,r; clrscr(); initgraph(&gd,&gm," "); printf("enter the value of x,y,r\n"); scanf("%d%d%d",&x1,&y1,&r); polycircle(x1,y1,r); getch(); closegraph();}

void polycircle(int x1,int y1,int rad){ int x,xend; x=0; xend=rad/sqrt(2); double y; y=sqrt((rad*rad)-(x*x));

while(x<xend) { putpixel(x1+x,y1+y,4); putpixel(x1+y,y1-x,4); putpixel(x1+y,y1+x,4); putpixel(x1-y,y1-x,4); putpixel(x1-y,y1+x,4); putpixel(x1-x,y1-y,4); putpixel(x1+x,y1-y,4); putpixel(x1-x,y1+y,4); x=x+1; y=sqrt((rad*rad)-(x*x)); }}

10

OUTPUT

11

  Program: - To Draw Circle Using Trigonometry Function

#include<graphics.h>#include<stdio.h>#include<conio.h>#include<math.h>void trignocircle(int x1,int y1,int rad);void main(){ int gd=DETECT,gm; int x1,y1,r; clrscr(); initgraph(&gd,&gm," "); printf("enter the value of x,y,r\n"); scanf("%d%d%d",&x1,&y1,&r); trignocircle(x1,y1,r); getch(); closegraph();}

void trignocircle(int x1,int y1,int rad){ double x,y; double hta; hta=0; while(hta<360) { x=rad*cos(hta); y=rad*sin(hta); putpixel(x1+x,y1+y,4); putpixel(x1+y,y1-x,4); putpixel(x1+y,y1+x,4); putpixel(x1-y,y1-x,4); putpixel(x1-y,y1+x,4); putpixel(x1-x,y1-y,4); putpixel(x1+x,y1-y,4); putpixel(x1-x,y1+y,4); hta=hta+1; }}

12

OUTPUT

13

Program: -To Draw Line using Direct Line Algorithm

#include<conio.h>#include<graphics.h>#include<iostream.h>void main(){

int gd=DETECT,gm;initgraph(&gd,&gm," " );int x1,y1,x2,y2,dx,dy,m,c,x,y;

cout<<”enter the value of x1,y1";cin>>x1>>y1;cout<<"enter the value of x2,y2";cin>>x2>>y2;dx=x2-x1;dy=y2-y1;m=dy/dx;c=y1-m*x1;if(m<=1){

for(x=x1;x<=x2;x++){

y=m*x+c;putpixel(x,y,5);

}}else{

for(y=1;y<y2;y++){

x=(y-c)/m;putpixel(x,y,5);

}}

getch();}

14

OUTPUT

15

Program: -To Draw Hut using Line Function

#include<conio.h>#include<graphics.h>void main(){

int gd=DETECT,gm;initgraph(&gd,&gm,"");setcolor(100);line(100,100,100,300);line(100,300,150,300);line(150,300,150,200);line(150,200,125,200);line(100,100,150,100);line(200,100,200,300);line(200,100,250,100);line(250,100,250,300);line(200,200,250,200);line(350,100,300,100);line(300,100,300,300);line(300,300,350,300);line(350,300,350,200);line(350,200,325,200);line(400,100,400,300);line(400,100,450,100);line(450,100,450,300);line(400,200,450,200);line(500,300,500,100);line(500,100,550,300);line(550,300,550,100);getch();

}

16

OUTPUT

17

  Program: - To Draw Smiley

#include<graphics.h>#include<conio.h>void main(){

int gd=DETECT,gm;initgraph(&gd,&gm,"");circle(300,250,150);line(300,200,300,270);line(230,320,360,320);circle(250,200,15);circle(350,200,15);line(280,320,280,350);line(280,350,300,350);line(300,350,300,320);line(300,340,320,340);line(320,340,320,320);getch();

}

OUTPUT

Program-T0 Draw Ellipse using Polynominal Equation

18

                                                                       #include<iostream.h>#include<conio.h>#include<graphics.h>#include<math.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");int h,k;double a=150,b=100;int x=0;float y,p,z;int C=getmaxx()/2;int D=getmaxy()/2;for(x=0;x<=a;x++){p=(a*a)-(x*x);z=sqrt(p);y=(b/a)*z;putpixel(x+C,y+D,2);putpixel(-x+C,y+D,2);putpixel(x+C,-y+D,2);putpixel(-x+C,-y+D,2);}getch();

}                   

OUTPUT

19

                                  

Program-   To Draw a Ellipse using Trigonometric function

20

#include<graphics.h>#include<process.h>#include<iostream.h>#include<math.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm," ");int a,b,m,n;float x,y;cout<<"Enter the value of Major axes";cin>>a;cout<<"Enter the value of Minor axes";cin>>b;m=getmaxx()/2;n=getmaxy()/2;for(int i=0;i<90;i=i+1){x=a*cos(i);y=b*sin(i);putpixel(x+m,y+n);putpixel(-x+m,y+n);putpixel(x+m,-y+n);putpixel(-x+m,-y+n);}}

OUTPUT

21

PUNJAB COLLEGE OF TECHNICAL EDUCATION(BADDOWAL, LUDHIANA)

22

PCTE INSTITUTIONS(SINCE 1999)

PRACTICAL FILE ON ‘COMPUTER GRAPHICS’

SUBMITTED TO: SUBMITTED BY:MISS. HARJEET KAUR. RITESH BANYAL(FACULTY PCTE) BCA 3A

626232682

INDEX

23

1) To draw line using bresenham’s algorithm.

2) To draw circle using bresenham’s algorithm.

3) To draw a line using DDA algorithm.

4) To draw circle using using mid point algorithm.

5) To draw a circle using polynomial equation.

6) To draw a circle using trigonometric function.

7) To draw a line using direct line algorithms.

8) To draw hut using line function.

9) To draw smiley face.

10) To draw ellipse using polynomial equation.

11) To draw ellipse using trigonometric function.

top related