File: drawline.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (78 lines) | stat: -rw-r--r-- 2,056 bytes parent folder | download | duplicates (2)
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
/****** drawline.c *****************************************************

        Function to draw a line segment defined by points (row, col)
        and (backrow, backcol) using Bresenham's Algorithm.
        Note:         do not draw the end point (backrow, backcol)
                except when it is the same as the beginning point
                (row, col) in order to be able utilize path segments
                drawn before the current path segment.

 ***********************************************************************/

#include <grass/segment.h>

void drawline(int x1, int y1, int x2, int y2)
{
    extern char *value;
    extern SEGMENT out_seg;

    int dx, dy, i, e;
    int incx, incy, inc1, inc2;
    int x, y;
    int data = 1;

    /*debug: printf("\n(%d,%d)->(%d,%d): ", x1,y1,x2,y2); */
    dx = x2 - x1;
    dy = y2 - y1;
    incx = 1;
    incy = 1;
    if (dx < 0) {
        dx = -dx;
        incx = -1;
    }
    if (dy < 0) {
        dy = -dy;
        incy = -1;
    }
    x = x1;
    y = y1;

    value = (char *)&data;

    if (dx > dy) {
        Segment_put(&out_seg, value, x, y);
        /*debug: printf("put1-(%d,%d) ",x,y); */
        e = 2 * dy - dx;
        inc1 = 2 * (dy - dx);
        inc2 = 2 * dy;
        for (i = 0; i < dx - 1; i++) {
            if (e >= 0) {
                y += incy;
                e += inc1;
            }
            else
                e += inc2;
            x += incx;
            Segment_put(&out_seg, value, x, y);
            /*debug:printf("put2-(%d,%d) ",x,y); */
        }
    }
    else {
        Segment_put(&out_seg, value, x, y);
        /*debug:printf("put3-(%d,%d) ",x,y); */
        e = 2 * dx - dy;
        inc1 = 2 * (dx - dy);
        inc2 = 2 * dx;
        for (i = 0; i < dy - 1; i++) {
            if (e >= 0) {
                x += incx;
                e += inc1;
            }
            else
                e += inc2;
            y += incy;
            Segment_put(&out_seg, value, x, y);
            /*debug:rintf("put4-(%d,%d) ",x,y); */
        }
    }
}