Curvy road

Coord-generator for roadwarrior3, which generates a smoothly curving road passing through a specified set of points. This function is an example of calculating the points instead of using the GUI of roadwarrior3.

function slingerweg_vb

% Slingerweg voorbeeld.
% 
% Maakt automatisch een vierkante baan met een rechte, en oplopend-bochtige stukken
% Bochterigheid (2 of meer) is gedefinieerd als aantal bochten op een gegeven lijnstuk.

coords = [];
% Rechte stuk: bovenste zijde.
x0 = -0.75;
y0 = 0.75;
x1 = 0.75;
y1 = 0.75;
coords = [coords; x0 y0; x1 y1];

% Bochtige stuk 1: rechter zijde.
%
% Bochten definieren als zig-zag, worden bij berekenen weg afgerond.
bochtigheid = 8;
x0 = 0.75;
y0 = 0.75;
x1 = 0.75;
y1 = -0.75;
lijnstuk = bochelLijn(x0, y0, x1, y1, bochtigheid);
coords = [coords; lijnstuk];

% Bochtige stuk 2: onderste zijde.
%
% Bochten definieren als zig-zag, worden bij berekenen weg afgerond.
bochtigheid = 12;
x0 = 0.75;
y0 = -0.75;
x1 = -0.75;
y1 = -0.75;
lijnstuk = bochelLijn(x0, y0, x1, y1, bochtigheid);
coords = [coords; lijnstuk];

% Bochtige stuk 3: linker zijde.
%
% Bochten definieren als zig-zag, worden bij berekenen weg afgerond.
bochtigheid = 16;
x0 = -0.75;
y0 = -0.75;
x1 = -0.75;
y1 = 0.75;
lijnstuk = bochelLijn(x0, y0, x1, y1, bochtigheid);
coords = [coords; lijnstuk];

roadwarrior3(coords);

function lijnstuk = bochelLijn(x0, y0, x1, y1, bochtigheid);
lijnstuk = [x0 y0];
dx = (x1 - x0) / bochtigheid;
dy = (y1 - y0) / bochtigheid;
bocht_amplitude = leng(x1 - x0, y1 - y0) / bochtigheid;
for iHoek = 1:(bochtigheid - 1),
    % basis
    x_hoek = x0 + dx * iHoek;
    y_hoek = y0 + dy * iHoek;
    % zijwaartse stap
    x_zij = -dy;
    y_zij = dx;
    L_zijstap = leng(x_zij, y_zij);
    alterneer = -1 + 2 * mod(iHoek, 2);
    x_zij = alterneer * bocht_amplitude * x_zij / L_zijstap;
    y_zij = alterneer * bocht_amplitude * y_zij / L_zijstap;
    lijnstuk = [lijnstuk; x_hoek + x_zij y_hoek + y_zij];
end;
lijnstuk = [lijnstuk; x1 y1];

function l = leng(dx, dy)
l = sqrt(dx^2 + dy^2);