Skip to content

Commit

Permalink
Small refactor point
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Jun 24, 2023
1 parent d615713 commit 97c0b53
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
60 changes: 29 additions & 31 deletions lua/gesture/core/point.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
local listlib = require("gesture.lib.list")

local Point = {}
Point.__index = Point

function Point.new(x, y)
return {
x = x,
y = y,
}
end

local Y = function(p1, p2)
local b = (p1.x * p2.y - p2.x * p1.y) / (p1.x - p2.x)
Expand All @@ -11,47 +17,47 @@ local Y = function(p1, p2)
end
end

function Point.interpolate(self, point)
function Point.interpolate(p1, p2)
local points = {}
if self.x == point.x then
local p1 = self
local p2 = point
if p1.x == p2.x then
local p_start = p1
local p_end = p2
local reverse = false
if self.y > point.y then
p1 = point
p2 = self
table.insert(points, p1)
if p1.y > p2.y then
p_start = p2
p_end = p1
table.insert(points, p_start)
reverse = true
end

local x = p1.x
local y = p1.y + 1
while y < p2.y do
local x = p_start.x
local y = p_start.y + 1
while y < p_end.y do
table.insert(points, Point.new(x, y))
y = y + 1
end

if reverse then
return listlib.reverse(points)
end
table.insert(points, p2)
table.insert(points, p_end)
return points
end

local p1 = self
local p2 = point
local p_start = p1
local p_end = p2
local reverse = false
if self.x > point.x then
p1 = point
p2 = self
if p1.x > p2.x then
p_start = p2
p_end = p1
reverse = true
end
table.insert(points, p1)
table.insert(points, p_start)

local offset = 0.1
local x = p1.x + offset
local get_y = Y(p1, p2)
while x < p2.x do
local x = p_start.x + offset
local get_y = Y(p_start, p_end)
while x < p_end.x do
local y = math.floor(get_y(x) + 0.5)
local new = Point.new(math.floor(x + 0.5), y)
local last = points[#points]
Expand All @@ -65,16 +71,8 @@ function Point.interpolate(self, point)
return listlib.reverse(points)
end
table.remove(points, 1)
table.insert(points, p2)
table.insert(points, p_end)
return points
end

function Point.new(x, y)
local tbl = {
x = x,
y = y,
}
return setmetatable(tbl, Point)
end

return Point
5 changes: 4 additions & 1 deletion lua/gesture/core/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ end
function State.action_context(self)
local point = self.view.current_point()
return {
last_position = { point.y, point.x },
last_position = {
point.y,
point.x,
},

inputs = vim.tbl_map(function(input)
return input
Expand Down
2 changes: 1 addition & 1 deletion lua/gesture/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function View.focus(self, last_point)
else
last = self._new_points[#self._new_points] or last_point
end
self._new_points = last:interpolate(current_point)
self._new_points = Point.interpolate(last, current_point)

return current_point
end
Expand Down

0 comments on commit 97c0b53

Please sign in to comment.