Program to draw a star using DDA Line Drawing Algorithm in C++



#include <iostream>
#include <graphics.h>
using namespace std;
void star(float,float,float,float);
int main()
{
    int i,gd=DETECT,gm;
    char path[] = "";
    initgraph(&gd,&gm,path);
    //Statically setted  coordinates
    star(300,200,200,400);
    star(200,400,400,400);
    star(300,200,400,400);
    star(200,250,400,250);
    star(200,250,300,450);
    star(400,250,300,450);
    getch();
    closegraph();
}
//Function to draw a star
void star(float x1,float y1,float x2,float y2)
{
    float x,y,dx,dy,step;
    int i;
    dx=(x2-x1);
    dy=(y2-y1);

    if(dx>=dy)
    {
        step=abs(dx);
    }
    else
    {
       step=abs(dy);
    }
    dx=dx/step;
    dy=dy/step;
    x=x1;
    y=y1;
    i=1;
    while(i<=step)
    {
        putpixel(x,y,WHITE);
        x=x+dx;
        y=y+dy;
        i=i+1;
    }
}

OUTPUT:



Comments

Popular posts from this blog

HOW TO SETUP CODE BLOCKS FOR GRAPHICS PROGRAMS

Smile emoji using graphics in Java using Applet