Created
April 18, 2022 06:58
-
-
Save stephancom/00346b938abd7d316e2ab916583541db to your computer and use it in GitHub Desktop.
superegg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# superegg.rb | |
# Piet Hein's superegg | |
# | |
# ported from Python in 2022 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$fa=0.01; | |
$fs=0.5; | |
// radius of superegg in mm | |
radius = 22.8; | |
// exponent to raise to | |
exponent = 2.5; | |
// ratio of radius/height | |
ratio = 0.75; | |
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