C

Tuesday, 30 October 2018

Write a C++ program to draw a line using Bresenham’s line drawing algorithm.


#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void drawline(int x0, int y0, int x1, int y1)
{
                int dx, dy,p,x,y;

                dx=x1-x0;
                dy=y1-y0;
                x=x0;
                y=y0;

                p=2*dy-dx;
                while(x<x1)
                {
                                if(p>=0)
                                {
                                                putpixel(x,y,7);
                                                y=y+1;
                                                p=p+2*dy-2*dx;
                                }
                                else
                                {
                                                putpixel(x,y,7);
                                                p=p+2*dy;
                                }
                                x=x+1;
                }
}
int main()
{
                int gd=DETECT, gm, error, x0,y0,x1,y1;
                initgraph(&gd,&gm,"c:\\turboc3\\bgi");
                cout<<"Enter the values of x0 and y0 :";
                cin>>x0>>y0;
                cout<<"Enter the vlaues of x1 and y1 :";
                cin>>x1>>y1;
                drawline(x0,y0,x1,y1);
                getch();
                return 0;
}

No comments:

Post a Comment

Write a program to draw a circle using mid-point circle drawing algorithm.

#include<iostream.h> #include<graphics.h> #include<conio.h> void drawcircle(int x0, int y0, int r) {         ...