0% found this document useful (0 votes)
330 views11 pages

GD Graphics Driver (Detects Best Graphics Driver and Assigns It As Default, GM Graphics Mode

The document contains C code for graphics algorithms including DDA line drawing, midpoint circle generation, ellipse generation, and 2D transformations including translation, scaling, and rotation. The code includes function definitions and main functions that get input from the user, perform the algorithms, and display output.

Uploaded by

Samiksha Bargude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
330 views11 pages

GD Graphics Driver (Detects Best Graphics Driver and Assigns It As Default, GM Graphics Mode

The document contains C code for graphics algorithms including DDA line drawing, midpoint circle generation, ellipse generation, and 2D transformations including translation, scaling, and rotation. The code includes function definitions and main functions that get input from the user, perform the algorithms, and display output.

Uploaded by

Samiksha Bargude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

#include<stdio.

h>
#include<graphics.h>
#include<math.h>
float round(float a);
void main()
{
int gd=DETECT,gm;

// gd=graphics driver (detects best graphics driver and assigns it as default, gm=graphics mode.
int x1,y1,x2,y2,steps,k;
float xincr,yincr,x,y,dx,dy;
printf("enter x1,y1");

scanf("%d%d",&x1,&y1);

printf("enter x2,y2");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"c:\\turboc3\\BGI"); //initializes the graph
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))

steps=abs(dx);
else
steps=abs(dy);
xincr=dx/steps;
yincr=dy/steps;
x=x1;

y=y1;
for(k=1;k<=steps;k++)
{
delay(100); //for seeing the line drawing process slowly.
x+=xincr;
y+=yincr;
putpixel(round(x),round(y),WHITE);

}
outtextxy(200,20,"DDA");
// for printing text at desired screen location.
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch();
closegraph();
// closes the graph and comes back to previous graphic mode.

float round(float a)
{
int b=a+0.5;
return b;
}
Midpoint Circle Generation Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void draw_circle(int, int, int);


void symmetry(int, int, int, int);

void main()
{
int xc, yc, R;
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

printf("Enter the center of the circle:\n");


printf("Xc =");
scanf("%d", &xc);
printf("Yc =");
scanf("%d", &yc);

printf("Enter the radius of the circle :");


scanf("%d", &R);

draw_circle(xc, yc, R);

getch();
closegraph();
}

void draw_circle(int xc, int yc, int rad)


{
int x = 0, y = rad, p = 1-rad;
symmetry(x, y, xc, yc);
for(x=0; y>x; x++)
{
if(p<0)
{ p += 2 * x + 3; }
else
{ p += 2 * (x - y) + 5;
y--;
}

symmetry(x, y, xc, yc);


delay(50);
}
}

void symmetry(int x, int y, int xc, int yc)


{
putpixel(xc+x, yc-y, GREEN); //For pixel (x, y)
delay(50);
putpixel(xc+y, yc-x, GREEN); //For pixel (y, x)
delay(50);
putpixel(xc+y, yc+x, GREEN); //For pixel (y, -x)
delay(50);
putpixel(xc+x, yc+y, GREEN); //For pixel (x, -y)
delay(50);
putpixel(xc-x, yc+y, GREEN); //For pixel (-x, -y)
delay(50);
putpixel(xc-y, yc+x, GREEN); //For pixel (-y, -x)
delay(50);
putpixel(xc-y, yc-x, GREEN); //For pixel (-y, x)
delay(50);
putpixel(xc-x, yc-y, GREEN); //For pixel (-x, y)
delay(50);

}
Ellipse Generation Algorithm

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

void disp();
float x, y;
int xc, yc;

void main()

{
int gd = DETECT, gm;
int rx, ry;
float p1, p2;
clrscr();

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

printf("Enter the centre point :");


scanf("%d %d", &xc, &yc);

printf("Enter the value for Rx and Ry :");


scanf("%d %d", &rx, &ry);

x = 0;
y = ry;

disp();
p1 = (ry * ry) - (rx * rx * ry) + (rx * rx) / 4;
while((2.0 * ry * ry * x) <= (2.0 * rx * rx * y))
{
x++;
if(p1 <= 0)
{ p1 = p1 + (2.0 * ry * ry * x) + ( ry * ry); }
else
{ y--;
p1 = p1 + (2.0 * ry * ry * x) - (2.0 * rx * rx * y) + (ry *ry);
}

disp();
x =-x;

disp();
x =-x;
}

x = rx;
y = 0;

disp();
p2 = (rx * rx) + 2.0 * (ry * ry * rx) + (ry * ry) / 4;

while((2.0 * ry * ry * x) > (2.0 * rx * rx * y))


{
y++;
if(p2 > 0)
{ p2 = p2 + (rx * rx) - (2.0 * rx * rx * y); }
else
{ x--;
p2 = p2 + (2.0 * ry * ry * x) - (2.0 * rx * rx * y) + (rx * rx);
}

disp();
y =-y;

disp();
y =-y;
}

getch();
closegraph();
}

void disp()
{
delay(50);
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
}
Program for translation:

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

void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("Enter the starting point of line segment:");


scanf("%d %d",&x1,&y1);

printf("Enter the ending point of line segment:");


scanf("%d %d",&x2,&y2);

printf("Enter translation distances tx,ty:\n");


scanf("%d%d",&tx,&ty);

setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");

x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=y2+ty;

setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x4+2,y4+2,"Line after translation");

getch();
}

Program for scaling:

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

void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,sx,sy,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("Enter the starting point coordinates:");


scanf("%f %f",&x1,&y1);

printf("Enter the ending point coordinates:");


scanf("%f %f",&x2,&y2);

printf("Enter scaling factors sx,sy:\n");


scanf("%f%f",&sx,&sy);

setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");

x3=x1*sx;
y3=y1*sy;
x4=x2*sx;
y4=y2*sy;

setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after scaling");

getch();
}

Program for Rotation:

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

void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,x3,y3,x4,y4,a,t;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("Enter coordinates of starting point:\n");


scanf("%f%f",&x1,&y1);

printf("Enter coordinates of ending point\n");


scanf("%f%f",&x2,&y2);

printf("Enter angle for rotation\n");


scanf("%f",&a);

setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
t=a*(3.14/180);
x3=(x1*cos(t))-(y1*sin(t));
y3=(x1*sin(t))+(y1*cos(t));
x4=(x2*cos(t))-(y2*sin(t));
y4=(x2*sin(t))+(y2*cos(t));

setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after rotation");

getch();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy