Browse Source

Initial commit

main
Peter 1 year ago
commit
417caae273
4 changed files with 89 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +11
    -0
      LICENSE.txt
  3. +33
    -0
      README.md
  4. +43
    -0
      scad-gen.rb

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
*.scad
*.stl

+ 11
- 0
LICENSE.txt View File

@@ -0,0 +1,11 @@
Copyright 2019 Peter Ludikovsky

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 33
- 0
README.md View File

@@ -0,0 +1,33 @@
scad-gen
========

Generate [OpenSCAD](https://openscad.org/) files for visualizations of
arbitrary functions.

WHY?!
-----

Why not?

I wanted to be able to generate 3D models of various mathematical
functions after seeing [this
Tweet](https://twitter.com/nwilliams030/status/1112516582662721537) by
[\@nwilliams030](https://twitter.com/nwilliams030)

How?
----

This is one simple Ruby script. At the top are the only things you need
to edit:

- `t`: the starting input value for your function (*f*(0))
- `step`: the stepping between function values
- `tmax`: the end value
- `calc(t)`: your function, expected to return an array with (x,y,z)

The main loop iterates from `t` to `tmax` at `step` increments,
calculates the function `calc` at each step, and draws cylinders between
the points, with a sphere at each point (for a smoother appearance).

After setting it all up, run the script, sending the output to a target
file. Open in OpenSCAD, render, voilà.

+ 43
- 0
scad-gen.rb View File

@@ -0,0 +1,43 @@
#!/usr/bin/env ruby

t = 0
step = Math::PI / 10
tmax = 2 * Math::PI

def calc(t)[10 * Math.sin(t), 0, 10 * Math.cos(t)]
end

old_vec = calc(0)

iter = 0;

puts <<HEAD
$fn=100;

union() {
translate([#{old_vec.join(',')}])
sphere(d=1);

HEAD

while t < tmax do
t += step
new_vec = calc(t)
puts <<MAIN
length#{iter} = norm([#{new_vec[0] - old_vec[0]}, #{new_vec[1] - old_vec[1]}, #{new_vec[2] - old_vec[2]}]);
b#{iter} = acos(#{new_vec[2] - old_vec[2]} / length#{iter});
c#{iter} = atan2(#{new_vec[1] - old_vec[1]}, #{new_vec[0] - old_vec[0]});
translate([#{new_vec.join(', ')}])
sphere(d=1);
translate([#{old_vec.join(', ')}])
rotate([0, b#{iter}, c#{iter}])
cylinder(d=1, h=length#{iter});

MAIN
iter += 1
old_vec = new_vec
end

puts <<TAIL
}
TAIL

Loading…
Cancel
Save