Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add max smear length #57

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lua/smear_cursor/animation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,50 @@ vim.defer_fn(function()
end, 0)

local function update()
local distance_head_to_target = math.huge
local index_head = 0

-- Move toward targets
for i = 1, 4 do
local distance_squared = (current_corners[i][1] - target_corners[i][1]) ^ 2
+ (current_corners[i][2] - target_corners[i][2]) ^ 2
local stiffness = math.min(1, stiffnesses[i] * distance_squared ^ config.slowdown_exponent)

if distance_squared < distance_head_to_target then
distance_head_to_target = distance_squared
index_head = i
end

for j = 1, 2 do
current_corners[i][j] = current_corners[i][j] + (target_corners[i][j] - current_corners[i][j]) * stiffness
end
end

-- Shorten smear if too long
local smear_length = 0

for i = 1, 4 do
if i ~= index_head then
-- stylua: ignore
local distance = math.sqrt(
(current_corners[i][1] - current_corners[index_head][1]) ^ 2 +
(current_corners[i][2] - current_corners[index_head][2]) ^ 2
)
smear_length = math.max(smear_length, distance)
end
end

if smear_length <= config.max_length then return end
local factor = config.max_length / smear_length

for i = 1, 4 do
if i ~= index_head then
for j = 1, 2 do
current_corners[i][j] = current_corners[index_head][j]
+ (current_corners[i][j] - current_corners[index_head][j]) * factor
end
end
end
end

local function get_center(corners)
Expand Down
1 change: 1 addition & 0 deletions lua/smear_cursor/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ M.matrix_pixel_threshold = 0.7 -- 0: all pixels, 1: no pixel
M.matrix_pixel_min_factor = 0.5 -- 0: all pixels, 1: no pixel
M.volume_reduction_exponent = 0.3 -- 0: no reduction, 1: full reduction
M.minimum_volume_factor = 0.7 -- 0: no limit, 1: no reduction
M.max_length = 25 -- Maximum smear length

-- For debugging ---------------------------------------------------------------

Expand Down
Loading