much love
[mu.git] / 507line.mu
blobd9992747a9cbdbc717e24eae5d61c3be37817584
1 fn draw-line screen: (addr screen), x0: int, y0: int, x1: int, y1: int, color: int {
2   var dx: int
3   var dy: int
4   var sx: int
5   var sy: int
6   var err: int
7   # dx = abs(x1-x0)
8   var tmp2/ecx: int <- copy x1
9   tmp2 <- subtract x0
10   var tmp/eax: int <- abs tmp2
11   copy-to dx, tmp
12   # sx = sgn(x1-x0)
13   tmp <- sgn tmp2
14   copy-to sx, tmp
15   # dy = -abs(y1-y0)
16   tmp2 <- copy y1
17   tmp2 <- subtract y0
18   tmp <- abs tmp2
19   tmp <- negate
20   copy-to dy, tmp
21   # sy = sgn(y1-y0)
22   tmp <- sgn tmp2
23   copy-to sy, tmp
24   # err = dx + dy
25   tmp <- copy dy
26   tmp <- add dx
27   copy-to err, tmp
28   #
29   var x/ecx: int <- copy x0
30   var y/edx: int <- copy y0
31   $draw-line:loop: {
32     pixel screen, x, y, color
33     # if (x == x1 && y == y1) break
34     {
35       compare x, x1
36       break-if-!=
37       compare y, y1
38       break-if-!=
39       break $draw-line:loop
40     }
41     # e2 = err*2
42     var e2/ebx: int <- copy err
43     e2 <- shift-left 1
44     # if (e2 >= dy) { err += dy; x += sx; }
45     {
46       compare e2, dy
47       break-if-<
48       tmp <- copy dy
49       add-to err, tmp
50       x <- add sx
51     }
52     # if (e2 <= dx) { err += dx; y += sy; }
53     {
54       compare e2, dx
55       break-if->
56       tmp <- copy dx
57       add-to err, tmp
58       y <- add sy
59     }
60     loop
61   }
64 fn draw-horizontal-line screen: (addr screen), y: int, x0: int, x1: int, color: int {
65   var x/eax: int <- copy x0
66   {
67     compare x, x1
68     break-if->=
69     pixel screen, x, y, color
70     x <- increment
71     loop
72   }
75 fn draw-vertical-line screen: (addr screen), x: int, y0: int, y1: int, color: int {
76   var y/eax: int <- copy y0
77   {
78     compare y, y1
79     break-if->=
80     pixel screen, x, y, color
81     y <- increment
82     loop
83   }
86 fn draw-rect screen: (addr screen), xmin: int, ymin: int, xmax: int, ymax: int, color: int {
87   var y/eax: int <- copy ymin
88   {
89     compare y, ymax
90     break-if->=
91     draw-horizontal-line screen, y, xmin, xmax, color
92     y <- increment
93     loop
94   }
97 fn draw-rect2 screen: (addr screen), xmin: int, ymin: int, w: int, h: int, color: int {
98   var xmax/eax: int <- copy xmin
99   xmax <- add w
100   var ymax/ecx: int <- copy ymin
101   ymax <- add h
102   draw-rect screen, xmin ymin, xmax ymax, color
105 # 0 <= u <= 1
106 fn line-point u: float, x0: int, x1: int -> _/eax: int {
107   var one/eax: int <- copy 1
108   var u-prime/xmm0: float <- convert one
109   u-prime <- subtract u
110   var result/xmm1: float <- convert x0
111   result <- multiply u-prime
112   var term2/xmm2: float <- convert x1
113   term2 <- multiply u
114   result <- add term2
115   var result/eax: int <- convert result
116   return result