Let’s try that idea for improving
make_waypoints
. I have a good feeling about that.
The idea is to work from a list of (len, t) and make waypoints from adjacent pairs.
I think I’ll do it with a real list first and then look to see how to do it even better. After not too much difficulty, I come up with this:
function D_Bezier:make_waypoints_refactored(intervals)
-- make a list of four intervale, five pairs (len, t)
-- make a list of waypoints from the ordered pairs from that list
local len = 0
local pos = self.bezier:at(0)
local len_t_items = {}
for i = 0, intervals do
local t = i/intervals
local new_pos = self.bezier:at(t)
local span = vector.magnitude((new_pos-pos))
len = len + span
local item = {len=len, t=t}
table.insert(len_t_items, item)
pos = new_pos
end
local waypoints = {}
for i = 1, intervals do
p1 = len_t_items[i]
p2 = len_t_items[i+1]
wp = WayPoint(p1.len, p2.len, p1.t, p2.t)
table.insert(waypoints, wp)
end
return waypoints
end
This is longer than the original:
function D_Bezier:make_waypoints(intervals)
local wps = {}
local min_d = 0
local min_t = 0
local min_pos = self.bezier:at(min_t)
for i = 1, intervals do
local max_t = i/intervals -- 1/8, 2/8
local max_pos = self.bezier:at(max_t)
local span = vector.magnitude(max_pos-min_pos)
local max_d = min_d + span
wp = WayPoint(min_d, max_d, min_t, max_t)
table.insert(wps, WayPoint(min_d, max_d, min_t, max_t))
min_t = max_t
min_d = max_d
min_pos = max_pos
end
return wps
end
But I think our new versions is more clear and that it can be more easily improved. It passes all the tests, so it is matching the results of the other. That gives me confidence in both versions.
I’m not satisfied in the sense that this is good enough, but I am satisfied that it is better. One obscurity that I do not like is the first iteration going zero through intervals and the second going one through intervals. But it is the nature of the solution to create five endpoints and then make four objects with low and high.
Possibly the waypoint idea itself is flawed and a list of single values would work as well. We’ll experiment with that as well. I do like that we can just find the right waypoint and then ask it to evaluate, which keeps all the logic inside it. A little extra hassle in making something useful often pays off, in my experience.
Again progress. Safe paths!