# Line3D Demo

Jun 24th, 2021
1,760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. SCREEN 13
2.
3. DIM SHARED Znear
4.
5. Znear = .5
6.
7. TYPE vec3
8.   x AS SINGLE
9.   y AS SINGLE
10.   z AS SINGLE
11. END TYPE
12.
13. TYPE vec2
14.   x AS SINGLE
15.   y AS SINGLE
16. END TYPE
17.
18. DIM a1 AS vec3
19. DIM a2 AS vec3
20. DIM org AS vec3
21. DIM rot AS vec3
22.
23. size = 16
24.
25. org.x = 0: org.y = -.25: org.z = 0
26. rot.x = 0: rot.y = 2: rot.z = 0
27.
28. DO
29.
30.   LINE (0, 0)-(320, 200), 1, BF
31.
32.   ' Horizontal lines
33.   ' ---------------------
34.   FOR x = -size TO size
35.
36.     a1.x = x: a1.y = 0: a1.z = -size
37.     a2.x = x: a2.y = 0: a2.z = size
38.     pd a1, a2, org, rot
39.
40.   NEXT
41.
42.   ' Vertical lines
43.   ' ---------------------
44.   FOR z = -size TO size
45.
46.     a1.x = -size: a1.y = 0: a1.z = z
47.     a2.x = size: a2.y = 0: a2.z = z
48.     pd a1, a2, org, rot
49.
50.   NEXT
51.
52.   ' Wait key
53.   DO: i\$ = INKEY\$: LOOP WHILE i\$ = ""
54.
55.   ' Up
56.   IF i\$ = CHR\$(0) + "H" THEN org.x = org.x - SIN(rot.y) * .1: org.z = org.z - COS(rot.y) * .1
57.
58.   ' Down
59.   IF i\$ = CHR\$(0) + "P" THEN org.x = org.x + SIN(rot.y) * .1: org.z = org.z + COS(rot.y) * .1
60.
61.   ' Left / Right
62.   IF i\$ = CHR\$(0) + "K" THEN rot.y = rot.y - .05
63.   IF i\$ = CHR\$(0) + "M" THEN rot.y = rot.y + .05
64.
65.   ' PgDn/PgUp
66.   IF i\$ = CHR\$(0) + "I" THEN org.y = org.y - .1
67.   IF i\$ = CHR\$(0) + "Q" THEN org.y = org.y + .1
68.
69. LOOP UNTIL i\$ = CHR\$(27)
70.
71. SUB pd (v1 AS vec3, v2 AS vec3, o AS vec3, r AS vec3)
72.
73.   DIM a AS vec3, b AS vec3, e AS vec3
74.   DIM c AS vec2, d AS vec2
75.
76.   ' Move coordinates
77.   a.x = v1.x + o.x: b.x = v2.x + o.x
78.   a.y = v1.y + o.y: b.y = v2.y + o.y
79.   a.z = v1.z + o.z: b.z = v2.z + o.z
80.
81.   ' RotateY
82.   rt a, r
83.   rt b, r
84.
85.   ' Cross z-near
86.   t = (Znear - a.z) / (b.z - a.z)
87.
88.   e.x = a.x + t * (b.x - a.x)
89.   e.y = a.y + t * (b.y - a.y)
90.   e.z = a.z + t * (b.z - a.z)
91.
92.   ' Line behind Znear
93.   IF a.z < Znear AND b.z < Znear THEN EXIT SUB
94.
95.   ' Line crossing Znear
96.   IF a.z > Znear AND b.z < Znear THEN b = e ELSE IF a.z < Znear AND b.z > Znear THEN a = e
97.
98.   vp a, c
99.   vp b, d
100.
101.   LINE (c.x, c.y)-(d.x, d.y), 15
102.
103. END SUB
104.
105. SUB rt (v AS vec3, r AS vec3)
106.
107.   DIM m AS vec3
108.
109.   ' Rotate by Y
110.   m.x = v.x * COS(r.y) - v.z * SIN(r.y)
111.   m.y = v.y
112.   m.z = v.z * COS(r.y) + v.x * SIN(r.y)
113.
114.   v = m
115.
116. END SUB
117.
118. SUB vp (a AS vec3, b AS vec2)
119.
120.   b.x = 160 + a.x * 200 / a.z
121.   b.y = 100 - a.y * 200 / a.z
122.
123. END SUB
124.
125.