Numworks Epsilon  1.4.1
Graphing Calculator Operating System
line.cpp
Go to the documentation of this file.
1 #include <kandinsky/line.h>
2 #include <kandinsky/pixel.h>
3 #include <assert.h>
4 
6  // Find the largest gap
7  KDPoint left, right;
8  if (p2.x > p1.x) {
9  left = p1;
10  right = p2;
11  } else {
12  left = p2;
13  right = p1;
14  }
15  KDPoint top, bottom;
16  if (p2.y > p1.y) {
17  top = p1;
18  bottom = p2;
19  } else {
20  top = p2;
21  bottom = p1;
22  }
23  assert(right.x >= left.x);
24  assert(bottom.y >= top.y);
25 
26  KDCoordinate deltaX = 2*(right.x - left.x);
27  KDCoordinate deltaY = 2*(bottom.y - top.y);
28 
29  KDPoint p, alwaysTranslate, conditionalTranslate;
30  KDCoordinate scanLength, error, minusError, plusError;
31 
32  if (deltaX >= deltaY) {
33  p = left;
34  scanLength = right.x - left.x;
35  error = right.x - left.x;
36  minusError = deltaY;
37  plusError = deltaX;
38  alwaysTranslate = KDPointMake(1,0);
39  conditionalTranslate = KDPointMake(0, (right.y >= left.y ? 1 : -1));
40  } else {
41  p = top;
42  scanLength = bottom.y - top.y;
43  error = bottom.y - top.y;
44  minusError = deltaX;
45  plusError = deltaY;
46  alwaysTranslate = KDPointMake(0,1);
47  conditionalTranslate = KDPointMake((bottom.x >= top.x ? 1 : -1), 0);
48  }
49 
50  KDCoordinate scanCounter = 0;
51  while (scanCounter++ < scanLength) {
52  KDSetPixel(p, c);
53  p = KDPointTranslate(p, alwaysTranslate);
54  error = error - minusError;
55  if (error <= 0) {
56  p = KDPointTranslate(p, conditionalTranslate);
57  error = error + plusError;
58  }
59  }
60 }
61 /*
62 #include <cmath>
63 #include <stdlib.h>
64 
65 void KDDrawAntiAliasedLine(KDPoint p1, KDPoint p2, KDCoordinate width, KDColor frontColor, KDColor backColor) {
66  int x0 = p1.x;
67  int y0 = p1.y;
68  int x1 = p2.x;
69  int y1 = p2.y;
70  float wd = width;
71 
72  int dx = abs(x1-x0), sx = x0 < x1 ? 1 : -1;
73  int dy = abs(y1-y0), sy = y0 < y1 ? 1 : -1;
74  int err = dx-dy, e2, x2, y2; // error value e_xy
75  float ed = dx+dy == 0 ? 1 : sqrt((float)dx*dx+(float)dy*dy);
76 
77  for (wd = (wd+1)/2; ; ) { // pixel loop
78  KDPoint p = {.x = x0, .y = y0};
79  KDColor color = KDColorMix(KDColorRed, KDColorWhite, (abs(err-dx+dy)/ed-wd+1));
80  KDSetPixel(p, color);
81  e2 = err; x2 = x0;
82  if (2*e2 >= -dx) { // x step
83  for (e2 += dy, y2 = y0; e2 < ed*wd && (y1 != y2 || dx > dy); e2 += dx) {
84  y2 += sy;
85  p = {.x = x0, .y = y2};
86  color = KDColorMix(KDColorRed, KDColorWhite, (abs(err-dx+dy)/ed-wd+1));
87  setPixelColor(x0, y2 += sy, max(0,255*(abs(e2)/ed-wd+1)));
88  }
89  if (x0 == x1) break;
90  e2 = err; err -= dy; x0 += sx;
91  }
92  if (2*e2 <= dy) { // y step
93  for (e2 = dx-e2; e2 < ed*wd && (x1 != x2 || dx < dy); e2 += dy)
94  setPixelColor(x2 += sx, y0, max(0,255*(abs(e2)/ed-wd+1)));
95  if (y0 == y1) break;
96  err += dx; y0 += sy;
97  }
98  }
99 }
100 */
KDCoordinate y() const
Definition: point.h:11
#define assert(e)
Definition: assert.h:9
int16_t KDCoordinate
Definition: coordinate.h:6
Definition: point.h:6
void KDDrawLine(KDPoint p1, KDPoint p2, KDColor c)
Definition: line.cpp:5
c(generic_all_nodes)
Definition: color.h:6
KDCoordinate x() const
Definition: point.h:10