Skip to content

Instantly share code, notes, and snippets.

@stephancom
Created April 14, 2022 09:00
Show Gist options
  • Save stephancom/0fa8d634918f5778826253337dbc43ea to your computer and use it in GitHub Desktop.
Save stephancom/0fa8d634918f5778826253337dbc43ea to your computer and use it in GitHub Desktop.
superegg
#
# superegg.rb
# Piet Hein's superegg
#
# ported from Python in 2020 by stephan.com
#
# original by Chris Wallace (kitwallace) 2013
# https://www.thingiverse.com/thing:48808
#
# https://en.wikipedia.org/wiki/Superegg
#
# ruby superegg.rb 20 2.5 0.75 > egg.scad
#
class Superegg
MAX = 100
def initialize(radius, exponent, ratio)
@radius = radius
@exponent = exponent
@ratio = ratio
end
def fx(num)
t = @radius**@exponent - num.abs**@exponent
return 0 unless t.positive?
(@ratio * t)**(1 / @exponent)
end
def shape
(MAX + 1).times.map do |i|
angle = (Math::PI * i / MAX)
y = @radius * Math.cos(angle)
x = fx(y)
[x, y]
end
end
def to_scad
puts <<-EOMODULE
module shape() {
translate([0, #{@radius}, 0]) polygon(#{shape});
}
EOMODULE
end
end
r = 20
e = 2.5
a = 0.75
r = ARGV[0].to_f unless ARGV[0].nil?
e = ARGV[1].to_f unless ARGV[1].nil?
a = ARGV[2].to_f unless ARGV[2].nil?
huevo = Superegg.new(r, e, a)
# huevo = Superegg.new(20, 2.5, 0.75)
puts huevo.to_scad
$fa=0.01;
$fs=0.5;
// radius of superegg in mm
radius = 22.8;
// exponent to raise to
exponent = 2.5;
// height
height = 27.4;
// ratio of radius/height
ratio = radius / height;
function oneeighty() = [0 : 180 * $fa : 180];
function tee(angle) = radius^exponent - abs(egg_y(angle))^exponent;
function egg_y(angle) = radius * cos(angle);
function egg_x(angle) = tee(angle) > 0 ? pow((ratio * tee(angle)), (1 / exponent)) : 0;
function point(angle) = [egg_x(angle), egg_y(angle)];
function superegg_points() = [ for(angle = oneeighty()) point(angle) ];
rotate_extrude() polygon( superegg_points() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment