Blame view

build4/epsilon-master/kandinsky/src/context_line.cpp 1.54 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  #include <kandinsky/context.h>
  #include <assert.h>
  
  void KDContext::drawLine(KDPoint p1, KDPoint p2, KDColor c) {
    // Find the largest gap
    KDPoint left = KDPointZero, right = KDPointZero;
    if (p2.x() > p1.x()) {
      left = p1;
      right = p2;
    } else {
      left = p2;
      right = p1;
    }
    KDPoint top = KDPointZero, bottom = KDPointZero;
    if (p2.y() > p1.y()) {
      top = p1;
      bottom = p2;
    } else {
      top = p2;
      bottom = p1;
    }
    assert(right.x() >= left.x());
    assert(bottom.y() >= top.y());
  
    KDCoordinate deltaX = 2*(right.x() - left.x());
    KDCoordinate deltaY = 2*(bottom.y() - top.y());
  
    KDPoint p = KDPointZero, alwaysTranslate = KDPointZero, conditionalTranslate = KDPointZero;
    KDCoordinate scanLength, error, minusError, plusError;
  
    if (deltaX >= deltaY) {
      p = left;
      scanLength = right.x() - left.x();
      error = right.x() - left.x();
      minusError = deltaY;
      plusError = deltaX;
      alwaysTranslate = KDPoint(1,0);
      conditionalTranslate = KDPoint(0, (right.y() >= left.y() ? 1 : -1));
    } else {
      p = top;
      scanLength = bottom.y() - top.y();
      error = bottom.y() - top.y();
      minusError = deltaX;
      plusError = deltaY;
      alwaysTranslate = KDPoint(0,1);
      conditionalTranslate = KDPoint((bottom.x() >= top.x() ? 1 : -1), 0);
    }
  
    KDCoordinate scanCounter = 0;
    while (scanCounter++ < scanLength) {
      setPixel(p, c);
      p = p.translatedBy(alwaysTranslate);
      error = error - minusError;
      if (error <= 0) {
        p = p.translatedBy(conditionalTranslate);
        error = error + plusError;
      }
    }
  }