All pastes #623915 Raw Edit

Someone

public ruby v1 · immutable
·published 2007-07-17 22:41 UTC
rendered paste body
require "RUDL"include RUDLinclude Constantrequire 'complex'# koh's curve drawer that doesn't use recursive functions# written by Aur Saraf. Submitted to Public Domain.## internally uses a coordinate system where all coords are pairs of values in the range -1..1# converts to screen coords before drawing to screenwidth = 640height = 640iterations = 5# used to rotatesin300 = Math::sin(300.0/180.0*Math::PI)cos300 = Math::cos(300.0/180.0*Math::PI)sin120 = Math::sin(120.0/180.0*Math::PI)cos120 = Math::cos(120.0/180.0*Math::PI)a = []#~ # find coords of a triangle with equal angles.#~ x = [1, 0.0]#~ 3.times{|i|  #~ a << x  #~ x = [cos120*x[0]-sin120*x[1], sin120*x[0]+cos120*x[1]] # rotate by 120 deg#~ }a << [-1, 0] << [1,0]# performance tweakb = p1 = p2 = v = niliterations.times{|t|  b = []  a.each_index{|i| # in ruby a[-1] is the last value in the array    # find point dividing segment by a 1:3 ratio    p1 = [(2*a[i-1][0]+a[i][0])/3.0, (2*a[i-1][1]+a[i][1])/3.0]    # find point dividing segment by a 2:3 ratio    p2 = [(a[i-1][0]+2*a[i][0])/3.0, (a[i-1][1]+2*a[i][1])/3.0]    # find vector p1p2    v = [p2[0]-p1[0], p2[1]-p1[1]]    # rotate v by 60 deg ccw    v = [cos300*v[0]-sin300*v[1], sin300*v[0]+cos300*v[1]]    # add p1    b << p1    # add point p1+v    b << [p1[0]+v[0], p1[1]+v[1]]    # add p2    b << p2    # add a[i]    b << a[i]  }  a = b}#convert points in a[] to screen coordinatesa.collect!{|x|  [((x[0]+1)*width/2).to_i, ((x[1]+1)*height/2).to_i]}# draw a[]'s contents:# open a windoww = DisplaySurface.new([width, height])# draw a[]'s contents as a poligonw.polygon(a, [255, 255, 255])#update screenw.flip# wait for input and closeloop do    ev = EventQueue.wait    break if ev.is_a? QuitEvent    break if ev.is_a? KeyDownEventend