Advertisement
HangMan23

Untitled

Jan 28th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. local computer = require ("computer")
  2.  
  3. ---------------------------------
  4.  
  5. local Render = {Color = {}}
  6.  
  7. ---------------------------------
  8.  
  9. function Render.Color.RGBPack (r, g, b)
  10.  
  11. return (r *(256^2)) + (g * 256) + b
  12.  
  13. end
  14.  
  15. ---------------------------------
  16.  
  17. function Render.Color.RGBExtract (color)
  18.  
  19. color = color % 0x1000000
  20.  
  21. local r = math.floor(color / 0x10000)
  22. local g = math.floor((color - r * 0x10000) / 0x100)
  23. local b = color - r * 0x10000 - g * 0x100
  24.  
  25. return r, g, b
  26.  
  27. end
  28.  
  29. ---------------------------------
  30.  
  31. function Render.Color.Interpolate (a, b, t)
  32.  
  33. local ar, ag, ab = Render.Color.RGBExtract (a)
  34. local br, bg, bb = Render.Color.RGBExtract (b)
  35.  
  36. return Render.Color.RGBPack (math.floor (ar + (br - ar) * t),
  37. math.floor (ag + (bg - ag) * t),
  38. math.floor (ab + (bb - ab) * t))
  39.  
  40. end
  41.  
  42. ---------------------------------
  43.  
  44. function Render.InterpolatePoint (a, b, t)
  45.  
  46. local point = {}
  47.  
  48. point.x = a.x + t * (b.x - a.x)
  49. point.y = a.y + t * (b.y - a.y)
  50. point.z = a.z + t * (b.z - a.z)
  51.  
  52. if t > 1 or t < 0 then computer.beep (1000, 0.01) end
  53.  
  54. point.color = Render.Color.Interpolate (a.color, b.color, t)
  55.  
  56. return point
  57.  
  58. end
  59.  
  60. ---------------------------------
  61.  
  62. function Render.SortPointsByY (a, b, c)
  63.  
  64. if (c.y < a.y) then local tc = c; c = a; a = tc end
  65. if (c.y < b.y) then local tc = c; b = c; c = tc end
  66. if (b.y < a.y) then local ta = a; a = b; b = ta end
  67.  
  68. return a, b, c
  69.  
  70. end
  71.  
  72. ---------------------------------
  73.  
  74. function Render.Triangle (a, b, c, setpixel_func, renderscale)
  75.  
  76. local renderscale = renderscale or 1
  77.  
  78. a, b, c = Render.SortPointsByY (a, b, c)
  79.  
  80. if a.y == c.y then
  81.  
  82. if a.x == c.x then
  83.  
  84. setpixel_func (a.x, a.y, a.z, a.color)
  85.  
  86. return
  87.  
  88. end
  89.  
  90. if a.x > c.x then local tx = a.x; a.x = c.x; c.x = tx end
  91.  
  92. local dt = 1 / (c.x - a.x)
  93. local t = 0;
  94.  
  95. for x = a.x, c.x do
  96.  
  97. if t > 1 then t = 1 end
  98.  
  99. local d = Render.InterpolatePoint (a, c, t)
  100.  
  101. setpixel_func (d.x, d.y, d.z, d.color)
  102.  
  103. t = t + dt
  104.  
  105. end
  106.  
  107. return
  108.  
  109. elseif a.y == b.y then
  110.  
  111. if a.y > c.y then ta = a; a = c; c = ta end
  112.  
  113. local dt0 = 1 / (c.y - a.y)
  114. local t0 = dt0
  115.  
  116. for y = a.y, c.y do
  117.  
  118. if t0 > 1 then t0 = 1 end
  119.  
  120. local e = Render.InterpolatePoint (a, c, t0)
  121. local f = Render.InterpolatePoint (b, c, t0)
  122.  
  123. e.y = y
  124. f.y = y
  125.  
  126. if e.x == f.x then
  127.  
  128. setpixel_func (e.x, e.y, e.z, e.color)
  129.  
  130. else
  131.  
  132. if f.x > e.x then local tf = f; f = e; e = tf end
  133.  
  134. local dt1 = 1 / (e.x - f.x)
  135. local t1 = 0;
  136.  
  137. for x = f.x, e.x do
  138.  
  139. if t1 > 1 then t1 = 1 end
  140.  
  141. g = Render.InterpolatePoint (f, e, t1)
  142.  
  143. g.x = x;
  144.  
  145. setpixel_func (g.x, g.y, g.z, g.color)
  146.  
  147. t1 = t1 + dt1
  148.  
  149. end
  150.  
  151. end
  152.  
  153. t0 = t0 + dt0
  154.  
  155. end
  156.  
  157. return
  158.  
  159. elseif c.y == b.y then
  160.  
  161. local dt0 = 1 / (c.y - a.y)
  162. local t0 = 0;
  163.  
  164. for y = a.y, c.y do
  165.  
  166. if t0 > 1 then t0 = 1 end
  167.  
  168. e = Render.InterpolatePoint (a, c, t0)
  169. f = Render.InterpolatePoint (a, b, t0)
  170.  
  171. e.y = y
  172. f.y = y
  173.  
  174. if t0 > 1 then t0 = 1 end
  175.  
  176. if e.x == f.x then
  177.  
  178. setpixel_func (e.x, e.y, e.z, e.color)
  179.  
  180. else
  181.  
  182. if f.x > e.x then tf = f; f = e; e = tf end
  183.  
  184. local dt1 = 1 / (e.x - f.x)
  185. local t1 = 0
  186.  
  187. local ex = math.floor (e.x)
  188. if a.y < c.y then ex = math.ceil (e.x) end
  189.  
  190. for x = f.x, ex do
  191.  
  192. if t1 > 1 then t1 = 1 end
  193.  
  194. local g = Render.InterpolatePoint (f, e, t1)
  195.  
  196. g.x = x
  197.  
  198. setpixel_func (g.x, g.y, g.z, g.color)
  199.  
  200. t1 = t1 + dt1
  201.  
  202. end
  203.  
  204. end
  205.  
  206. t0 = t0 + dt0
  207.  
  208. end
  209.  
  210. return
  211.  
  212. else
  213.  
  214. local d = Render.InterpolatePoint (a, c, (b.y - a.y) / (c.y - a.y))
  215.  
  216. if d.y == a.y then return end
  217.  
  218. local dt0 = 1 / (d.y - a.y) / renderscale
  219. local t0 = 0;
  220.  
  221. for y = a.y, d.y, 1 / renderscale do
  222.  
  223. if t0 > 1 then t0 = 1 end
  224.  
  225. local e = Render.InterpolatePoint (a, d, t0)
  226. local f = Render.InterpolatePoint (a, b, t0)
  227.  
  228. e.y = y
  229. f.y = y
  230.  
  231. if t0 > 1 then t0 = 1 end
  232.  
  233. if e.x == f.x then
  234.  
  235. setpixel_func (e.x, e.y, e.y, e.color);
  236.  
  237. else
  238.  
  239. if f.x > e.x then local tf = f; f = e; e = tf end
  240.  
  241. local dt1 = 1 / (e.x - f.x) / renderscale;
  242. local t1 = 0;
  243.  
  244. for x = f.x, e.x, 1 / renderscale do
  245.  
  246. if t1 > 1 then t1 = 1 end
  247.  
  248. local g = Render.InterpolatePoint (f, e, t1)
  249.  
  250. setpixel_func (x, g.y, g.z, g.color)
  251.  
  252. t1 = t1 + dt1
  253.  
  254. end
  255.  
  256. end
  257.  
  258. t0 = t0 + dt0
  259.  
  260. end
  261.  
  262. if d.y == c.y then return end
  263.  
  264. dt0 = 1 / (c.y - d.y) / renderscale
  265. t0 = dt0;
  266.  
  267. for y = d.y, c.y, 1 / renderscale do
  268.  
  269. if t0 > 1 then t0 = 1 end
  270.  
  271. local e = Render.InterpolatePoint (d, c, t0)
  272. local f = Render.InterpolatePoint (b, c, t0)
  273.  
  274. e.y = y
  275. f.y = y
  276.  
  277. if e.x == f.x then
  278.  
  279. setpixel_func (e.x, e.y, e.z, e.color);
  280.  
  281. else
  282.  
  283. if f.x > e.x then local tf = f; f = e; e = tf end
  284.  
  285. local dt1 = 1 / (e.x - f.x) / renderscale;
  286. local t1 = 0;
  287.  
  288. local ex = e.x
  289. if b.x < c.x then e.x = e.x + 2 else e.x = e.x - 2 end
  290.  
  291. for x = f.x, ex, 1 / renderscale do
  292.  
  293. if t1 > 1 then t1 = 1 end
  294.  
  295. local g = Render.InterpolatePoint (f, e, t1)
  296.  
  297. setpixel_func (x, g.y, g.z, g.color)
  298.  
  299. t1 = t1 + dt1
  300.  
  301. end
  302.  
  303. end
  304.  
  305. t0 = t0 + dt0
  306.  
  307. end
  308.  
  309. end
  310.  
  311. end
  312.  
  313. ---------------------------------
  314.  
  315. return Render
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement