Numworks Epsilon  1.4.1
Graphing Calculator Operating System
context_line.cpp
Go to the documentation of this file.
1 #include <kandinsky/context.h>
2 #include <assert.h>
3 
5  // Find the largest gap
6  KDPoint left = KDPointZero, right = KDPointZero;
7  if (p2.x() > p1.x()) {
8  left = p1;
9  right = p2;
10  } else {
11  left = p2;
12  right = p1;
13  }
14  KDPoint top = KDPointZero, bottom = KDPointZero;
15  if (p2.y() > p1.y()) {
16  top = p1;
17  bottom = p2;
18  } else {
19  top = p2;
20  bottom = p1;
21  }
22  assert(right.x() >= left.x());
23  assert(bottom.y() >= top.y());
24 
25  KDCoordinate deltaX = 2*(right.x() - left.x());
26  KDCoordinate deltaY = 2*(bottom.y() - top.y());
27 
28  KDPoint p = KDPointZero, alwaysTranslate = KDPointZero, conditionalTranslate = KDPointZero;
29  KDCoordinate scanLength, error, minusError, plusError;
30 
31  if (deltaX >= deltaY) {
32  p = left;
33  scanLength = right.x() - left.x();
34  error = right.x() - left.x();
35  minusError = deltaY;
36  plusError = deltaX;
37  alwaysTranslate = KDPoint(1,0);
38  conditionalTranslate = KDPoint(0, (right.y() >= left.y() ? 1 : -1));
39  } else {
40  p = top;
41  scanLength = bottom.y() - top.y();
42  error = bottom.y() - top.y();
43  minusError = deltaX;
44  plusError = deltaY;
45  alwaysTranslate = KDPoint(0,1);
46  conditionalTranslate = KDPoint((bottom.x() >= top.x() ? 1 : -1), 0);
47  }
48 
49  KDCoordinate scanCounter = 0;
50  while (scanCounter++ < scanLength) {
51  setPixel(p, c);
52  p = p.translatedBy(alwaysTranslate);
53  error = error - minusError;
54  if (error <= 0) {
55  p = p.translatedBy(conditionalTranslate);
56  error = error + plusError;
57  }
58  }
59 }
KDCoordinate y() const
Definition: point.h:11
void setPixel(KDPoint p, KDColor c)
#define assert(e)
Definition: assert.h:9
int16_t KDCoordinate
Definition: coordinate.h:6
constexpr KDPoint KDPointZero
Definition: point.h:25
Definition: point.h:6
c(generic_all_nodes)
void drawLine(KDPoint p1, KDPoint p2, KDColor c)
Definition: context_line.cpp:4
Definition: color.h:6
KDCoordinate x() const
Definition: point.h:10
KDPoint translatedBy(KDPoint other) const
Definition: point.cpp:3