GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 /* 00003 * draw a line between two given points in the current color. 00004 * 00005 * Called by: 00006 * Cont_abs() in ../lib/Cont_abs.c 00007 */ 00008 00009 #include <stdlib.h> 00010 00011 #include "pngdriver.h" 00012 00013 static void store_xy(int x, int y) 00014 { 00015 if (x < clip_left || x >= clip_rite || y < clip_top || y >= clip_bot) 00016 return; 00017 00018 grid[y * width + x] = currentColor; 00019 } 00020 00021 static void draw_line(int x1, int y1, int x2, int y2) 00022 { 00023 int x, y, x_end, y_end; 00024 int xinc, yinc, error; 00025 int delta_x, delta_y; 00026 00027 x = x1; 00028 x_end = x2; 00029 y = y1; 00030 y_end = y2; 00031 00032 if (x == x_end && y == y_end) { 00033 store_xy(x, y); 00034 return; 00035 } 00036 00037 /* generate equation */ 00038 delta_y = y_end - y; 00039 delta_x = x_end - x; 00040 00041 /* figure out which way to move x */ 00042 xinc = 1; 00043 if (delta_x < 0) { 00044 delta_x = -delta_x; 00045 xinc = -1; 00046 } 00047 00048 /* figure out which way to move y */ 00049 yinc = 1; 00050 if (delta_y < 0) { 00051 delta_y = -delta_y; 00052 yinc = -1; 00053 } 00054 00055 if (delta_x > delta_y) { 00056 /* always move x, decide when to move y */ 00057 /* initialize the error term, and double delta x and delta y */ 00058 delta_y = delta_y * 2; 00059 error = delta_y - delta_x; 00060 delta_x = delta_y - (delta_x * 2); 00061 00062 while (x != x_end) { 00063 00064 store_xy(x, y); 00065 00066 if (error > 0) { 00067 y += yinc; 00068 error += delta_x; 00069 } 00070 else 00071 error += delta_y; 00072 00073 x += xinc; 00074 } 00075 } 00076 else { 00077 /* always move y, decide when to move x */ 00078 /* initialize the error term, and double delta x and delta y */ 00079 delta_x = delta_x * 2; 00080 error = delta_x - delta_y; 00081 delta_y = delta_x - (delta_y * 2); 00082 00083 while (y != y_end) { 00084 00085 store_xy(x, y); 00086 00087 if (error > 0) { 00088 x += xinc; 00089 error += delta_y; 00090 } 00091 else 00092 error += delta_x; 00093 00094 y += yinc; 00095 } 00096 } 00097 00098 store_xy(x, y); 00099 } 00100 00101 void PNG_draw_line(int x1, int y1, int x2, int y2) 00102 { 00103 int dx, dy; 00104 int i; 00105 00106 if (linewidth <= 1) { 00107 draw_line(x1, y1, x2, y2); 00108 modified = 1; 00109 return; 00110 } 00111 00112 dx = abs(x2 - x1); 00113 dy = abs(y2 - y1); 00114 00115 for (i = 0; i < linewidth; i++) { 00116 int k = i - linewidth / 2; 00117 00118 if (dy > dx) 00119 draw_line(x1 + k, y1, x2 + k, y2); 00120 else 00121 draw_line(x1, y1 + k, x2, y2 + k); 00122 } 00123 00124 modified = 1; 00125 }