Fixed-point version: 선분 길이가 4096 픽셀 정도까지는 1-pixel 이내의 오차로 그려진다.
void DDALine(CPoint A, CPoint B) {
const int mulfac = 4096; // 2^12 = 4096;
int dx = B.x - A.x;
int dy = B.y - A.y;
int adx = dx < 0 ? -dx : dx;
int ady = dy < 0 ? -dy : dy;
int steps = adx < ady ? ady : adx;
if (steps == 0) SetPixel(A.x, A.y); //single point;
else {
int curX = A.x * mulfac;
int curY = A.y * mulfac;
dx = (dx * mulfac) / steps;
dy = (dy * mulfac) / steps;
while (steps-- >= 0) {
SetPixel(curX / mulfac, curY / mulfac);
curX += dx;
curY += dy;
}
}
}
void DDALine(int x0, int y0, int x1, int y1) {
int dx = x1 - x0;
int dy = y1 - y0;
// calculate steps required for generating pixels
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float xinc = dx / (float)steps;
float yinc = dy / (float)steps;
float x = x0;
float y = x0;
for (int i = 0; i <= steps; i++) {
SetPixel(round(x), round(y));
x += xinc;
y += yinc;
}
}
728x90
'Computational Geometry' 카테고리의 다른 글
Data Fitting with B-Spline Curves (0) | 2021.04.30 |
---|---|
Closest Pair of Points (0) | 2021.04.27 |
B-Spline (0) | 2021.04.25 |
Bezier Smoothing (0) | 2021.04.23 |
Flatness of Cubic Bezier Curve (0) | 2021.04.23 |