This is a fast line drawing routine written in C. Since it uses only integer arthmetic and add/subtract/shift operations, it can easily be ported to assembler.
Using BASIC with the ZX81 is also possible, but would be very slow, because everything would be calculated using floating-point arithmetic.
Of course you still need the sin oder cos functions first to calculate the destination coordinates of the line depending on the given angle.
- Code: Select all
//---------------------------------------------------------------------------
//draws a line from x1, y1 to x2, y2; line can be drawn in any direction
//set show to 1 to draw pixel, set to 0 to clear pixel
//---------------------------------------------------------------------------
void DrawLine(int x1, int y1, int x2, int y2, unsigned char show)
{
int dx, dy, stepx, stepy, fraction;
dy = y2 - y1;
dx = x2 - x1;
if (dy < 0)
{
dy = -dy;
stepy = -1;
}
else
{
stepy = 1;
}
if (dx < 0)
{
dx = -dx;
stepx = -1;
}
else
{
stepx = 1;
}
dy <<= 1;
dx <<= 1;
LCD_SetPixel(x1, y1, show);
if (dx > dy)
{
fraction = dy - (dx >> 1);
while (x1 != x2)
{
if (fraction >= 0)
{
y1 += stepy;
fraction -= dx;
}
x1 += stepx;
fraction += dy;
LCD_SetPixel(x1, y1, show);
}
}
else
{
fraction = dx - (dy >> 1);
while (y1 != y2)
{
if (fraction >= 0)
{
x1 += stepx;
fraction -= dy;
}
y1 += stepy;
fraction += dx;
LCD_SetPixel(x1, y1, show);
}
}
}
HTH Siggi
There are 10 types of people in this world: those who understand binary and those who don't.