Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
136 views

Graphics Assignment PDF

The document contains 9 sections describing C programs for graphics assignments. Section 1 draws simple shapes using built-in graphics functions. Section 2 draws a line using the DDA line drawing algorithm. Section 3 draws a line using Bresenham's line drawing algorithm. Section 4 draws a circle using Bresenham's circle drawing algorithm. Section 5 draws a circle using the mid-point circle drawing algorithm. The remaining sections scale a square, draw a polygon, translate a line, and reflect a polygon. The code for each program is provided.

Uploaded by

Sorya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Graphics Assignment PDF

The document contains 9 sections describing C programs for graphics assignments. Section 1 draws simple shapes using built-in graphics functions. Section 2 draws a line using the DDA line drawing algorithm. Section 3 draws a line using Bresenham's line drawing algorithm. Section 4 draws a circle using Bresenham's circle drawing algorithm. Section 5 draws a circle using the mid-point circle drawing algorithm. The remaining sections scale a square, draw a polygon, translate a line, and reflect a polygon. The code for each program is provided.

Uploaded by

Sorya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Graphics Assignment Page 1

1. A C program to draw some simple shapes using some inbuilt functions.


CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main() {
int gd=DETECT,gm,midx,midy;
int arr[] = {330, 150, 465, 30, 450, 200, 330, 150};
initgraph(&gd,&gm,(char*)"");

midx=getmaxx()/2;
midy=getmaxy()/2;

line(0,midy,midx*2,midy);
line(midx,0,midx,midy*2);
line(midx/2,0,midx/2,midy*2);
line(getmaxy(),0,getmaxy(),midy*2);

outtextxy(65,219,(char*)"Line");
outtextxy(220,219,(char*)"Circle");
outtextxy(375,219,(char*)"Polygon");
outtextxy(540,219,(char*)"Sector");

outtextxy(50,459,(char*)"Rectangle");
outtextxy(220,459,(char*)"Ellipse");
outtextxy(390,459,(char*)"Arc");
outtextxy(535,459,(char*)"Pieslice");

//Line
line(10,30,149,170);
//Circle
circle(239,119,75);
//Polygon
drawpoly(4, arr);
//Sector
sector(560,119,30,250,75,40);
//Rectangle
rectangle(10,319,148,379);
//Ellipse
ellipse(239,355,0,360,75,40);
//Arc
arc(400,355,0,150,60);
//Pieslice
pieslice(560,355,20, 340,75);
getch();
}

Graphics Assignment Page 2


Graphics Assignment Page 3
2. A C program to draw a line using DDA Line Drawing Algorithm.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
DDA_LINE(int x1, int y1, int x2, int y2){

int dx,dy,step,i;
float x,y,xinc,yinc;

dx=x2-x1;
dy=y2-y1;

if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);

xinc=dx/(float)step;
yinc=dy/(float)step;
x=x1;
y=y1;

//put all pixels


for(i=0;i<step;i++)
{
putpixel(x,y,15);
x=x+xinc;
y=y+yinc;
}
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
int x1,y1,x2,y2;
printf("Enter the starting coordinates: ");
scanf("%d%d",&x1,&y1);
printf("Enter the ending coordinates: ");
scanf("%d%d",&x2,&y2);
DDA_LINE(x1,y1,x2,y2);
getch();
return 0;
}

Graphics Assignment Page 4


Graphics Assignment Page 5
3. A C program to draw a line using Bresenham’s Line Drawing Algorithm.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void bresenham_line(int x1, int y1, int x2, int y2)
{
int dx,dy,dp,x,y,i=0;
dx=x2-x1;
dy=y2-y1;
dp=2*dy-dx;
x=x1;
y=y1;
while(i<=dx)
{
putpixel(x,y,WHITE);
if(dp<=0)
{
dp=dp+2*dy;
x=x+1;
}
else
{
dp=dp+2*dy-2*dx;
x=x+1;
y=y+1;
}
i++;
}
}
int main()
{
int gd=DETECT,gm;
int x1, y1, x2, y2;
printf ("Enter the strating co-ordinates: ");
scanf("%d%d",&x1 , &y1);
printf ("Enter the ending co-ordinates: ");
scanf("%d%d",&x2 ,&y2);
initgraph (&gd, &gm,(char*)"");
bresenham_line (x1, y1, x2, y2);
getch();
}

Graphics Assignment Page 6


Graphics Assignment Page 7
4. A C program to draw a circle using Bresenham’s Circle Drawing Algorithm.
CODE:-
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void drawCircle(int xc,int yc,int x,int y) {
putpixel(x+xc,y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(-x+xc,y+yc,WHITE);
putpixel(y+xc,x+yc,WHITE);
putpixel(y+xc,-x+yc,WHITE);
putpixel(-y+xc,-x+yc,WHITE);
putpixel(-y+xc,x+yc,WHITE);
}

void BresenhamCircle(int xc,int yc,int r) {


int x=0,y=r,d=3-(2*r);
drawCircle(xc,yc,x,y);

while(x<=y) {
if(d<=0)
d=d+(4*x)+6;
else {
d=d+(4*x)-(4*y)+10;
y=y-1;
}
x=x+1;
drawCircle(xc,yc,x,y);
}
}

int main(void) {
int gd = DETECT, gm, x, y, r;
initgraph(&gd, &gm, (char*)"");

printf("Enter Co-ordinate of centre of circle: ");


scanf("%d%d",&x,&y);
printf("Enter radius of the circle: ");
scanf("%d",&r);
BresenhamCircle(x,y,r);

getch();
return 0;
}

Graphics Assignment Page 8


Graphics Assignment Page 9
5. A C program to draw a circle using Mid-Point Circle Drawing Algorithm.
CODE:-
#include<stdio.h>
#include<graphics.h>
void plot_pixel(int xc, int yc, int x, int y) {
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);
}
void midpoint_circle(int xc, int yc, int r) {
int x,y;
float p;
x=0;
y=r;
p=(5/4)-r;
do {
plot_pixel(xc,yc,x,y);
if(p<0) {
p=p+((2*x)+1);
}
else {
p=p+((2*(x-y))+1);
y--;
}
x++;
} while(x<y);
if(x==y) {
plot_pixel(xc,yc,x,y);
}
}
int main() {
int gd = DETECT, gm;
int xc,yc,r;
printf("Enter the co-ordinate of center of circle: ");
scanf("%d%d",&xc,&yc);
printf("Enter the radious of circle: ");
scanf("%d",&r);
initgraph(&gd,&gm,(char*)"");
midpoint_circle(xc, yc, r);
getch();
}

Graphics Assignment Page 10


Graphics Assignment Page 11
6. A C program to Scale the Square two units in X-direction and two units in Y-direction with
respect to origin.
CODE:-
#include<graphics.h>
void scaling(int a[2][2],int b[2]) {
setcolor(2);
rectangle(a[0][0]+100,a[0][1]+100,a[1][0]+100,a[1][1]+100);
a[0][0]=a[0][0]*b[0];
a[0][1]=a[0][1]*b[1];
a[1][0]=a[1][0]*b[0];
a[1][1]=a[1][1]*b[1];
setcolor(4);
rectangle(a[0][0]+100,a[0][1]+100,a[1][0]+100,a[1][1]+100);
}
int main() {
int gd = DETECT,gm;
initgraph(&gd,&gm,(char*)"");
int a[2][2]={0,0,100,100};
int b[2] = {2,2};
scaling(a,b);
getch();
}

Graphics Assignment Page 12


Graphics Assignment Page 13
7. A C program to draw a Polygon.
CODE:-
#include<graphics.h>
int main() {
int gd = DETECT, gm;
int midx,midy,x1[10],y1[10],n;
initgraph(&gd,&gm,(char*)"");
printf ("How many vertices you want: ");
scanf("%d",&n);
for(int i=0;i<n;i++) {
printf ("Enter the %d co-ordinates: ",i+1);
scanf("%d",&x1[i]);
scanf("%d",&y1[i]);
}
x1[n]=x1[0];
y1[n]=y1[0];
for(int j=0;j<n;j++) {
line(x1[j],y1[j],x1[j+1],y1[j+1]);
}
getch();
}

Graphics Assignment Page 14


Graphics Assignment Page 15
8. A C program to translate a Line.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void translateLine ( int P[2][2], int T[2]) {


int gd = DETECT, gm, errorcode;
initgraph(&gd,&gm,(char*)"");

setcolor (5);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
outtextxy(80,210,(char*)"Original line");

P[0][0] = P[0][0] + T[0];


P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];

setcolor(3);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
outtextxy(440,300,(char*)"Translated line");
getch();
}

