Skip to content

Instantly share code, notes, and snippets.

@ochafik
Created December 25, 2024 12:52
Show Gist options
  • Select an option

  • Save ochafik/9579e8005ae28ccfda09ec41cc2e1b23 to your computer and use it in GitHub Desktop.

Select an option

Save ochafik/9579e8005ae28ccfda09ec41cc2e1b23 to your computer and use it in GitHub Desktop.
Prototype Ruler Axes For OpenScad Web Playground
// https://github.com/openscad/openscad-playground/issues/72
include <BOSL2/std.scad>
min_pt=[-400, -20, -2];
max_pt=[700, 30, 20];
steps = [
[
["unit", 0.001],
["milestones", 0.1],
["min", -0.25],
["max", 0.25],
],
[
["unit", 0.1],
["milestones", 0.2],
["min", -0.5],
["max", 0.5],
],
[
["unit", 1],
["milestones", 1],
["min", -1],
["max", 1],
],
[
["unit", 1],
["milestones", 2],
["min", -3],
["max", 3],
],
[
["unit", 1],
["milestones", 2],
["min", -3],
["max", 3],
],
[
["unit", 1],
["milestones", 10],
["min", -10],
["max", 10],
],
[
["unit", 10],
["milestones", 20],
["min", -30],
["max", 30],
],
[
["unit", 10],
["milestones", 100],
["min", -100],
["max", 100],
],
[
["unit", 100],
["milestones", 200],
["min", -300],
["max", 300],
],
[
["unit", 100],
["milestones", 1000],
["min", -1000],
["max", 1000]
]
];
function nth_dim(i) =
let(step=steps[i],
min_v=struct_val(step, "min"),
max_v=struct_val(step, "max"),
dim=max_v-min_v)
dim;
min_dimensions = nth_dim(0);
max_dimensions = nth_dim(len(steps)-1);
function find_step(dimension, i=0) =
i < len(steps) - 1
? (let(step=steps[i],
min_v=struct_val(step, "min"),
max_v=struct_val(step, "max"),
dim=max_v-min_v)
dimension < dim ? step : find_step(dimension, i+1))
: steps[len(steps)-1];
//unit = 1; // [0.1, 1, 10, 100, 1000]
$t=0;
//time=$t;
time=0;
//dimension = $time * max_dimensions;
dimension = 30;
step = find_step(dimension);
//echo(max_dimensions=max_dimensions);
unit=struct_val(step, "unit");
milestones=struct_val(step, "milestones");
min_v=struct_val(step, "min");
max_v=struct_val(step, "max");
dim = max_v-min_v;
//dim=max_v-min_v
scale = 1;//min_dimensions;
//scale = lerp(time, min_dimensions, max_dimensions);
//scale = 1; // [1:100];
//unit = scale < 5 ? 1 : scale < 50 ? 10 : scale < 500 ? 100 : 1000; // [0.1, 1, 10, 100, 1000]
width=0.05;
thickness=width;//*min_dimensions/scale;
//next_unit=10*unit;
echo(time=time, step=step, scale=scale, thickness=thickness, min_dimensions, max_dimensions, dim);
module axis(dir=1, text_rot=0) {
echo(scale=scale, unit=unit, min_v=min_v, max_v=max_v);
hull() {
translate([min_v, 0, 0]) sphere(d=thickness);
translate([max_v, 0, 0]) sphere(d=thickness);
}
for (t=[0:unit:max_v]) {
//isMilestone = t != 0 && floor(t / next_unit) * next_unit == t;
nMilestones = t / milestones;
isMilestone = t != 0 && floor(nMilestones) * milestones == nMilestones;
if (isMilestone)
translate([t, dir * (scale/2 - width), 0])
rotate([0, 0, text_rot])
linear_extrude(width)
text(str(t), size=scale*dim*0.1, valign="center", halign="center");
tickSize = isMilestone ? 1.33*scale : scale*0.66;
echo($t=$t, t=t, tickSize=tickSize, nMilestones=nMilestones, isMilestone=isMilestone);
hull() {
translate([t, 0, 0]) sphere(d=thickness);
translate([t, -dir * tickSize, 0]) sphere(d=thickness);
}
}
}
//scale([1/scale, 1/scale, 1/scale])
//
////color("black")
color([1,0,0,0.2])
{
axis();
rotate([0, 0, 90])
axis(dir=-1, text_rot=0);//180);
rotate([90, -90, 0])
axis(dir=-1, text_rot=0);//180);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment