Algorithmen
In dieser Lehrveranstaltung werden folgenden Themen behandelt:
Zeichenalgorithmen:
- Line drawing principle
- Digital Differential Analyser (DDA)
- Bresenham-Algorithm
- Zeichnen von Ellipsen mit DDA
- Polygone als Anzahl von 2D Punkten
- Polygon In/Out Decision
- Polygon Rasterization
DDA am Beispiel Linienzeichnen:
Evolution from a naive line drawing implementation, which draws a line
from position (x1, y1) to (x2, y2), to the Bresenham algorithm:
Step 1) Naive implementation (horizontal case)
float dx = x2 - x1;
float dy = y2 - y1;
float delta = dy / dx;
int x = x1;
float y = y1;
while (x++ <= x2)
{
mvaddch((int)(y+0.5), x, '*');
y = y + delta;
}
Step 2) Differential implementation (float version)
float dx = x2 - x1;
float dy = y2 - y1;
float delta = dy / dx;
int x = x1, y = y1;
float err = 0.5;
while (x++ <= x2)
{
mvaddch(y, x, '*');
err += delta;
if (err > 1)
{
err -= 1;
y++;
}
}
Step 3) Differential implementation (float version /wo division)
int dx = x2 - x1;
int dy = y2 - y1;
int x = x1, y = y1;
float err = 0.5*dx;
while (x++ <= x2)
{
mvaddch(y, x, '*');
err += dy;
if (err > dx)
{
err -= dx;
y++;
}
}
Step 4) Differential implementation (int version)
int dx = x2 - x1;
int dy = y2 - y1;
int x = x1, y = y1;
int err = dx;
while (x++ <= x2)
{
mvaddch(y, x, '*');
err += 2*dy;
if (err > 2*dx)
{
err -= 2*dx;
y++;
}
}
Step 5) Differential implementation (negated version)
int dx = x2 - x1;
int dy = y2 - y1;
int x = x1, y = y1;
int err = dx;
while (x++ <= x2)
{
mvaddch(y, x, '*');
err -= dy<<1;
if (err < 0)
{
err += dx<<1;
y++;
}
}
Step 6) Universal implementation (Bresenham algorithm)
int dx = x2 - x1;
int dy = y2 - y1;
if (abs(dx) > abs(dy))
{
// x is fast direction
... horizontal case
}
else
{
// y is fast direction
... symmetrical vertical case
}
from position (x1, y1) to (x2, y2), to the Bresenham algorithm:
Step 1) Naive implementation (horizontal case)
float dx = x2 - x1;
float dy = y2 - y1;
float delta = dy / dx;
int x = x1;
float y = y1;
while (x++ <= x2)
{
mvaddch((int)(y+0.5), x, '*');
y = y + delta;
}
Step 2) Differential implementation (float version)
float dx = x2 - x1;
float dy = y2 - y1;
float delta = dy / dx;
int x = x1, y = y1;
float err = 0.5;
while (x++ <= x2)
{
mvaddch(y, x, '*');
err += delta;
if (err > 1)
{
err -= 1;
y++;
}
}
Step 3) Differential implementation (float version /wo division)
int dx = x2 - x1;
int dy = y2 - y1;
int x = x1, y = y1;
float err = 0.5*dx;
while (x++ <= x2)
{
mvaddch(y, x, '*');
err += dy;
if (err > dx)
{
err -= dx;
y++;
}
}
Step 4) Differential implementation (int version)
int dx = x2 - x1;
int dy = y2 - y1;
int x = x1, y = y1;
int err = dx;
while (x++ <= x2)
{
mvaddch(y, x, '*');
err += 2*dy;
if (err > 2*dx)
{
err -= 2*dx;
y++;
}
}
Step 5) Differential implementation (negated version)
int dx = x2 - x1;
int dy = y2 - y1;
int x = x1, y = y1;
int err = dx;
while (x++ <= x2)
{
mvaddch(y, x, '*');
err -= dy<<1;
if (err < 0)
{
err += dx<<1;
y++;
}
}
Step 6) Universal implementation (Bresenham algorithm)
int dx = x2 - x1;
int dy = y2 - y1;
if (abs(dx) > abs(dy))
{
// x is fast direction
... horizontal case
}
else
{
// y is fast direction
... symmetrical vertical case
}
DDA am Beispiel Kreiszeichnen:
Ähnlicher differentieller Algorithmus wie beim Linienzeichnen nur mit veränderlicher Steigung.
Graphische Algorithmen am Beispiel Polygonzeichnen:
ASCII-Gfx Implementierung: svn revisions 111–132 of polygon.cpp