GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 #include <math.h>
11 
12 #include "pngdriver.h"
13 
14 static void store_xy(double x, double y)
15 {
16  int xi = (int) floor(x);
17  int yi = (int) floor(y);
18 
19  if (x < png.clip_left || x >= png.clip_rite || y < png.clip_top || y >= png.clip_bot)
20  return;
21 
22  png.grid[yi * png.width + xi] = png.current_color;
23 }
24 
25 static void swap(double *a, double *b)
26 {
27  double t = *a; *a = *b; *b = t;
28 }
29 
30 static void draw_line(double x1, double y1, double x2, double y2)
31 {
32  double x, y;
33  double dx, dy;
34 
35  if (fabs(y1 - y2) > fabs(x1 - x2)) {
36  if (y1 > y2) {
37  swap(&y1, &y2);
38  swap(&x1, &x2);
39  }
40 
41  dy = y2 - y1;
42  dx = x2 - x1;
43 
44  for (y = floor(y1) + 0.5; y < y2; y++) {
45  x = x1 + (y - y1) * dx / dy;
46  store_xy(x, y);
47  }
48  }
49  else {
50  if (x1 > x2) {
51  swap(&x1, &x2);
52  swap(&y1, &y2);
53  }
54 
55  dx = x2 - x1;
56  dy = y2 - y1;
57 
58  for (x = floor(x1) + 0.5; x < x2; x++) {
59  y = y1 + (x - x1) * dy / dx;
60  store_xy(x, y);
61  }
62  }
63 }
64 
65 void png_draw_line(double x1, double y1, double x2, double y2)
66 {
67  struct path path;
68  struct vertex vertices[5];
69  double k = png.linewidth / 2;
70  double dx, dy;
71 
72  if (png.linewidth <= 1) {
73  draw_line(x1, y1, x2, y2);
74  png.modified = 1;
75  return;
76  }
77 
78  path.vertices = vertices;
79  path.count = 0;
80  path.alloc = 5;
81  path.start = -1;
82 
83  /* FIXME: rendering issues (#1283) */
84  dx = fabs(x2 - x1);
85  dy = fabs(y2 - y1);
86 
87  if (dy > dx) {
88  path_move(&path, x1 - k, y1);
89  path_cont(&path, x1 + k, y1);
90  path_cont(&path, x2 + k, y2);
91  path_cont(&path, x2 - k, y2);
92  path_close(&path);
93  }
94  else {
95  path_move(&path, x1, y1 - k);
96  path_cont(&path, x1, y1 + k);
97  path_cont(&path, x2, y2 + k);
98  path_cont(&path, x2, y2 - k);
99  path_close(&path);
100  }
101 
102  png_polygon(&path);
103 }
104 
int width
Definition: pngdriver.h:43
Definition: path.h:11
int current_color
Definition: pngdriver.h:34
GRASS png display driver - header file.
void path_close(struct path *p)
Definition: driver/path.c:84
void path_cont(struct path *p, double x, double y)
Definition: driver/path.c:79
int count
Definition: path.h:18
double clip_rite
Definition: pngdriver.h:42
double clip_bot
Definition: pngdriver.h:42
#define x
void path_move(struct path *p, double x, double y)
Definition: driver/path.c:73
int start
Definition: path.h:20
struct png_state png
int linewidth
Definition: pngdriver.h:49
double t
Definition: r_raster.c:39
int modified
Definition: pngdriver.h:47
struct vertex * vertices
Definition: path.h:17
double b
Definition: r_raster.c:39
int alloc
Definition: path.h:19
unsigned int * grid
Definition: pngdriver.h:44
void png_draw_line(double x1, double y1, double x2, double y2)
Definition: draw_line.c:65
Definition: path.h:16
void png_polygon(struct path *)
Draw polygon.