summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@l3000.localdomain>2010-12-06 09:22:31 -0500
committerSøren Sandmann Pedersen <ssp@l3000.localdomain>2010-12-20 10:48:51 -0500
commitb8661cae52314c8c0c66a685098bddf8dbadb35d (patch)
tree8f875abd051227733d711831650612855c5961ba
parenteded1748e75cd5e9dd25327f828c5e57d864c55e (diff)
Make xi a sample number
-rw-r--r--dda.c88
1 files changed, 50 insertions, 38 deletions
diff --git a/dda.c b/dda.c
index dcc3dca..d0ba4b7 100644
--- a/dda.c
+++ b/dda.c
@@ -14,31 +14,14 @@
#define SMALL_STEP_Y 0.25
#define FIRST_STEP_Y 0.125
-static double
-next_sample_x (double x)
-{
- double i = floor (x);
- double f = x - i;
- double s = (floor ((f - STEP/2.0) / STEP) + 1) * (STEP) + STEP/2.0;
-
- if (s == 0.875)
- {
- s += STEP;
- }
-
- if (s > 1.0)
- {
- s -= 1.0;
- i += 1;
- }
-
- return s + i;
-}
+#define BIG_STEP_X 0.5
+#define SMALL_STEP_X 0.25
+#define FIRST_STEP_X 0.125
static double
sample_to_pos (int y)
{
- return (((unsigned)y) >> 8) + (y & 0xff) * SMALL_STEP_Y + FIRST_STEP_Y;
+ return (y >> 8) + (y & 0xff) * SMALL_STEP_Y + FIRST_STEP_Y;
}
static int
@@ -62,6 +45,12 @@ next_sample_y (double y)
return r;
}
+static int
+next_sample_x (double x)
+{
+ return next_sample_y (x);
+}
+
static double
sample_step_x_forward (double x)
{
@@ -89,43 +78,66 @@ dda (test_data_t *testdata)
double y1 = testdata->segment.y1;
int i = 0;
- double dx = (x1 - x0);
- double dy = (y1 - y0);
- int yi = next_sample_y (y0);
- double xi = next_sample_x (x0);
- double e = (xi - x0) * dy - (sample_to_pos (yi) - y0) * dx;
-
#if 0
printf ("= = = = %f %f %f %f = = = = \n", x0, y0, x1, y1);
#endif
+ double dx = (x1 - x0);
+ double dy = (y1 - y0);
+ int yi = next_sample_y (y0);
+ int xi = next_sample_x (x0);
+ double e = (sample_to_pos (xi) - x0) * dy - (sample_to_pos (yi) - y0) * dx;
+
while (yi < next_sample_y (y1))
{
if (dx >= 0)
{
while (e <= 0)
{
- double s = sample_step_x_forward (xi);
-
- e += s * dy;
- xi += s;
+ if ((xi & 0xff) == N_GRID_X - 1)
+ {
+ e += BIG_STEP_X * dy;
+ xi += (1 << 8);
+ xi &= 0xffffff00;
+ }
+ else
+ {
+ e += SMALL_STEP_X * dy;
+ xi++;
+ }
}
}
else
{
- double s;
-
- while (s = sample_step_x_backward (xi), e > s * dy)
+ begin:
+ if ((xi & 0xff) == 0)
{
- e -= s * dy;
- xi -= s;
+ if (e > BIG_STEP_X * dy)
+ {
+ e -= BIG_STEP_X * dy;
+ xi -= (1 << 8);
+ xi |= (N_GRID_X - 1);
+
+ goto begin;
+ }
+ }
+ else
+ {
+ if (e > SMALL_STEP_X * dy)
+ {
+ e -= SMALL_STEP_X * dy;
+ xi--;
+
+ goto begin;
+ }
}
}
- if (testdata->points[i++] != xi ||
+ if (testdata->points[i++] != sample_to_pos (xi) ||
testdata->points[i++] != sample_to_pos (yi))
{
- printf ("%d error %f %f\n", i, testdata->points[i - 1], sample_to_pos (yi));
+ printf ("%d error %f %f\n", i, testdata->points[i - 1],
+ sample_to_pos (xi));
exit (-1);
}