GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pngdriver/Draw_line.c
Go to the documentation of this file.
1 
2 /*
3  * draw a line between two given points in the current color.
4  *
5  * Called by:
6  * Cont_abs() in ../lib/Cont_abs.c
7  */
8 
9 #include <stdlib.h>
10 
11 #include "pngdriver.h"
12 
13 static void store_xy(int x, int y)
14 {
15  if (x < clip_left || x >= clip_rite || y < clip_top || y >= clip_bot)
16  return;
17 
18  grid[y * width + x] = currentColor;
19 }
20 
21 static void draw_line(int x1, int y1, int x2, int y2)
22 {
23  int x, y, x_end, y_end;
24  int xinc, yinc, error;
25  int delta_x, delta_y;
26 
27  x = x1;
28  x_end = x2;
29  y = y1;
30  y_end = y2;
31 
32  if (x == x_end && y == y_end) {
33  store_xy(x, y);
34  return;
35  }
36 
37  /* generate equation */
38  delta_y = y_end - y;
39  delta_x = x_end - x;
40 
41  /* figure out which way to move x */
42  xinc = 1;
43  if (delta_x < 0) {
44  delta_x = -delta_x;
45  xinc = -1;
46  }
47 
48  /* figure out which way to move y */
49  yinc = 1;
50  if (delta_y < 0) {
51  delta_y = -delta_y;
52  yinc = -1;
53  }
54 
55  if (delta_x > delta_y) {
56  /* always move x, decide when to move y */
57  /* initialize the error term, and double delta x and delta y */
58  delta_y = delta_y * 2;
59  error = delta_y - delta_x;
60  delta_x = delta_y - (delta_x * 2);
61 
62  while (x != x_end) {
63 
64  store_xy(x, y);
65 
66  if (error > 0) {
67  y += yinc;
68  error += delta_x;
69  }
70  else
71  error += delta_y;
72 
73  x += xinc;
74  }
75  }
76  else {
77  /* always move y, decide when to move x */
78  /* initialize the error term, and double delta x and delta y */
79  delta_x = delta_x * 2;
80  error = delta_x - delta_y;
81  delta_y = delta_x - (delta_y * 2);
82 
83  while (y != y_end) {
84 
85  store_xy(x, y);
86 
87  if (error > 0) {
88  x += xinc;
89  error += delta_y;
90  }
91  else
92  error += delta_x;
93 
94  y += yinc;
95  }
96  }
97 
98  store_xy(x, y);
99 }
100 
101 void PNG_draw_line(int x1, int y1, int x2, int y2)
102 {
103  int dx, dy;
104  int i;
105 
106  if (linewidth <= 1) {
107  draw_line(x1, y1, x2, y2);
108  modified = 1;
109  return;
110  }
111 
112  dx = abs(x2 - x1);
113  dy = abs(y2 - y1);
114 
115  for (i = 0; i < linewidth; i++) {
116  int k = i - linewidth / 2;
117 
118  if (dy > dx)
119  draw_line(x1 + k, y1, x2 + k, y2);
120  else
121  draw_line(x1, y1 + k, x2, y2 + k);
122  }
123 
124  modified = 1;
125 }
int currentColor
int modified
tuple width
int clip_rite
def error
Display an error message using g.message -e
Definition: core.py:370
unsigned char * grid
int y
Definition: plot.c:34
int clip_bot
int linewidth
void PNG_draw_line(int x1, int y1, int x2, int y2)
double x
Definition: cnversions.c:36