0% found this document useful (0 votes)
3 views5 pages

Document

This document provides two C programs that utilize the Digital Differential Analyzer (DDA) algorithm for drawing graphics. The first program demonstrates how to draw a line using the DDA algorithm, while the second program illustrates how to draw a circle. Both programs require the graphics.h library and include adjustable parameters for line and circle coordinates.

Uploaded by

anaizaakram
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)
3 views5 pages

Document

This document provides two C programs that utilize the Digital Differential Analyzer (DDA) algorithm for drawing graphics. The first program demonstrates how to draw a line using the DDA algorithm, while the second program illustrates how to draw a circle. Both programs require the graphics.h library and include adjustable parameters for line and circle coordinates.

Uploaded by

anaizaakram
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/ 5

Write a program to display a line using DDA algorithm

Ans: Here’s a C program to display a line using the Digital Differential


Analyzer (DDA) algorithm:

#include <stdio.h>

#include <graphics.h>

Void draw_line_DDA(int x1, int y1, int x2, int y2) {

Int dx = x2 – x1;

Int dy = y2 – y1;

Int steps;

If (abs(dx) > abs(dy)) {

Steps = abs(dx);

} else {

Steps = abs(dy);

Float x_increment = (float)dx / steps;

Float y_increment = (float)dy / steps;

Float x = x1;

Float y = y1;
Putpixel(round(x), round(y), WHITE);

For (int i = 0; i < steps; i++) {

X += x_increment;

Y += y_increment;

Putpixel(round(x), round(y), WHITE);

Int main() {

Int gd = DETECT, gm;

Initgraph(&gd, &gm, NULL);

Int x1 = 100, y1 = 100;

Int x2 = 400, y2 = 300;

Draw_line_DDA(x1, y1, x2, y2);

Getch();

Closegraph();

Return 0;

This program uses the graphics.h library for graphics operations, which is
commonly available in Turbo C/C++ compiler. Adjust the coordinates x1, y1,
x2, y2 to draw different lines.
Write a program to draw the circle with DDA.

program to draw a circle using the DDA (Digital Differential Analyzer) algorithm:

#include <stdio.h>

#include <graphics.h>

Void drawCircle(int xc, int yc, int radius) {

Int x = radius;

Int y = 0;

Int dx = 1;

Int dy = 1;

Int err = dx – (radius << 1);

While (x >= y) {

Putpixel(xc + x, yc + y, WHITE);

Putpixel(xc – x, yc + y, WHITE);

Putpixel(xc + x, yc – y, WHITE);

Putpixel(xc – x, yc – y, WHITE);

Putpixel(xc + y, yc + x, WHITE);

Putpixel(xc – y, yc + x, WHITE);

Putpixel(xc + y, yc – x, WHITE);
Putpixel(xc – y, yc – x, WHITE);

If (err <= 0) {

Y++;

Err += dy;

Dy += 2;

If (err > 0) {

x--;

dx += 2;

err += dx – (radius << 1);

Int main() {

Int gd = DETECT, gm;

Initgraph(&gd, &gm, NULL);

Int xc = 250; // x-coordinate of center

Int yc = 250; // y-coordinate of center

Int radius = 100; // radius of circle

drawCircle(xc, yc, radius);

delay(5000); // Delay to show the circle

closegraph(); // Close the graphics window


return 0;

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