I am using WinBGIm.
#include <iostream>
#include <cmath>
#include "graphics.h"
void Swap(double &a, double &b)
{
double x = a;
a = b;
b = x;
}
double Round(double number)
{
return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
}
void PlotPixel(int x, int y, int color)
{
const int radius = 1;
setcolor(color);
circle(400+x,300-y,radius);
}
void DDALine(double x1, double y1, double x2, double y2, int color)
{
//Is it really required?
if(x1>x2 || y1>y2)
{
Swap(x1, x2);
Swap(y1, y2);
}
//////////////////////???
double xi = x1;
double yi = y1;
double deltaY = y2 - y1;
double deltaX = x2 - x1;
double m = deltaY / deltaX;
//Is it really required?
/*if(x1 == x2)
{
m = 2;
}*/
if(abs(m)<=1)
{
while (xi<=x2)
{
yi = yi + m;
yi = Round(yi);
PlotPixel(xi, yi, color);
xi = xi + 1;
}
}
if(abs(m)>1)
{
while (yi<=y2)
{
xi = xi + 1/m;
xi = Round(xi);
PlotPixel(xi, yi, color);
yi = yi + 1;
}
}
}
int main()
{
int gm=DETECT, gd=DETECT;
initwindow(1024, 800,"Er");
DDALine(-200, 0, 200, 0, BLUE);
DDALine(0, 200, 0, -200, BLUE);
///////////////////////////////
DDALine(-100, -100, -100, -100, WHITE);
DDALine(-100, -100, -100, 100, WHITE);
DDALine(-100, -100, 100, -100, WHITE);
DDALine(-100, -100, 100, 100, WHITE);
DDALine(-100, 100, -100, -100, WHITE);
DDALine(-100, 100, -100, 100, WHITE);
DDALine(-100, 100, 100, -100, WHITE);
DDALine(-100, 100, 100, 100, WHITE);
/////////////////////////////
DDALine(100, -100, -100, -100, WHITE);
DDALine(100, -100, -100, 100, WHITE);
DDALine(100, -100, 100, -100, WHITE);
DDALine(100, -100, 100, 100, WHITE);
DDALine(100, 100, -100, -100, WHITE);
DDALine(100, 100, -100, 100, WHITE);
DDALine(100, 100, 100, -100, WHITE);
DDALine(100, 100, 100, 100, WHITE);
getch();
closegraph();
}
Output: