summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@l3000.localdomain>2010-12-07 01:31:51 -0500
committerSøren Sandmann Pedersen <ssp@l3000.localdomain>2010-12-20 10:48:52 -0500
commit9ce9147cd584a37486f692caaef10188ffd8d4bf (patch)
tree58b82c85dcbd33db8503fce67ee753d0d2cd31c9
parent7ae923df85bf5090d037f001bd456c6e87dea971 (diff)
Move most state to an edge struct
-rw-r--r--dda.c101
1 files changed, 61 insertions, 40 deletions
diff --git a/dda.c b/dda.c
index 033be52..8446293 100644
--- a/dda.c
+++ b/dda.c
@@ -69,6 +69,38 @@ sample_step_x_backward (double x)
return 0.25;
}
+typedef struct
+{
+ int xi;
+ int yi;
+ int bottom;
+ int dx_positive;
+ double e;
+ double delta_e_big_x;
+ double delta_e_small_x;
+ double delta_e_big_y;
+ double delta_e_small_y;
+} edge_t;
+
+static void
+edge_init (edge_t *edge, double x0, double y0, double x1, double y1)
+{
+ double dx = (x1 - x0);
+ double dy = (y1 - y0);
+
+ edge->xi = next_sample_x (x0);
+ edge->yi = next_sample_y (y0);
+ edge->bottom = next_sample_y (y1);
+ edge->e =
+ (sample_to_pos (edge->xi) - x0) * dy -
+ (sample_to_pos (edge->yi) - y0) * dx;
+ edge->delta_e_big_x = BIG_STEP_X * dy;
+ edge->delta_e_small_x = SMALL_STEP_X * dy;
+ edge->delta_e_big_y = BIG_STEP_Y * dx;
+ edge->delta_e_small_y = SMALL_STEP_Y * dx;
+ edge->dx_positive = (dx >= 0);
+}
+
static void
dda (test_data_t *testdata)
{
@@ -81,83 +113,72 @@ dda (test_data_t *testdata)
#if 0
printf ("= = = = %f %f %f %f = = = = \n", x0, y0, x1, y1);
#endif
+ edge_t edge;
- double dx = (x1 - x0);
- double dy = (y1 - y0);
- int yi = next_sample_y (y0);
- int xi = next_sample_x (x0);
- int bottom = next_sample_y (y1);
- double e = (sample_to_pos (xi) - x0) * dy - (sample_to_pos (yi) - y0) * dx;
- double delta_e_big_x, delta_e_big_y;
- double delta_e_small_x, delta_e_small_y;
-
- delta_e_big_x = BIG_STEP_X * dy;
- delta_e_small_x = SMALL_STEP_X * dy;
- delta_e_big_y = BIG_STEP_Y * dx;
- delta_e_small_y = SMALL_STEP_Y * dx;
-
- while (yi < bottom)
+ edge_init (&edge, x0, y0, x1, y1);
+
+ while (edge.yi < edge.bottom)
{
- if (dx >= 0)
+ if (edge.dx_positive)
{
- while (e <= 0)
+ while (edge.e <= 0)
{
- if ((xi & 0xff) == N_GRID_X - 1)
+ if ((edge.xi & 0xff) == N_GRID_X - 1)
{
- e += delta_e_big_x;
- xi += (1 << 8);
- xi &= 0xffffff00;
+ edge.e += edge.delta_e_big_x;
+ edge.xi += (1 << 8);
+ edge.xi &= 0xffffff00;
}
else
{
- e += delta_e_small_x;
- xi++;
+ edge.e += edge.delta_e_small_x;
+ edge.xi++;
}
}
}
else
{
begin:
- if ((xi & 0xff) == 0)
+ if ((edge.xi & 0xff) == 0)
{
- if (e > delta_e_big_x)
+ if (edge.e > edge.delta_e_big_x)
{
- e -= delta_e_big_x;
- xi -= (1 << 8);
- xi |= (N_GRID_X - 1);
+ edge.e -= edge.delta_e_big_x;
+ edge.xi -= (1 << 8);
+ edge.xi |= (N_GRID_X - 1);
goto small;
}
}
else
{
small:
- if (e > delta_e_small_x)
+ if (edge.e > edge.delta_e_small_x)
{
- e -= delta_e_small_x;
- xi--;
+ edge.e -= edge.delta_e_small_x;
+ edge.xi--;
goto begin;
}
}
}
- if (testdata->points[i++] != sample_to_pos (xi) ||
- testdata->points[i++] != sample_to_pos (yi))
+ if (testdata->points[i++] != sample_to_pos (edge.xi) ||
+ testdata->points[i++] != sample_to_pos (edge.yi))
{
printf ("%d error %f %f\n", i, testdata->points[i - 1],
- sample_to_pos (xi));
+ sample_to_pos (edge.xi));
exit (-1);
}
- if ((yi & 0xff) == (N_GRID_Y - 1))
+ if ((edge.yi & 0xff) == (N_GRID_Y - 1))
{
- e -= delta_e_big_y;
- yi += (1 << 8);
- yi &= 0xffffff00;
+ edge.e -= edge.delta_e_big_y;
+ edge.yi += (1 << 8);
+ edge.yi &= 0xffffff00;
}
else
{
- e -= delta_e_small_y;
- yi++;
+ edge.e -= edge.delta_e_small_y;
+ edge.yi++;
}
}
}