Jonas Ernst said:
Hi,
Can somebody give me some hints how to do a line interpolation without using
floating point arithemtics?
The function shall do a linear interpolation between 2 points (line interp?)
and it shall return the y value to any x value given.
e.g. p1: (0,0)
p2 (1000,50)
y value to calculate at x=600
Is there a way to implement this not using any FP arithmetics in C? Perhaps
"scaling up" or sth like this?
Any help would be great
Thanks in advance
J. Ernst
Here is my old C implementation of the Bresenham algorithm.
The routine calls another subroutine point() which actually
sets a point on the screen.
This routine is hardware specific and not provided here.
Regards,
Martin
/************************************************************/
void line (int x1, int y1, int x2, int y2, long col)
{
int dx, /*X-Differenz*/
dy, /*Y-Differenz*/
sx, /*X-Schritt, 1
oder -1*/
sy, /*Y-Schritt, 1
oder -1*/
count, /*Z"hler fr
Punkte*/
wert; /*fr
Entscheidung*/
if (x2 > x1) { /*Linie nach
rechts*/
dx = x2 - x1; /*X-Differenz
berechnen, positiv*/
sx = 1; /*X-Schritt +1*/
}
else { /*Linie nach
links*/
dx = x1 - x2; /*X-Differenz
berechnen, positiv*/
sx = -1; /*X-Schritt -1*/
}
if (y2 > y1) { /*Linie nach
unten*/
dy = y2 - y1; /*Y-Differenz
berechnen, positiv*/
sy = 1; /*Y-Schritt +1*/
}
else { /*Linie nach
oben*/
dy = y1 - y2; /*Y-Differenz
berechnen, positiv*/
sy = -1; /*Y-Schritt -1*/
}
wert = 0; /*Wert fr
Entscheidung*/
if (dx > dy) { /*mehr
X-Schritte*/
wert = dx / 2; /*halbe
X-Differenz*/
for (count = 0; count <= dx; count++) /*Zahl der
Punkte*/
{
point (x1, y1, col); /*Punkt setzen*/
x1 = x1 + sx; /*Schritt in
X-Richtung*/
wert -= dy;
if (wert < 0)
{
y1 += sy; /*Schritt in
Y-Richtung*/
wert += dx;
}
}
}
else { /*mehr
Y-Schritte*/
wert = dy / 2; /*halbe
Y-Differenz*/
for (count = 0; count <= dy; count++) { /*Zahl der
Punkte*/
point (x1, y1, col); /*Punkt setzen*/
y1 = y1 + sy; /*Schritt in
Y-Richtung*/
wert -= dx;
if (wert < 0) {
x1 = x1 + sx; /*Schritt in
X-Richtung*/
wert += dy;
}
}
}
}
/************************************************************/>