-
Notifications
You must be signed in to change notification settings - Fork 1
/
trajectory2.p8
60 lines (54 loc) · 1.75 KB
/
trajectory2.p8
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
pico-8 cartridge // http://www.pico-8.com
version 29
__lua__
-- point to point trajectory and animation
-- by @apa64
function _init()
p1 = { x = 10, y = 10 }
p2 = { x = 100, y = 10 }
pk = { speed = 2, c = 11 }
end
function _update()
pk.x = pk.x and (pk.x + pk.speed) or 0
--[[
if (dx == 0) then
-- vertical line
return xk
end
if (dy == 0) then
-- horizontal line
end
--]]
pk.y = pointslope(p1.x, p1.y, p2.x, p2.y, pk.x)
if (pk.x > 127 or pk.y > 127) then
pk.x = 0
p2.y += 10
end
end
function _draw()
cls(1)
-- line1
line(p1.x, p1.y, p2.x, p2.y, 5)
pset(pk.x, pk.y, pk.c)
end
function pointslope(x1, y1, x2, y2, xk)
local dx = x2 - x1
-- can't handle vertical lines
assert(dx != 0)
local dy = y2 - y1
-- slope
local m = dy / dx
-- y = mx + b | b = y - mx
local b = y2 - m * x2
local yk = m * xk + b
-- local xk = (yk - b)/m
-- oneliner: yk = ((y2 - y1)/(x2 - x1)) * xk + (y2 - ((y2 - y1) / (x2 - x1)) * x2)
return yk
end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000