Clarification on skeleton code for C graphics library -


i'm studying c , i've been given code draws single line of pixels:

void draw_line(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2) { // insert algorithm here. if (x1 == x2) {     //draw horizontal line     unsigned char i;     (i = y1; <= y2; i++){         set_pixel(x1, i, 1);     }            } else if (y1 == y2){     //draw vertical line     unsigned char i;     (i = x1; <= x2; i++){         set_pixel(i, y1, 1);     }        

i understand how works, not how implement it. provide example of how use it?

hope you:

algorithm:
1)get x , y co-ordinates of start , end points of line.
2)find difference in x , y co-ordinates values dx , dy.
3)check if dx greater of dy greater , assign greater value ‘steps’.
4)increment x , y values @ regular intervals dividing corresponding axes difference steps.
5)plot initial point using “putpixel” syntax.
6)repeat step 4 ‘steps’ number of times , mark end point using “putpixel” syntax.
7)end program.

program: #include"graphics.h"
#include"stdio.h"
#include"conio.h"
#include"math.h"
void linedraw(int,int,int,int);
int main()
{
int x1coeff,y1coeff,x2coeff,y2coeff;
printf("\enter x , y value starting point:");
scanf("%d%d",&x1coeff,&y1coeff);
printf("\enter x , y value end point:");
scanf("%d%d",&x2coeff,&y2coeff);
linedraw(x1coeff,y1coeff,x2coeff,y2coeff);
getch();
return 0;
}
void linedraw(int xa,int ya,int xb,int yb)
{
int dx,dy,steps,k;
int gdriver=detect,gmode;
float xinc,yinc,x,y;
initgraph(&gdriver,&gmode,""); //initialise graphics
dx=xb-xa;
dy=yb-ya;
x=xa;
y=ya;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xinc=dx/steps;
yinc=dy/steps;
putpixel(x,y,white);
for(k=0;k {
x+=xinc;
y+=yinc;
putpixel(x,y,white);
}}
output:
enter x , y value starting point:100 100
enter x , y value end point:200 200
line drawn is

enter image description here


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -