|
@@ -11,102 +11,62 @@
|
|
|
|
|
|
#include "pngdriver.h"
|
|
|
|
|
|
-static void store_xy(int x, int y)
|
|
|
+static void store_xy(double x, double y)
|
|
|
{
|
|
|
+ int xi = (int) floor(x);
|
|
|
+ int yi = (int) floor(y);
|
|
|
+
|
|
|
if (x < png.clip_left || x >= png.clip_rite || y < png.clip_top || y >= png.clip_bot)
|
|
|
return;
|
|
|
|
|
|
- png.grid[y * png.width + x] = png.current_color;
|
|
|
+ png.grid[yi * png.width + xi] = png.current_color;
|
|
|
}
|
|
|
|
|
|
-static void draw_line(int x1, int y1, int x2, int y2)
|
|
|
+static void swap(double *a, double *b)
|
|
|
{
|
|
|
- int x, y, x_end, y_end;
|
|
|
- int xinc, yinc, error;
|
|
|
- int delta_x, delta_y;
|
|
|
-
|
|
|
- x = x1;
|
|
|
- x_end = x2;
|
|
|
- y = y1;
|
|
|
- y_end = y2;
|
|
|
-
|
|
|
- if (x == x_end && y == y_end) {
|
|
|
- store_xy(x, y);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- /* generate equation */
|
|
|
- delta_y = y_end - y;
|
|
|
- delta_x = x_end - x;
|
|
|
-
|
|
|
- /* figure out which way to move x */
|
|
|
- xinc = 1;
|
|
|
- if (delta_x < 0) {
|
|
|
- delta_x = -delta_x;
|
|
|
- xinc = -1;
|
|
|
- }
|
|
|
+ double t = *a; *a = *b; *b = t;
|
|
|
+}
|
|
|
|
|
|
- /* figure out which way to move y */
|
|
|
- yinc = 1;
|
|
|
- if (delta_y < 0) {
|
|
|
- delta_y = -delta_y;
|
|
|
- yinc = -1;
|
|
|
- }
|
|
|
+static void draw_line(double x1, double y1, double x2, double y2)
|
|
|
+{
|
|
|
+ double x, y;
|
|
|
+ double dx, dy;
|
|
|
|
|
|
- if (delta_x > delta_y) {
|
|
|
- /* always move x, decide when to move y */
|
|
|
- /* initialize the error term, and double delta x and delta y */
|
|
|
- delta_y = delta_y * 2;
|
|
|
- error = delta_y - delta_x;
|
|
|
- delta_x = delta_y - (delta_x * 2);
|
|
|
+ if (fabs(y1 - y2) > fabs(x1 - x2)) {
|
|
|
+ if (y1 > y2) {
|
|
|
+ swap(&y1, &y2);
|
|
|
+ swap(&x1, &x2);
|
|
|
+ }
|
|
|
|
|
|
- while (x != x_end) {
|
|
|
+ dy = y2 - y1;
|
|
|
+ dx = x2 - x1;
|
|
|
|
|
|
+ for (y = floor(y1) + 0.5; y < y2; y++) {
|
|
|
+ x = x1 + (y - y1) * dx / dy;
|
|
|
store_xy(x, y);
|
|
|
-
|
|
|
- if (error > 0) {
|
|
|
- y += yinc;
|
|
|
- error += delta_x;
|
|
|
- }
|
|
|
- else
|
|
|
- error += delta_y;
|
|
|
-
|
|
|
- x += xinc;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
- /* always move y, decide when to move x */
|
|
|
- /* initialize the error term, and double delta x and delta y */
|
|
|
- delta_x = delta_x * 2;
|
|
|
- error = delta_x - delta_y;
|
|
|
- delta_y = delta_x - (delta_y * 2);
|
|
|
+ if (x1 > x2) {
|
|
|
+ swap(&x1, &x2);
|
|
|
+ swap(&y1, &y2);
|
|
|
+ }
|
|
|
|
|
|
- while (y != y_end) {
|
|
|
+ dx = x2 - x1;
|
|
|
+ dy = y2 - y1;
|
|
|
|
|
|
+ for (x = floor(x1) + 0.5; x < x2; x++) {
|
|
|
+ y = y1 + (x - x1) * dy / dx;
|
|
|
store_xy(x, y);
|
|
|
-
|
|
|
- if (error > 0) {
|
|
|
- x += xinc;
|
|
|
- error += delta_y;
|
|
|
- }
|
|
|
- else
|
|
|
- error += delta_x;
|
|
|
-
|
|
|
- y += yinc;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- store_xy(x, y);
|
|
|
}
|
|
|
|
|
|
-void PNG_draw_line(double fx1, double fy1, double fx2, double fy2)
|
|
|
+void PNG_draw_line(double x1, double y1, double x2, double y2)
|
|
|
{
|
|
|
- int x1 = (int) floor(fx1 + 0.5);
|
|
|
- int y1 = (int) floor(fy1 + 0.5);
|
|
|
- int x2 = (int) floor(fx2 + 0.5);
|
|
|
- int y2 = (int) floor(fy2 + 0.5);
|
|
|
+ double ax[4], ay[4];
|
|
|
+ double k = png.linewidth / 2;
|
|
|
int dx, dy;
|
|
|
- int i;
|
|
|
|
|
|
if (png.linewidth <= 1) {
|
|
|
draw_line(x1, y1, x2, y2);
|
|
@@ -114,17 +74,19 @@ void PNG_draw_line(double fx1, double fy1, double fx2, double fy2)
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- dx = abs(x2 - x1);
|
|
|
- dy = abs(y2 - y1);
|
|
|
-
|
|
|
- for (i = 0; i < png.linewidth; i++) {
|
|
|
- int k = i - png.linewidth / 2;
|
|
|
-
|
|
|
- if (dy > dx)
|
|
|
- draw_line(x1 + k, y1, x2 + k, y2);
|
|
|
- else
|
|
|
- draw_line(x1, y1 + k, x2, y2 + k);
|
|
|
+ if (dy > dx) {
|
|
|
+ ax[0] = x1 - k; ay[0] = y1;
|
|
|
+ ax[1] = x1 + k; ay[1] = y1;
|
|
|
+ ax[2] = x2 + k; ay[2] = y2;
|
|
|
+ ax[3] = x2 - k; ay[3] = y2;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ax[0] = x1; ay[0] = y1 - k;
|
|
|
+ ax[1] = x1; ay[1] = y1 + k;
|
|
|
+ ax[2] = x2; ay[2] = y2 + k;
|
|
|
+ ax[3] = x2; ay[3] = y2 - k;
|
|
|
}
|
|
|
|
|
|
- png.modified = 1;
|
|
|
+ PNG_Polygon(ax, ay, 4);
|
|
|
}
|
|
|
+
|