0% found this document useful (0 votes)
47 views12 pages

Anuditcg Lab

The document contains 11 code snippets demonstrating various graphics functions in C including drawing lines, circles, ellipses, rectangles, and triangles using pixel points, boundary fill, midpoint circle algorithm, DDA line algorithm and Bresenham's line algorithm.

Uploaded by

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

Anuditcg Lab

The document contains 11 code snippets demonstrating various graphics functions in C including drawing lines, circles, ellipses, rectangles, and triangles using pixel points, boundary fill, midpoint circle algorithm, DDA line algorithm and Bresenham's line algorithm.

Uploaded by

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

Lab Report:

1) Line using pixel points


#include <stdio.h>

#include <graphics.h>

#include <conio.h>

//#include <math.h>

int main()

int x1, y1, x2, y2, dx, dy, steps, x, y;

float xinc, yinc;

int gd = DETECT, gm;

initgraph(&gm, &gd, (char *)"");

printf("Please Enter two line endpoints(x1,y1) and (x2,y2)");

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

dx = x2 - x1;

dy = y2 - y1;

if (abs(dx) > abs(dy))

steps = abs(dx);

else

steps = abs(dy);

xinc = dx / steps;

yinc = dy / steps;

x = x1;
y = y1;

putpixel(x, y, WHITE);

for (int k = 0; k < steps; k++)

x = x + xinc;

y = y + yinc;

putpixel(abs(x), abs(y), WHITE);

getch();

closegraph();

return 0;

2) Setfills
#include <graphics.h>

#include <conio.h>

main()

int gd = DETECT, gm;

initgraph(&gd, &gm, (char *)"");

setfillstyle(2, RED);

circle(100, 100, 45);

floodfill(101, 101, WHITE);

getch();
closegraph();

3) Boundary Fill
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void boundary_fill4(int x, int y, int b_color, int fill_color)

int value = getpixel(x, y);

if (value != b_color && value != fill_color)

putpixel(x, y, fill_color);

boundary_fill4(x - 1, y, b_color, fill_color);

boundary_fill4(x + 1, y, b_color, fill_color);

boundary_fill4(x, y - 1, b_color, fill_color);

boundary_fill4(x, y + 1, b_color, fill_color);

int main()

int gd = DETECT, gm, x, y, b_color, f_color;

initgraph(&gd, &gm, (char *)"");

rectangle(300, 300, 400, 400);

printf("Enter the interior point of polygon(200 - 400): ");

scanf("%d%d", &x, &y);


printf("Enter the boundary color: ");

scanf("%d", &b_color);

printf("Enter the fill color: ");

scanf("%d", &f_color);

boundary_fill4(x, y, b_color, f_color);

getch();

closegraph();

return 0;

4) Brehsenham
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
//#include <math.h>

void drawLine(int x1, int y1, int x2, int y2) {


int dx, dy, p, x, y;

int gd = DETECT, gm;


initgraph(&gd, &gm, (char *)"");

dx = abs(x2 - x1);
dy = abs(y2 - y1);

int labelx = (x2 > x1) ? 1 : -1;


int labely = (y2 > y1) ? 1 : -1;

x = x1;
y = y1;

putpixel(x, y, 15);

if (dx > dy) {


p = 2 * dy - dx;
while (x != x2) {
if (p < 0) {
x += labelx;
p += 2 * dy;
} else {
x += labelx;
y += labely;
p += 2 * (dy - dx);
}
putpixel(x, y, 15);
}
} else {
p = 2 * dx - dy;
while (y != y2) {
if (p < 0) {
y += labely;
p += 2 * dx;
} else {
x += labelx;
y += labely;
p += 2 * (dx - dy);
}
putpixel(x, y, 15);
}
}

getch();
closegraph();
}

int main() {
int x1, y1, x2, y2;

printf("Enter two line endpoints (x1,y1) and (x2,y2): ");


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

drawLine(x1, y1, x2, y2);

return 0;
}

