-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.fs
112 lines (96 loc) · 1.7 KB
/
line.fs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
[undefined] line.fs [if]
vocabulary line.fs also line.fs definitions
require sdl.fs
require zbuffer.fs
also sdl.fs
also zbuffer.fs
0 value x0
0 value y0
0 value x1
0 value y1
0 value dx
0 value dy
0 value e
0 value s
: line-put-pixel
x0 y0 pixel-off? 0= if
x0 y0 put-pixel
then
;
: linev
dx abs to dx
dy abs to dy
dx 1 rshift to e \ needed?
x1 x0 > if 1 else -1 then to s
begin
y1 y0 >
while
line-put-pixel
e dx + to e
e dy >= if
e dy - to e
x0 s + to x0
then
y0 1+ to y0
\ yield
repeat
;
: lineh
dx abs to dx
dy abs to dy
dy 1 rshift to e \ needed?
y1 y0 > if 1 else -1 then to s
begin
x1 x0 >
while
line-put-pixel
e dy + to e
e dx >= if
e dx - to e
y0 s + to y0
\ yield
then
x0 1+ to x0
repeat
;
: line ( x0 y0 x1 y1 -- )
to y1 to x1 to y0 to x0
x1 x0 - to dx
y1 y0 - to dy
dx abs dy abs > if \ horizontal
dx 0 > if \ right
lineh
else \ left
x0 x1 swap to x1 to x0
y0 y1 swap to y1 to y0
lineh
then
else \ vertical
dy 0 > if \ down
linev
else \ up
x0 x1 swap to x1 to x0
y0 y1 swap to y1 to y0
linev
then
then
;
\ #width 2/ value ox
\ #height 2/ value oy
\ : line-test
\ init-sdl
\ 255 255 255 set-color
\ ox oy ox 20 + oy 50 - line \ sector 0
\ ox oy ox 50 + oy 20 + line \ sector 1
\ ox oy ox 50 + oy 20 - line \ sector 2
\ ox oy ox 20 + oy 50 + line \ sector 3
\ ox oy ox 20 - oy 50 - line \ sector 4
\ ox oy ox 50 - oy 20 + line \ sector 5
\ ox oy ox 50 - oy 20 - line \ sector 6
\ ox oy ox 20 - oy 50 + line \ sector 7
\ flip-screen
\ wait-key
\ sdl-quit
\ ;
previous definitions
[then]