summaryrefslogtreecommitdiff
path: root/dda.c
blob: 6915d8bea3b99d247f1d3e95b7b62356ee11420c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "testdata.c"

#define N_STEPS  (4)
#define STEP  (1/((double)N_STEPS))


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;
}

static double
next_sample_y (double y)
{
    return next_sample_x (y);
}

static double
sample_step_x (double x)
{
    if ((x - floor (x)) == 0.625)
	return 0.5;
    else
	return 0.25;
}

static double
sample_step_y (double y)
{
    return sample_step_x (y);
}

static void
dda (test_data_t *testdata)
{
    double x0 = testdata->segment.x0;
    double y0 = testdata->segment.y0;
    double x1 = testdata->segment.x1;
    double y1 = testdata->segment.y1;
    int i = 0;

    double dxdy = (x1 - x0) / (y1 - y0);
    double y = next_sample_y (y0);
    double x = (y - y0) * dxdy + x0;
    double xi = next_sample_x (x);

    while (y < next_sample_y (y1))
    {
	if (testdata->points[i++] != xi		||
	    testdata->points[i++] != y)
	{
	    printf ("error\n");
	    exit (-1);
	}

	x += sample_step_y (y) * dxdy;
	xi = next_sample_x (x);
	y += sample_step_y (y);
    }
}

int
main ()
{
    int i;
    for (i = 0; i < sizeof testdata / sizeof testdata[0]; ++i)
    {
	dda (&(testdata[i]));
    }

    return 0;
}