int main() {
int P[2][2] = {100, 150, 500, 400}; // coordinates of point
int T[2] = {75, 10}; // translation factor
translateLine (P, T);
return 0;
}

Graphics Assignment Page 16


Graphics Assignment Page 17
9. A C program to show the standards Reflection of Polygon.
CODE:-
#include<stdio.h>
#include<graphics.h>
int main() {
int gd = DETECT, gm;
int midx,midy,x1[10],y1[10],n;
initgraph(&gd,&gm,(char*)"");
midx=getmaxx()/2;
midy=getmaxy()/2;
line(0,midy,midx*2,midy);
printf ("How many vertices you want: ");
scanf("%d",&n);
for(int i=0;i<n;i++) {
printf ("Enter the %d co-ordinates: ",i+1);
scanf("%d",&x1[i]);
scanf("%d",&y1[i]);
}
x1[n]=x1[0];
y1[n]=y1[0];
for(int j=0;j<n;j++) {
line(x1[j],midy-y1[j],x1[j+1],midy-y1[j+1]);
line(x1[j],midy+y1[j],x1[j+1],midy+y1[j+1]);
}
getch();
}

Graphics Assignment Page 18


Graphics Assignment Page 19
10. A C program rotates a Triangle by an angle of 60 with respect to any point.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void TriAngle(int x1, int y1, int x2, int y2, int x3, int y3) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

void Rotate(int x1, int y1, int x2, int y2, int x3, int y3) {
int a1, b1, a2, b2, a3, b3, p = x2, q = y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f", &Angle);
Angle = (Angle * 3.14) / 180;
a1 = p + (x1 - p) * cos(Angle)-(y1 - q) * sin(Angle);
b1 = q + (x1 - p) * sin(Angle)+(y1 - q) * cos(Angle);
a2 = p + (x2 - p) * cos(Angle)-(y2 - q) * sin(Angle);
b2 = q + (x2 - p) * sin(Angle)+(y2 - q) * cos(Angle);
a3 = p + (x3 - p) * cos(Angle)-(y3 - q) * sin(Angle);
b3 = q + (x3 - p) * sin(Angle)+(y3 - q) * cos(Angle);
TriAngle(a1, b1, a2, b2, a3, b3);
}

int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
printf("Enter the 1st point for the triangle:");
scanf("%d%d", &x1, &y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d", &x2, &y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d", &x3, &y3);
initgraph(&gd, &gm, (char*)"");
setcolor(15);
TriAngle(x1, y1, x2, y2, x3, y3);
outtextxy(250,230,(char*)"Original form");
setcolor(2);
Rotate(x1, y1, x2, y2, x3, y3);
outtextxy(80,230,(char*)"Rotation form");
getch();
}

Graphics Assignment Page 20


Graphics Assignment Page 21
11. A C program to draw three concentric circles using mid-point circle generation algorithm.
CODE:-
#include<graphics.h>
void plot_pixel(int xc, int yc, int x, int y) {
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);
}
void midpoint_circle(int xc, int yc, int r) {
int x,y;
float p;
x=0;
y=r;
p=(5/4)-r;
do {
plot_pixel(xc,yc,x,y);
if(p<0) {
p=p+((2*x)+1);
}
else {
p=p+((2*(x-y))+1);
y--;
}
x++;
} while(x<y);
if(x==y) {
plot_pixel(xc,yc,x,y);
}
}
int main()
{
int gd= DETECT,gm;
initgraph(&gd,&gm,(char*)"");
setbkcolor(WHITE);
midpoint_circle(200,300,100);
midpoint_circle(300,200,100);
midpoint_circle(400,300,100);
getch();
}

Graphics Assignment Page 22


Graphics Assignment Page 23
12. A C program to draw some figure and filling the figure using any filling algorithm.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void flood(int x,int y,int fillColor, int defaultColor)
{
if(getpixel(x,y)==defaultColor)
{
putpixel(x,y,fillColor);
flood(x+1,y,fillColor,defaultColor);
flood(x-1,y,fillColor,defaultColor);
flood(x,y+1,fillColor,defaultColor);
flood(x,y-1,fillColor,defaultColor);
}
}
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm,(char*)"");

rectangle(50,50,250,250);
line(50,50,250,250);
line(50,250,250,50);

flood(151,150,1,0);//right
flood(150,151,4,0);//bottom
flood(149,150,2,0);//left
flood(150,149,14,0);//top

getch();
}

Graphics Assignment Page 24

You might also like