Skip to content

Instantly share code, notes, and snippets.

@markburns
Created December 22, 2010 08:01
Show Gist options
  • Save markburns/751252 to your computer and use it in GitHub Desktop.
Save markburns/751252 to your computer and use it in GitHub Desktop.
Koch Snowflake area calculator
ruby-1.9.2-p0 > require './koch_snowflake'
=> true
ruby-1.9.2-p0 > k=KochSnowflake.new 5,514
=> #<KochSnowflake:0x97222e4 @iteration=514, @size=5>
ruby-1.9.2-p0 > k.area
=> NaN
ruby-1.9.2-p0 > k=KochSnowflake.new 5,513
=> #<KochSnowflake:0x977cc80 @iteration=513, @size=5>
ruby-1.9.2-p0 > k.area
=> 16.168975852600433
class EquilateralTriangle
def initialize side
@length_of_side = side
end
def angle
2* Math::PI / 6.0
end
def height
Math.atan(angle) * @length_of_side / 2.0
end
def area
@length_of_side * height
end
end
require './equilateral_triangle'
describe EquilateralTriangle do
before {@triangle = EquilateralTriangle.new 10}
it "should have a correct angle" do
@triangle.angle.should == Math::PI * 2 / 6.0
end
it "should have a correct height" do
@triangle.height.should == Math.atan(@triangle.angle) * 5
end
it "should have a correct area" do
@triangle.area.should == @triangle.height * 10
end
end
require './equilateral_triangle'
class KochSnowflake
def initialize size, iteration
@iteration=iteration
@size=size
end
def self.extra_triangles iteration
#0 1 2 3
#0 3 12 48
case iteration
when 0 then return 0
when 1 then return 3
else
return KochSnowflake.extra_triangles(iteration-1)*4
end
end
def area
area = 0
@iteration.times do |i|
if i==0
area += EquilateralTriangle.new(@size).area
else
triangles = KochSnowflake.extra_triangles(i)
area += EquilateralTriangle.new(@size/3.0**i).area * triangles
end
end
area
end
end
require './koch_snowflake'
describe KochSnowflake do
context "in general" do
context "calculating number extra triangles to add for" do
it "1st iteration" do
KochSnowflake.extra_triangles(0).should == 0 # 0 * 3
end
it "2nd iteration" do
KochSnowflake.extra_triangles(1).should == 3 #= 1*3 = 2^0*3
end
it "3rd iteration" do
KochSnowflake.extra_triangles(2).should == 12 #= 4*3 = 2^2*3
end
it "4th iteration" do
KochSnowflake.extra_triangles(3).should == 48 #= 16*3 = 2^4*3
end
it "5th iteration" do
KochSnowflake.extra_triangles(4).should == 32*6 # = 64*3 = 2^8*3
end
end
end
context "with an iteration of 1" do
let(:koch) { KochSnowflake.new 10,1 }
it "has an area equal to the triangle" do
koch.area.should == EquilateralTriangle.new(10).area
end
end
context "iteration of 2" do
let(:koch) { KochSnowflake.new 10,2 }
it "has an area of 1 triangle plus 3 triangles with a 3rd of the base" do
large = EquilateralTriangle.new(10.0).area
small = EquilateralTriangle.new(10.0/3.0).area
koch.area.should == large + (small * 3.0)
end
end
context "with an iteration of 3" do
let(:koch) { KochSnowflake.new 10,3 }
it "has the correct area" do
large = EquilateralTriangle.new(10.0).area
medium = EquilateralTriangle.new(10.0/3.0).area
small = EquilateralTriangle.new(10.0/9.0).area
koch.area.should == large + (medium * 3.0) + (small * 12.0)
end
end
context "with a interation of 10" do
let(:koch) { KochSnowflake.new 10,4 }
it "has the correct area" do
large = EquilateralTriangle.new(10.0).area
medium = EquilateralTriangle.new(10.0/3.0).area
small = EquilateralTriangle.new(10.0/9.0).area
tiny = EquilateralTriangle.new(10.0/27.0).area
#0 1 2 3
#0 3 12 48
koch.area.should == large + (medium * 3.0) + (small * 12.0) + (tiny * 48.0)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment