Posts

C Program to Draw a Circle using MidPoint Algorithm

#include <stdio.h> #include <conio.h> #include <graphics.h> void circleMidpoint(int, int, int); void drawCircle(int, int, int, int); void main() {     int xc, yc, r;     int gd = DETECT, gm;     initgraph(&gd, &gm, "C:\\TC\\BGI");     printf("Enter radius of circle: ");     scanf("%d", &r);     printf("Enter center coordinates of circle: ");     scanf("%d %d", &xc, &yc);         circleMidpoint(xc, yc, r);     getch(); } void circleMidpoint(int xc, int yc, int r) {     int x = 0, y = r;     int p = 1 - r;     while (x < y)     {         drawCircle(xc, yc, x, y);         x++;         if (p < 0)             p = p + 2 * x + 1;         else         {             y--;             p = p + 2 * (x - y) + 1;         }         drawCircle(xc, yc, x, y);         delay(20);     } } void drawCircle(int xc, int yc, int x, int y) {     putpixel(xc+x, yc+y, RED);     putpixel(xc-x, yc+y, RED);     putpixel(xc

C Program to Draw a Line using Bresenham's Algorithm

#include <stdio.h> #include <conio.h> #include <graphics.h> void lineBres(int, int, int, int); void main() {     int x1, y1, xn, yn;     int gd = DETECT, gm;     initgraph(&gd, &gm, "C:\\tc\\bgi");     printf("Enter starting coordinates of line: ");     scanf("%d %d", &x1, &y1);     printf("Enter ending coordinates of line: ");     scanf("%d %d", &xn, &yn);     lineBres(x1, y1, xn, yn);     getch(); } void lineBres(int x1, int y1, int xn, int yn) {     int dx = xn - x1, dy = yn - y1;     int di = 2 * dy - dx;     int ds = 2 * dy, dt = 2 * (dy - dx);     putpixel(x1, y1, 10);     while (x1 < xn)     {         x1++;         if (di < 0)         di = di + ds;         else         {         y1++;         di = di + dt;         }         putpixel(x1, y1, 10);         delay(50);     } }

Program to Draw Line using DDA Algorithm in C

#include <stdio.h> #include <conio.h> #include <graphics.h> void DDA(int, int, int, int); void main() {     int x1, y1, xn, yn;     int gd = DETECT, gm;     initgraph(&gd, &gm, "C:\\TC\\BGI");     printf("Enter the starting coordinates of line: ");     scanf("%d %d", &x1, &y1);     printf("Enter the ending coordinates of line: ");     scanf("%d %d", &xn, &yn);     DDA(x1, y1, xn, yn);     getch(); } void DDA(int x1, int y1, int xn, int yn) {     int dx, dy, m, i;     m = (yn-y1)/(xn-x1);     for (i=x1; i<=xn; i++)     {         if (m <= 1)         {         dx = 1;         dy = m * dx;     }     else     {         dy = 1;         dx = dy / m; } x1 = x1 + dx; y1 = y1 + dy; putpixel(x1, y1, 10); delay(50); } }