mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-06 04:09:47 +00:00
Fixed docs formating and code indentation.
This commit is contained in:
parent
c6f4f7c586
commit
a89edec667
1 changed files with 127 additions and 120 deletions
231
shapes2d.scad
231
shapes2d.scad
|
@ -196,8 +196,8 @@ module arc(N, r, angle, d, cp, points, width, thickness, start, wedge=false)
|
||||||
|
|
||||||
|
|
||||||
function _normal_segment(p1,p2) =
|
function _normal_segment(p1,p2) =
|
||||||
let(center = (p1+p2)/2)
|
let(center = (p1+p2)/2)
|
||||||
[center, center + norm(p1-p2)/2 * line_normal(p1,p2)];
|
[center, center + norm(p1-p2)/2 * line_normal(p1,p2)];
|
||||||
|
|
||||||
|
|
||||||
// Function&Module: trapezoid()
|
// Function&Module: trapezoid()
|
||||||
|
@ -540,34 +540,34 @@ module supershape(step=0.5,m1=4,m2=undef,n1,n2=undef,n3=undef,a=1,b=undef, r=und
|
||||||
|
|
||||||
// Function: turtle()
|
// Function: turtle()
|
||||||
// Usage:
|
// Usage:
|
||||||
// turtle(commands, [state], [return_state])
|
// turtle(commands, [state], [return_state])
|
||||||
// Description:
|
// Description:
|
||||||
// Use a sequence of turtle graphics commands to generate a path. The parameter `commands` is a list of
|
// Use a sequence of turtle graphics commands to generate a path. The parameter `commands` is a list of
|
||||||
// turtle commands and optional parameters for each command. The turtle state has a position, movement direction,
|
// turtle commands and optional parameters for each command. The turtle state has a position, movement direction,
|
||||||
// movement distance, and default turn angle. If you do not give `state` as input then the turtle starts at the
|
// movement distance, and default turn angle. If you do not give `state` as input then the turtle starts at the
|
||||||
// origin, pointed along the positive x axis with a movement distance of 1. By default, `turtle` returns just
|
// origin, pointed along the positive x axis with a movement distance of 1. By default, `turtle` returns just
|
||||||
// the computed turtle path. If you set `full_state` to true then it instead returns the full turtle state.
|
// the computed turtle path. If you set `full_state` to true then it instead returns the full turtle state.
|
||||||
// You can invoke `turtle` again with this full state to continue the turtle path where you left off.
|
// You can invoke `turtle` again with this full state to continue the turtle path where you left off.
|
||||||
//
|
//
|
||||||
// For the list below, `dist` is the current movement distance.
|
// For the list below, `dist` is the current movement distance.
|
||||||
//
|
//
|
||||||
// Turtle commands:
|
// Turtle commands:
|
||||||
// - "move", [scale]: Move turtle scale*dist units in the turtle direction. Default scale=1.
|
// - "move", [scale]: Move turtle scale*dist units in the turtle direction. Default scale=1.
|
||||||
// - "xmove", [scale]: Move turtle scale*dist units in the x direction. Default scale=1.
|
// - "xmove", [scale]: Move turtle scale*dist units in the x direction. Default scale=1.
|
||||||
// - "ymove", [scale]: Move turtle scale*dist units in the y direction. Default scale=1.
|
// - "ymove", [scale]: Move turtle scale*dist units in the y direction. Default scale=1.
|
||||||
// - "untilx", xtarget: Move turtle in turtle direction until x==xtarget. Produces an error if xtarget is not reachable.
|
// - "untilx", xtarget: Move turtle in turtle direction until x==xtarget. Produces an error if xtarget is not reachable.
|
||||||
// - "untily", ytarget: Move turtle in turtle direction until y==ytarget. Produces an error if xtarget is not reachable.
|
// - "untily", ytarget: Move turtle in turtle direction until y==ytarget. Produces an error if xtarget is not reachable.
|
||||||
// - "jump", point: Move the turtle to the specified point
|
// - "jump", point: Move the turtle to the specified point
|
||||||
// - "xjump", x: Move the turtle's x position to the specified value
|
// - "xjump", x: Move the turtle's x position to the specified value
|
||||||
// - "yjump, y: Move the turtle's y position to the specified value
|
// - "yjump, y: Move the turtle's y position to the specified value
|
||||||
// - "turn", [angle]: Turn turtle direction by specified angle, or the turtle's default turn angle. The default angle starts at 90.
|
// - "turn", [angle]: Turn turtle direction by specified angle, or the turtle's default turn angle. The default angle starts at 90.
|
||||||
// - "left", [angle]: Same as "turn"
|
// - "left", [angle]: Same as "turn"
|
||||||
// - "right", [angle]: Same as "turn", -angle
|
// - "right", [angle]: Same as "turn", -angle
|
||||||
// - "angle", angle: Set the default turn angle.
|
// - "angle", angle: Set the default turn angle.
|
||||||
// - "setdir", dir: Set turtle direction. The parameter `dir` can be an angle or a vector.
|
// - "setdir", dir: Set turtle direction. The parameter `dir` can be an angle or a vector.
|
||||||
// - "length", length: Change the turtle move distance to `length`
|
// - "length", length: Change the turtle move distance to `length`
|
||||||
// - "scale", factor: Multiply turtle move distance by `factor`
|
// - "scale", factor: Multiply turtle move distance by `factor`
|
||||||
// - "addlength", length: Add `length` to the turtle move distance
|
// - "addlength", length: Add `length` to the turtle move distance
|
||||||
//
|
//
|
||||||
// Arguments:
|
// Arguments:
|
||||||
// commands = list of turtle commands
|
// commands = list of turtle commands
|
||||||
|
@ -584,29 +584,31 @@ module supershape(step=0.5,m1=4,m2=undef,n1,n2=undef,n3=undef,a=1,b=undef, r=und
|
||||||
// path = turtle(flatten(replist(["move","left",144],10)));
|
// path = turtle(flatten(replist(["move","left",144],10)));
|
||||||
// stroke(path,width=.05);
|
// stroke(path,width=.05);
|
||||||
// Example(2d): Sawtooth path
|
// Example(2d): Sawtooth path
|
||||||
// path = turtle(["turn", 55,
|
// path = turtle([
|
||||||
// "untily", 2,
|
// "turn", 55,
|
||||||
// "turn", -55-90,
|
// "untily", 2,
|
||||||
// "untily", 0,
|
// "turn", -55-90,
|
||||||
// "turn", 55+90,
|
// "untily", 0,
|
||||||
// "untily", 2.5,
|
// "turn", 55+90,
|
||||||
// "turn", -55-90,
|
// "untily", 2.5,
|
||||||
// "untily", 0,
|
// "turn", -55-90,
|
||||||
// "turn", 55+90,
|
// "untily", 0,
|
||||||
// "untily", 3,
|
// "turn", 55+90,
|
||||||
// "turn", -55-90,
|
// "untily", 3,
|
||||||
// "untily", 0
|
// "turn", -55-90,
|
||||||
// ]);
|
// "untily", 0
|
||||||
|
// ]);
|
||||||
// stroke(path, width=.1);
|
// stroke(path, width=.1);
|
||||||
// Example(2d): Simpler way to draw the sawtooth. The direction of the turtle is preserved when executing "yjump".
|
// Example(2d): Simpler way to draw the sawtooth. The direction of the turtle is preserved when executing "yjump".
|
||||||
// path = turtle(["turn", 55,
|
// path = turtle([
|
||||||
// "untily", 2,
|
// "turn", 55,
|
||||||
// "yjump", 0,
|
// "untily", 2,
|
||||||
// "untily", 2.5,
|
// "yjump", 0,
|
||||||
// "yjump", 0,
|
// "untily", 2.5,
|
||||||
// "untily", 3,
|
// "yjump", 0,
|
||||||
// "yjump", 0,
|
// "untily", 3,
|
||||||
// ]);
|
// "yjump", 0,
|
||||||
|
// ]);
|
||||||
// stroke(path, width=.1);
|
// stroke(path, width=.1);
|
||||||
// Example(2d): square spiral
|
// Example(2d): square spiral
|
||||||
// path = turtle(flatten(replist(["move","left","addlength",1],50)));
|
// path = turtle(flatten(replist(["move","left","addlength",1],50)));
|
||||||
|
@ -624,80 +626,85 @@ module supershape(step=0.5,m1=4,m2=undef,n1,n2=undef,n3=undef,a=1,b=undef, r=und
|
||||||
// function koch_unit(depth) =
|
// function koch_unit(depth) =
|
||||||
// depth==0 ? ["move"] :
|
// depth==0 ? ["move"] :
|
||||||
// concat(
|
// concat(
|
||||||
// koch_unit(depth-1),
|
// koch_unit(depth-1),
|
||||||
// ["right"],
|
// ["right"],
|
||||||
// koch_unit(depth-1),
|
// koch_unit(depth-1),
|
||||||
// ["left","left"],
|
// ["left","left"],
|
||||||
// koch_unit(depth-1),
|
// koch_unit(depth-1),
|
||||||
// ["right"],
|
// ["right"],
|
||||||
// koch_unit(depth-1)
|
// koch_unit(depth-1)
|
||||||
// );
|
// );
|
||||||
// koch=concat(["angle",60],flatten(replist(concat(koch_unit(3),["left","left"]),3)));
|
// koch=concat(["angle",60],flatten(replist(concat(koch_unit(3),["left","left"]),3)));
|
||||||
// polygon(turtle(koch));
|
// polygon(turtle(koch));
|
||||||
function turtle(commands, state=[[[0,0]],[1,0],90], full_state=false) =
|
function turtle(commands, state=[[[0,0]],[1,0],90], full_state=false) =
|
||||||
let( state = is_vector(state) ? [[state],[1,0],90] : state )
|
let( state = is_vector(state) ? [[state],[1,0],90] : state )
|
||||||
_turtle(commands,state,full_state);
|
_turtle(commands,state,full_state);
|
||||||
|
|
||||||
function _turtle(commands, state, full_state, index=0) =
|
function _turtle(commands, state, full_state, index=0) =
|
||||||
index < len(commands) ? _turtle(commands,
|
index < len(commands) ?
|
||||||
turtle_command(commands[index],commands[index+1],state,index),
|
_turtle(commands,
|
||||||
full_state,
|
turtle_command(commands[index],commands[index+1],state,index),
|
||||||
index+(!is_string(commands[index+1])?2:1)
|
full_state,
|
||||||
)
|
index+(!is_string(commands[index+1])?2:1)
|
||||||
: ( full_state ? state : state[0] );
|
) :
|
||||||
|
( full_state ? state : state[0] );
|
||||||
|
|
||||||
// Turtle state: state = [path, step_vector, default angle]
|
// Turtle state: state = [path, step_vector, default angle]
|
||||||
|
|
||||||
function turtle_command(command, parm, state, index) =
|
function turtle_command(command, parm, state, index) =
|
||||||
let(
|
let(
|
||||||
path = 0,
|
path = 0,
|
||||||
step=1,
|
step=1,
|
||||||
angle=2,
|
angle=2,
|
||||||
parm = !is_string(parm) ? parm : undef,
|
parm = !is_string(parm) ? parm : undef,
|
||||||
needvec = ["jump"],
|
needvec = ["jump"],
|
||||||
neednum = ["untilx","untily","xjump","yjump","angle","length","scale","addlength"],
|
neednum = ["untilx","untily","xjump","yjump","angle","length","scale","addlength"],
|
||||||
needeither = ["setdir"],
|
needeither = ["setdir"],
|
||||||
chvec = !in_list(command,needvec) || is_vector(parm),
|
chvec = !in_list(command,needvec) || is_vector(parm),
|
||||||
chnum = !in_list(command,neednum) || is_num(parm),
|
chnum = !in_list(command,neednum) || is_num(parm),
|
||||||
vec_or_num = !in_list(command,needeither) || (is_num(parm) || is_vector(parm)),
|
vec_or_num = !in_list(command,needeither) || (is_num(parm) || is_vector(parm)),
|
||||||
lastpt = select(state[path],-1)
|
lastpt = select(state[path],-1)
|
||||||
)
|
)
|
||||||
assert(chvec,str("\"",command,"\" requires a vector parameter at index ",index))
|
assert(chvec,str("\"",command,"\" requires a vector parameter at index ",index))
|
||||||
assert(chnum,str("\"",command,"\" requires a numeric parameter at index ",index))
|
assert(chnum,str("\"",command,"\" requires a numeric parameter at index ",index))
|
||||||
assert(vec_or_num,str("\"",command,"\" requires a vector or numeric parameter at index ",index))
|
assert(vec_or_num,str("\"",command,"\" requires a vector or numeric parameter at index ",index))
|
||||||
|
|
||||||
|
command=="move" ? list_set(state, path, concat(state[path],[default(parm,1)*state[step]+lastpt])) :
|
||||||
command=="move" ? list_set(state, path, concat(state[path],[default(parm,1)*state[step]+lastpt])):
|
command=="untilx" ? (
|
||||||
command=="untilx" ? let(
|
let(
|
||||||
int = line_intersection([lastpt,lastpt+state[step]], [[parm,0],[parm,1]]),
|
int = line_intersection([lastpt,lastpt+state[step]], [[parm,0],[parm,1]]),
|
||||||
xgood = sign(state[step].x) == sign(int.x-lastpt.x)
|
xgood = sign(state[step].x) == sign(int.x-lastpt.x)
|
||||||
)
|
)
|
||||||
assert(xgood,str("\"untilx\" never reaches desired goal at index ",index))
|
assert(xgood,str("\"untilx\" never reaches desired goal at index ",index))
|
||||||
list_set(state,path,concat(state[path],[int])):
|
list_set(state,path,concat(state[path],[int]))
|
||||||
command=="untily" ? let(
|
) :
|
||||||
int = line_intersection([lastpt,lastpt+state[step]], [[0,parm],[1,parm]]),
|
command=="untily" ? (
|
||||||
ffd=echo(int=int),
|
let(
|
||||||
ygood = is_def(int) && sign(state[step].y) == sign(int.y-lastpt.y)
|
int = line_intersection([lastpt,lastpt+state[step]], [[0,parm],[1,parm]]),
|
||||||
)
|
ffd=echo(int=int),
|
||||||
assert(ygood,str("\"untily\" never reaches desired goal at index ",index))
|
ygood = is_def(int) && sign(state[step].y) == sign(int.y-lastpt.y)
|
||||||
list_set(state,path,concat(state[path],[int])):
|
)
|
||||||
command=="xmove" ? list_set(state, path, concat(state[path],[default(parm,1)*norm(state[step])*[1,0]+lastpt])):
|
assert(ygood,str("\"untily\" never reaches desired goal at index ",index))
|
||||||
command=="ymove" ? list_set(state, path, concat(state[path],[default(parm,1)*norm(state[step])*[0,1]+lastpt])):
|
list_set(state,path,concat(state[path],[int]))
|
||||||
command=="jump" ? list_set(state, path, concat(state[path],[parm])):
|
) :
|
||||||
command=="xjump" ? list_set(state, path, concat(state[path],[[parm,lastpt.y]])):
|
command=="xmove" ? list_set(state, path, concat(state[path],[default(parm,1)*norm(state[step])*[1,0]+lastpt])):
|
||||||
command=="yjump" ? list_set(state, path, concat(state[path],[[lastpt.x,parm]])):
|
command=="ymove" ? list_set(state, path, concat(state[path],[default(parm,1)*norm(state[step])*[0,1]+lastpt])):
|
||||||
command=="turn" || command=="left" ? list_set(state, step, rot(default(parm,state[angle]),p=state[step],planar=true)) :
|
command=="jump" ? list_set(state, path, concat(state[path],[parm])):
|
||||||
command=="right" ? list_set(state, step, rot(-default(parm,state[angle]),p=state[step],planar=true)) :
|
command=="xjump" ? list_set(state, path, concat(state[path],[[parm,lastpt.y]])):
|
||||||
command=="angle" ? list_set(state, angle, parm) :
|
command=="yjump" ? list_set(state, path, concat(state[path],[[lastpt.x,parm]])):
|
||||||
command=="setdir" ? (
|
command=="turn" || command=="left" ? list_set(state, step, rot(default(parm,state[angle]),p=state[step],planar=true)) :
|
||||||
is_vector(parm) ? list_set(state, step, norm(state[step]) * normalize(parm))
|
command=="right" ? list_set(state, step, rot(-default(parm,state[angle]),p=state[step],planar=true)) :
|
||||||
: list_set(state, step, norm(state[step]) * [cos(parm),sin(parm)])
|
command=="angle" ? list_set(state, angle, parm) :
|
||||||
) :
|
command=="setdir" ? (
|
||||||
command=="length" ? list_set(state, step, parm*normalize(state[step])) :
|
is_vector(parm) ?
|
||||||
command=="scale" ? list_set(state, step, parm*state[step]) :
|
list_set(state, step, norm(state[step]) * normalize(parm)) :
|
||||||
command=="addlength" ? list_set(state, step, state[step]+normalize(state[step])*parm) :
|
list_set(state, step, norm(state[step]) * [cos(parm),sin(parm)])
|
||||||
assert(false,str("Unknown turtle command \"",command,"\" at index",index))
|
) :
|
||||||
[];
|
command=="length" ? list_set(state, step, parm*normalize(state[step])) :
|
||||||
|
command=="scale" ? list_set(state, step, parm*state[step]) :
|
||||||
|
command=="addlength" ? list_set(state, step, state[step]+normalize(state[step])*parm) :
|
||||||
|
assert(false,str("Unknown turtle command \"",command,"\" at index",index))
|
||||||
|
[];
|
||||||
|
|
||||||
|
|
||||||
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||||
|
|
Loading…
Reference in a new issue