diff --git a/src/turtle/_impl/_t2d_impl.scad b/src/turtle/_impl/_t2d_impl.scad index 4a2998bf..2ab39590 100644 --- a/src/turtle/_impl/_t2d_impl.scad +++ b/src/turtle/_impl/_t2d_impl.scad @@ -18,8 +18,8 @@ function _t2d_forward(t, leng) = function _t2d_turn(t, angle) = [_t2d_get_pt(t), _t2d_get_angle(t) + angle]; -function _t2d_get_x(t) = t[0][0]; -function _t2d_get_y(t) = t[0][1]; +function _t2d_get_x(t) = t[0].x; +function _t2d_get_y(t) = t[0].y; function _t2d_get_pt(t) = t[0]; function _t2d_get_angle(t) = t[1]; diff --git a/src/turtle/_impl/_t3d_impl.scad b/src/turtle/_impl/_t3d_impl.scad index 028a13ae..4d48ea6c 100644 --- a/src/turtle/_impl/_t3d_impl.scad +++ b/src/turtle/_impl/_t3d_impl.scad @@ -1,111 +1,86 @@ -function _t3d_x(pt) = pt[0]; -function _t3d_y(pt) = pt[1]; -function _t3d_z(pt) = pt[2]; - -function _t3d_pt3D(x, y, z) = [x, y, z]; +use <../../matrix/m_rotation.scad>; function _t3d_create(pt, unit_vts) = [ - is_undef(pt) ? _t3d_pt3D(0, 0, 0) : pt, - is_undef(unit_vts) ? [_t3d_pt3D(1, 0, 0), _t3d_pt3D(0, 1, 0), _t3d_pt3D(0, 0, 1)] : unit_vts + is_undef(pt) ? [0, 0, 0] : pt, + is_undef(unit_vts) ? [[1, 0, 0], [0, 1, 0], [0, 0, 1]] : unit_vts ]; -function _t3d_plus(pt, n) = - _t3d_pt3D(_t3d_x(pt) + n, _t3d_y(pt) + n, _t3d_z(pt) + n); -function _t3d_minus(pt, n) = - _t3d_pt3D(_t3d_x(pt) - n, _t3d_y(pt) - n, _t3d_z(pt) + n); -function _t3d_mlt(pt, n) = - _t3d_pt3D(_t3d_x(pt) * n, _t3d_y(pt) * n, _t3d_z(pt) * n); -function _t3d_div(pt, n) = - _t3d_pt3D(_t3d_x(pt) / n, _t3d_y(pt) / n, _t3d_z(pt) / n); -function _t3d_neg(pt, n) = - _t3d_mlt(pt, -1); - -function _t3d_ptPlus(pt1, pt2) = - _t3d_pt3D( - _t3d_x(pt1) + _t3d_x(pt2), - _t3d_y(pt1) + _t3d_y(pt2), - _t3d_z(pt1) + _t3d_z(pt2) - ); - function _t3d_pt(turtle) = turtle[0]; function _t3d_unit_vts(turtle) = turtle[1]; // forward the turtle in the x' direction function _t3d_xu_forward(turtle, leng) = _t3d_create( - _t3d_ptPlus(_t3d_pt(turtle), _t3d_mlt(_t3d_unit_vts(turtle)[0], leng)), + _t3d_pt(turtle) + _t3d_unit_vts(turtle).x * leng, _t3d_unit_vts(turtle) ); // forward the turtle in the y' direction function _t3d_yu_forward(turtle, leng) = _t3d_create( - _t3d_ptPlus(_t3d_pt(turtle), _t3d_mlt(_t3d_unit_vts(turtle)[1], leng)), + _t3d_pt(turtle) + _t3d_unit_vts(turtle).y * leng, _t3d_unit_vts(turtle) ); // forward the turtle in the z' direction function _t3d_zu_forward(turtle, leng) = _t3d_create( - _t3d_ptPlus( - _t3d_pt(turtle), - _t3d_mlt(_t3d_unit_vts(turtle)[2], leng) - ), + _t3d_pt(turtle) + _t3d_unit_vts(turtle).z * leng, _t3d_unit_vts(turtle) ); // turn the turtle around the x'-axis // return a new unit vector function _t3d_xu_turn(turtle, a) = - let(cosa = cos(a), sina = sin(a)) + let( + unit_vts = _t3d_unit_vts(turtle), + xu = unit_vts.x, + m = m_rotation(a, xu), + nyu = m * [each unit_vts.y, 1], + nzu = m * [each unit_vts.z, 1] + ) _t3d_create( _t3d_pt(turtle), [ - _t3d_unit_vts(turtle)[0], - _t3d_ptPlus( - _t3d_mlt(_t3d_unit_vts(turtle)[1], cosa), - _t3d_mlt(_t3d_unit_vts(turtle)[2], sina) - ), - _t3d_ptPlus( - _t3d_mlt(_t3d_neg(_t3d_unit_vts(turtle)[1]), sina), - _t3d_mlt(_t3d_unit_vts(turtle)[2], cosa) - ) + xu, + [nyu.x, nyu.y, nyu.z], + [nzu.x, nzu.y, nzu.z] ] ); // turn the turtle around the y'-axis // return a new unit vector function _t3d_yu_turn(turtle, a) = - let(cosa = cos(a), sina = sin(a)) + let( + unit_vts = _t3d_unit_vts(turtle), + yu = unit_vts.y, + m = m_rotation(a, yu), + nxu = m * [each unit_vts.x, 1], + nzu = m * [each unit_vts.z, 1] + ) _t3d_create( _t3d_pt(turtle), [ - _t3d_ptPlus( - _t3d_mlt(_t3d_unit_vts(turtle)[0], cosa), - _t3d_mlt(_t3d_neg(_t3d_unit_vts(turtle)[2]), sina) - ), - _t3d_unit_vts(turtle)[1], - _t3d_ptPlus( - _t3d_mlt(_t3d_unit_vts(turtle)[0], sina), - _t3d_mlt(_t3d_unit_vts(turtle)[2], cosa) - ) + [nxu.x, nxu.y, nxu.z], + yu, + [nzu.x, nzu.y, nzu.z] ] ); // turn the turtle around the z'-axis // return a new unit vector function _t3d_zu_turn(turtle, a) = - let(cosa = cos(a), sina = sin(a)) + let( + unit_vts = _t3d_unit_vts(turtle), + zu = unit_vts.z, + m = m_rotation(a, zu), + nxu = m * [each unit_vts.x, 1], + nyu = m * [each unit_vts.y, 1] + ) _t3d_create( _t3d_pt(turtle), [ - _t3d_ptPlus( - _t3d_mlt(_t3d_unit_vts(turtle)[0], cosa), - _t3d_mlt(_t3d_unit_vts(turtle)[1], sina) - ), - _t3d_ptPlus( - _t3d_mlt(_t3d_neg(_t3d_unit_vts(turtle)[0]), sina), - _t3d_mlt(_t3d_unit_vts(turtle)[1], cosa) - ), - _t3d_unit_vts(turtle)[2], + [nxu.x, nxu.y, nxu.z], + [nyu.x, nyu.y, nyu.z], + zu ] ); diff --git a/src/turtle/_impl/_turtle2d_impl.scad b/src/turtle/_impl/_turtle2d_impl.scad index db5dc6b2..28bfc837 100644 --- a/src/turtle/_impl/_turtle2d_impl.scad +++ b/src/turtle/_impl/_turtle2d_impl.scad @@ -15,8 +15,8 @@ function _turtle2d_forward(turtle, leng) = function _turtle2d_turn(turtle, angle) = [_turtle2d_get_pt(turtle), _turtle2d_get_angle(turtle) + angle]; -function _turtle2d_get_x(turtle) = turtle[0][0]; -function _turtle2d_get_y(turtle) = turtle[0][1]; +function _turtle2d_get_x(turtle) = turtle[0].x; +function _turtle2d_get_y(turtle) = turtle[0].y; function _turtle2d_get_pt(turtle) = turtle[0]; function _turtle2d_get_angle(turtle) = turtle[1]; diff --git a/src/turtle/_impl/_turtle3d_impl.scad b/src/turtle/_impl/_turtle3d_impl.scad index c136a900..c997e5a8 100644 --- a/src/turtle/_impl/_turtle3d_impl.scad +++ b/src/turtle/_impl/_turtle3d_impl.scad @@ -1,113 +1,87 @@ -function _turtle3d_x(pt) = pt[0]; -function _turtle3d_y(pt) = pt[1]; -function _turtle3d_z(pt) = pt[2]; - -function _turtle3d_pt3D(x, y, z) = [x, y, z]; +use <../../matrix/m_rotation.scad>; function _turtle3d_create(pt, unit_vts) = [pt, unit_vts]; function _turtle3d_create_default() = _turtle3d_create( - _turtle3d_pt3D(0, 0, 0), + [0, 0, 0], // unit vectors from the turtle's viewpoint - [_turtle3d_pt3D(1, 0, 0), _turtle3d_pt3D(0, 1, 0), _turtle3d_pt3D(0, 0, 1)] + [[1, 0, 0], [0, 1, 0], [0, 0, 1]] ); -function _turtle3d_plus(pt, n) = - _turtle3d_pt3D(_turtle3d_x(pt) + n, _turtle3d_y(pt) + n, _turtle3d_z(pt) + n); -function _turtle3d_minus(pt, n) = - _turtle3d_pt3D(_turtle3d_x(pt) - n, _turtle3d_y(pt) - n, _turtle3d_z(pt) + n); -function _turtle3d_mlt(pt, n) = - _turtle3d_pt3D(_turtle3d_x(pt) * n, _turtle3d_y(pt) * n, _turtle3d_z(pt) * n); -function _turtle3d_div(pt, n) = - _turtle3d_pt3D(_turtle3d_x(pt) / n, _turtle3d_y(pt) / n, _turtle3d_z(pt) / n); -function _turtle3d_neg(pt, n) = - _turtle3d_mlt(pt, -1); - -function _turtle3d_ptPlus(pt1, pt2) = - _turtle3d_pt3D( - _turtle3d_x(pt1) + _turtle3d_x(pt2), - _turtle3d_y(pt1) + _turtle3d_y(pt2), - _turtle3d_z(pt1) + _turtle3d_z(pt2) - ); - - function _turtle3d_pt(turtle) = turtle[0]; function _turtle3d_unit_vts(turtle) = turtle[1]; // forward the turtle in the x' direction function _turtle3d_xu_move(turtle, leng) = _turtle3d_create( - _turtle3d_ptPlus(_turtle3d_pt(turtle), _turtle3d_mlt(_turtle3d_unit_vts(turtle)[0], leng)), + _turtle3d_pt(turtle) + _turtle3d_unit_vts(turtle).x * leng, _turtle3d_unit_vts(turtle) ); // forward the turtle in the y' direction function _turtle3d_yu_move(turtle, leng) = _turtle3d_create( - _turtle3d_ptPlus(_turtle3d_pt(turtle), _turtle3d_mlt(_turtle3d_unit_vts(turtle)[1], leng)), + _turtle3d_pt(turtle) + _turtle3d_unit_vts(turtle).y * leng, _turtle3d_unit_vts(turtle) ); // forward the turtle in the z' direction function _turtle3d_zu_move(turtle, leng) = _turtle3d_create( - _turtle3d_ptPlus( - _turtle3d_pt(turtle), - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[2], leng) - ), + _turtle3d_pt(turtle) + _turtle3d_unit_vts(turtle).z * leng, _turtle3d_unit_vts(turtle) ); // turn the turtle around the x'-axis // return a new unit vector function _turtle3d_xu_turn(turtle, a) = - let(cosa = cos(a), sina = sin(a)) + let( + unit_vts = _turtle3d_unit_vts(turtle), + xu = unit_vts.x, + m = m_rotation(a, xu), + nyu = m * [each unit_vts.y, 1], + nzu = m * [each unit_vts.z, 1] + ) _turtle3d_create( _turtle3d_pt(turtle), [ - _turtle3d_unit_vts(turtle)[0], - _turtle3d_ptPlus( - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[1], cosa), - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[2], sina) - ), - _turtle3d_ptPlus( - _turtle3d_mlt(_turtle3d_neg(_turtle3d_unit_vts(turtle)[1]), sina), - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[2], cosa) - ) + xu, + [nyu.x, nyu.y, nyu.z], + [nzu.x, nzu.y, nzu.z] ] ); // turn the turtle around the y'-axis // return a new unit vector function _turtle3d_yu_turn(turtle, a) = - let(cosa = cos(a), sina = sin(a)) + let( + unit_vts = _turtle3d_unit_vts(turtle), + yu = unit_vts.y, + m = m_rotation(a, yu), + nxu = m * [each unit_vts.x, 1], + nzu = m * [each unit_vts.z, 1] + ) _turtle3d_create( _turtle3d_pt(turtle), [ - _turtle3d_ptPlus( - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[0], cosa), - _turtle3d_mlt(_turtle3d_neg(_turtle3d_unit_vts(turtle)[2]), sina) - ), - _turtle3d_unit_vts(turtle)[1], - _turtle3d_ptPlus( - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[0], sina), - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[2], cosa) - ) + [nxu.x, nxu.y, nxu.z], + yu, + [nzu.x, nzu.y, nzu.z] ] ); // turn the turtle around the z'-axis // return a new unit vector function _turtle3d_zu_turn(turtle, a) = - let(cosa = cos(a), sina = sin(a)) + let( + unit_vts = _turtle3d_unit_vts(turtle), + zu = unit_vts.z, + m = m_rotation(a, zu), + nxu = m * [each unit_vts.x, 1], + nyu = m * [each unit_vts.y, 1] + ) _turtle3d_create( _turtle3d_pt(turtle), [ - _turtle3d_ptPlus( - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[0], cosa), - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[1], sina) - ), - _turtle3d_ptPlus( - _turtle3d_mlt(_turtle3d_neg(_turtle3d_unit_vts(turtle)[0]), sina), - _turtle3d_mlt(_turtle3d_unit_vts(turtle)[1], cosa) - ), - _turtle3d_unit_vts(turtle)[2], + [nxu.x, nxu.y, nxu.z], + [nyu.x, nyu.y, nyu.z], + zu ] );