line – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 10 Mar 2024 17:05:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg line – Programmerbay https://programmerbay.com 32 32 C Program to Draw a Hut https://programmerbay.com/c-program-to-draw-a-hut/ https://programmerbay.com/c-program-to-draw-a-hut/#respond Thu, 29 Feb 2024 17:23:26 +0000 https://www.programmerbay.com/?p=1886 C supports a header file named “graphics.h” which enables us to draw various figures. In this, we’ll be using line and rectangle function of that particular header file to draw a hut.

C Program to draw a hut in computer graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main(){
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t\t ********* HUT ********");
line(150,100,50,200);
line(150,100,350,100); 
line(150,100,300,200);
line(300,200,500,200);
line(350,100,500,200);
line(50,200,300,200);
rectangle(50,400,300,200);
rectangle(300,200,500,400);
rectangle(130,250,230,400);

getch();
closegraph();

}

Output:

Hut

]]>
https://programmerbay.com/c-program-to-draw-a-hut/feed/ 0
C Program to Draw Line using DDA Algorithm in Computer raphics https://programmerbay.com/program-to-draw-a-line-using-dda-algorithm-in-computer-graphics/ https://programmerbay.com/program-to-draw-a-line-using-dda-algorithm-in-computer-graphics/#respond Mon, 04 Jul 2022 07:21:25 +0000 https://www.programmerbay.com/?p=1978 In this article, we’ll be using DDA algorithm to draw line in C.  DDA line drawing algorithm is the simplest algorithm as compared to others.

Digital differential Analyzer (DDA) is a line drawing algorithm which calculates and plots coordinates on the basis of the previously calculated intermediate points until it reaches to the final point. However, this algorithm works on the concept of the slope-intercept equation.

Must Read: DDA Algorithm in computer graphics

C Program to draw a line using DDA algorithm

Program:

#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
int x0,y0,x1,y1,i=0;
float delx,dely,len,x,y;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** DDA Line Drawing Algorithm ***********");
printf("\n Please enter the starting coordinate of x, y = ");
scanf("%d %d",&x0,&y0);
printf("\n Enter the final coordinate of x, y = ");
scanf("%d %d",&x1,&y1);
dely=abs(y1-y0);
delx=abs(x1-x0);

if(delx<dely)
{
len = dely;
}
else
{
len=delx;
}
delx=(x1-x0)/len;
dely=(y1-y0)/len;
x=x0+0.5;
y=y0+0.5;
do{
putpixel(x,y,3);
x=x+delx;
y=y+dely;
i++;
delay(30);
}while(i<=len);
getch();
closegraph();
}

 

Output:

dda line drawing algo

]]>
https://programmerbay.com/program-to-draw-a-line-using-dda-algorithm-in-computer-graphics/feed/ 0
Explain DDA Line Drawing Algorithm in Computer Graphics with Example https://programmerbay.com/dda-algorithm-in-computer-graphics/ https://programmerbay.com/dda-algorithm-in-computer-graphics/#respond Sat, 14 Sep 2019 16:25:25 +0000 https://programmerbay.com/?p=5078 Explain DDA Algorithm?

Digital differential Analyzer is a line drawing algorithm which calculates and plots coordinates on the basis of the previously calculated intermediate points until it reaches to the final point.  However, this algorithm works on the concept of the slope-intercept equation.

DDA Line Drawing Algorithm:-

      1. Since, A line segment has an initial point (x0,y0) and a final point (x1,y1), so get the input from the user regarding the initial and final point.
      2.  After getting the input, calculate the value of Δx and Δy.  (Δ represents the difference between two points)   Δx= x1-x0,  Δy= y1-y0
      3. Find the slope of the line by usin.g: Slope =Δy/Δx
      4.  if Slope > 1 then do,

          While x isn’t equivalent to the final point (x1)          do,

x0+k = xk + (1/m)

y=y+1

plot(x,y)

          End while

         else

         While y isn’t equivalent to the final point (y1)          do,

y0+k = yk + (m),  x=x+1

plot(x,y)

End While

C program to draw a line using DDA algorithm in computer graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,delx,dely;
float slope;
int gr=DETECT,gm;

initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter the initial points of x, y = ");
scanf("%f %f",&x,&y);
printf("\n enter the final points of x, y = ");
scanf("%f %f",&x1,&y1);
dely= y1-y;
delx=x1-x;
slope=dely/delx;
if(slope>1.0)
{
while(y<=y1){
putpixel(x,y,1);
x=x+(1/slope);
y=y+1.0;
}
}

else{
while(x<=x1){
putpixel(x,y,1);
y=y+slope;
x=x+1.0;
}
}

getch();
}

 

Output:

dda algo with slope

Alternate way:

Alternate Program to draw a line using DDA algorithm

]]>
https://programmerbay.com/dda-algorithm-in-computer-graphics/feed/ 0