/ examples / maze / maze_tower.scad
maze_tower.scad
  1  use <maze/mz_theta.scad>
  2  use <util/find_index.scad>
  3  use <arc.scad>
  4  use <polyline2d.scad>
  5  
  6  $fn = 48;
  7  
  8  rings = 5;
  9  beginning_number = 5;
 10  cell_width = 2;
 11  
 12  maze_tower();
 13  
 14  module maze_tower() {
 15  	wall_thickness = cell_width / 2;
 16  	wall_height = cell_width * 0.75;
 17      half_wall_thickness = wall_thickness / 2;
 18  
 19  	NO_WALL = 0;           
 20  	INWARD_WALL = 1;      
 21  	CCW_WALL = 2;         
 22  	INWARD_CCW_WALL = 3;   
 23  
 24  	function vt_from_angle(theta, r) = [r * cos(theta), r * sin(theta)];
 25  
 26  	mz = mz_theta(rings, beginning_number);
 27  	mz_leng = len(mz);
 28  	outThetaStep = 360 / len(mz[rings - 1]);
 29  	r = cell_width * (rings + 1);
 30  
 31  	module maze() {
 32  		for(rings = mz, cell = rings) {
 33  			ri = cell[0];
 34  			ci = cell[1];
 35  			type = cell[2];
 36  			thetaStep = 360 / len(mz[ri]);
 37  			innerR = (ri + 1) * cell_width;
 38  			outerR = (ri + 2) * cell_width;
 39  			theta1 = thetaStep * ci;
 40  			theta2 = thetaStep * (ci + 1);
 41  
 42  			innerVt2 = vt_from_angle(theta2, innerR);
 43  			outerVt2 = vt_from_angle(theta2, outerR);
 44  
 45  			if(type == INWARD_WALL || type == INWARD_CCW_WALL) {
 46  				if(!(ri == 0 && ci == 0)) {
 47  					arc(innerR, [theta1, theta2], wall_thickness);
 48  				}
 49  			}
 50  
 51  			if(type == CCW_WALL || type == INWARD_CCW_WALL) {
 52  				intersection() {
 53  					difference() {
 54  						circle(outerR + half_wall_thickness);
 55  						circle(innerR - half_wall_thickness);
 56  					}
 57  					polyline2d([innerVt2 * 0.9, outerVt2], width = wall_thickness);
 58  				}
 59  			}
 60  		} 
 61  
 62  		arc(r, 360, wall_thickness);
 63  	}
 64  
 65  	module maze_floors() {
 66  		difference() {
 67  			union() {
 68  				for(i = [0:mz_leng - 1]) {
 69  					rings = mz[i];
 70  					ir = (rings[i][0] + 1) * cell_width;
 71  					linear_extrude((mz_leng - i + 1) * wall_height)
 72  						circle(ir + wall_thickness * 0.4999);
 73  				}
 74  				linear_extrude(wall_height)
 75  					circle(r + wall_thickness * 0.4999);
 76  			}
 77  			translate([0, 0, -0.1])
 78  			linear_extrude(wall_height * (mz_leng + 2), convexity = 10) 
 79  			    maze();
 80  		}
 81  		last_rings = mz[mz_leng - 1];
 82  		i = find_index(last_rings, function(cell) cell[2] == CCW_WALL || cell[2] == INWARD_CCW_WALL);
 83  		theta1 = outThetaStep * last_rings[i][1];
 84  		linear_extrude(wall_height)
 85  			arc(r * 0.9999, [theta1 + outThetaStep * 0.1, theta1 + outThetaStep * 0.75], wall_thickness);
 86  	}
 87  
 88  	module d_stairs() {
 89  		num_stairs = 4;
 90  		stair_thickness = wall_thickness / 3;
 91  		or = r + half_wall_thickness;
 92  		for(ri = [0:2:rings * 2], si = [0:2]) {
 93  			r = or - stair_thickness * si - wall_thickness * ri;
 94  			translate([0, 0, wall_height * ri / 2 + wall_height / num_stairs * (si + 1)])
 95  			linear_extrude(wall_height / num_stairs * (3 - si))
 96  				arc(r, 360, stair_thickness, width_mode = "LINE_INWARD");
 97  		}
 98  	}
 99  
100  	difference() {
101          maze_floors();
102          d_stairs();
103  	}
104  }