5) DDA
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
//#include <math.h>
main()
{
int x1, y1, x2, y2, dx, dy, steps, gd = DETECT, gm;
float xinc, yinc;
initgraph(&gd, &gm, (char*)"");
printf("Enter two line endpoints(x1,y1) and (x2,y2): ");
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
{
steps = abs(dx);
}
else
{
steps = abs(dy);
}
xinc = dx / steps;
yinc = dy / steps;
putpixel(x1, y1, 15);

for (int k = 0; k < steps; k++)


{
x1 = x1 + xinc;
y1 = y1 + yinc;
putpixel(x1, y1, 15);
}
getch();
closegraph();
}

6) Line Graph
#include <graphics.h>

#include <conio.h>

main()

int gd = DETECT, gm;

initgraph(&gd, &gm, (char *)"");


// draw normal line

line(130, 150, 220, 150);

// draw solid and thick line

setlinestyle(SOLID_LINE, 0, THICK_WIDTH);

line(130, 160, 220, 160);

// draw dotted line

setlinestyle(DOTTED_LINE, 0, 1);

line(130, 210, 220, 210);

// drawing center line

setlinestyle(DASHED_LINE, 0, 1);

line(160, 200, 220, 220);

// userbit line

setlinestyle(USERBIT_LINE, 0110101110001010111, 1);

line(150, 180, 220, 230);

getch();

closegraph();

7) MidPoint Ellipse
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

main()

int x, y, rx, ry, xc, yc, gd = DETECT, gm;

float p;
initgraph(&gd, &gm, (char *)"");

printf("Enter the radius of the elipse(x,y): ");

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

printf("Enter the center of the elipse: ");

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

x = 0;

y = ry;

p = ry * ry - rx * rx * ry + 1 / 4 * rx * rx;

while (2 * ry * ry * x < 2 * rx * rx * y)

putpixel(x + xc, y + yc, 15);

putpixel(-x + xc, y + yc, 15);

putpixel(-x + xc, -y + yc, 15);

putpixel(x + xc, -y + yc, 15);

if (p < 0)

x += 1;

p = p + 2 * ry * ry * x + ry * ry;

else

x += 1;

y -= 1;

p = p + 2 * ry * ry * x - 2 * rx * rx * y + ry * ry;
}

p = ry * ry * (x + 0.5) * (x + 0.5) + rx * rx * (y - 1) * (y - 1) - rx * rx * ry * ry;

while (y >= 0)

if (p > 0)

y -= 1;

p = p - 2 * rx * rx * y + rx * rx;

else

x += 1;

y -= 1;

p = p + 2 * ry * ry * x - 2 * rx * rx * y + rx * rx;

putpixel(x + xc, y + yc, 15);

putpixel(-x + xc, y + yc, 15);

putpixel(-x + xc, -y + yc, 15);

putpixel(x + xc, -y + yc, 15);

getch();

closegraph();

8) MidPoint
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
main()
{
int r, xc, yc, x, y;
float p;
int gd = DETECT, gm;
initgraph(&gd, &gm, (char *)"");

printf("Enter the radius: ");


scanf("%d", &r);
printf("Enter the center of circle: ");
scanf("%d%d", &xc, &yc);
x = 0;
y = r;

p = 5 / 4 - r;
while (y >= x)
{
if (p < 0)
{
x += 1;
p = p + 2 * x + 1;
}
else
{
x += 1;
y -= 1;
p = p + 2 * x + 1 - 2 * y;
}

putpixel(x + xc, y + yc, 15);


putpixel(x + xc, -y + yc, 15);
putpixel(-x + xc, y + yc, 15);
putpixel(-x + xc, -y + yc, 15);
putpixel(y + xc, x + yc, 15);
putpixel(y + xc, -x + yc, 15);
putpixel(-y + xc, x + yc, 15);
putpixel(-y + xc, -x + yc, 15);
}
getch();
closegraph();
}
9) Rectangl
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char *)"");

rectangle(150, 100, 250, 300);

getch();
closegraph();
}

10) Circle
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char *)"");

circle(250, 150, 120);

getch();
closegraph();
}

11) Triangle
#include <graphics.h>
#include <conio.h>

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

// setcolor(CYAN);
line(350, 250, 100, 235);
line(100, 235, 70, 90);
line(70, 90, 350, 250);

outtextxy(190, 265, "This is Triangle");


getch();
closegraph();
}

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