Skip to content

Instantly share code, notes, and snippets.

@RKushnir
Created February 12, 2012 15:53
Show Gist options
  • Save RKushnir/1809173 to your computer and use it in GitHub Desktop.
Save RKushnir/1809173 to your computer and use it in GitHub Desktop.

Revisions

  1. RKushnir revised this gist Feb 12, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,9 @@ def draw
    unless @canvas
    diameter = 2 * @radius
    @canvas = Array.new(diameter + 1){ ' ' * (2 * (diameter + 1)) }
    key_points.each do |x, y|
    [x, diameter - x].product([y, diameter - y]).each do |p|
    @canvas[p[0]][2 * p[1]] = @canvas[p[1]][2 * p[0]] = '*'
    key_points.each do |kx, ky|
    [kx, diameter - kx].product([ky, diameter - ky]).each do |x, y|
    @canvas[x][2 * y] = @canvas[y][2 * x] = '*'
    end
    end
    end
  2. RKushnir revised this gist Feb 12, 2012. 1 changed file with 10 additions and 19 deletions.
    29 changes: 10 additions & 19 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -5,25 +5,23 @@ def initialize(radius)

    def draw
    unless @canvas
    @canvas = Array.new(diameter + 1){ Array.new(2 * (diameter + 1)){ ' ' } }
    key_points.each {|p| set_symmetric_points(*p) }
    diameter = 2 * @radius
    @canvas = Array.new(diameter + 1){ ' ' * (2 * (diameter + 1)) }
    key_points.each do |x, y|
    [x, diameter - x].product([y, diameter - y]).each do |p|
    @canvas[p[0]][2 * p[1]] = @canvas[p[1]][2 * p[0]] = '*'
    end
    end
    end

    puts @canvas.map(&:join).join "\n"
    puts @canvas.join "\n"
    end

    private

    def key_points
    unless @key_points
    q = (1 - 0.5 * (2 ** 0.5)) * @radius # (1 - cos45) * radius
    @key_points = [*q.floor..@radius].product([*0..q.ceil]).select{|x, y| lies_on_circle? x, y }
    end
    @key_points
    end

    def diameter
    @diameter ||= 2 * @radius
    q = (1 - 0.5 * (2 ** 0.5)) * @radius # (1 - cos45) * radius
    [*q.floor..@radius].product([*0..q.ceil]).select{|x, y| lies_on_circle? x, y }
    end

    # Detects if point is closest to the circle edge by checking the devitaion of points above and below
    @@ -32,13 +30,6 @@ def lies_on_circle?(x, y)
    dx2, r2 = (x - cx) ** 2, @radius ** 2
    [y - 1, y, y + 1].min_by {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 } == y
    end

    # Sets 8 points symmetrically to circle center
    def set_symmetric_points(x, y)
    [x, diameter - x].product([y, diameter - y]).each do |p|
    @canvas[p[0]][2 * p[1]] = @canvas[p[1]][2 * p[0]] = '*'
    end
    end
    end

    Circle.new(ARGV[0].to_i).draw
  3. RKushnir revised this gist Feb 12, 2012. 1 changed file with 18 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion circle.rb
    Original file line number Diff line number Diff line change
    @@ -41,4 +41,21 @@ def set_symmetric_points(x, y)
    end
    end

    Circle.new(ARGV[0].to_i).draw
    Circle.new(ARGV[0].to_i).draw

    # ruby circle.rb 7
    # * * * * *
    # * * * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * * * *
    # * * * * *
  4. RKushnir revised this gist Feb 12, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion circle.rb
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ def diameter
    end

    # Detects if point is closest to the circle edge by checking the devitaion of points above and below
    def lies_on_circle?(x ,y)
    def lies_on_circle?(x, y)
    cx, cy = @radius, @radius # central point
    dx2, r2 = (x - cx) ** 2, @radius ** 2
    [y - 1, y, y + 1].min_by {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 } == y
  5. RKushnir revised this gist Feb 12, 2012. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,7 @@ def initialize(radius)
    def draw
    unless @canvas
    @canvas = Array.new(diameter + 1){ Array.new(2 * (diameter + 1)){ ' ' } }
    key_points.each do |p|
    set_symmetric_points(p[0], p[1])
    end
    key_points.each {|p| set_symmetric_points(*p) }
    end

    puts @canvas.map(&:join).join "\n"
  6. RKushnir revised this gist Feb 12, 2012. 1 changed file with 10 additions and 21 deletions.
    31 changes: 10 additions & 21 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,12 @@
    class Circle
    def initialize(radius)
    @radius = radius
    @points = [*0..radius].product([*0..radius / 2]).select{|x, y| lies_on_circle? x, y }
    end

    def draw
    unless @canvas
    @canvas = Array.new(diameter + 1){ Array.new(2 * (diameter + 1)){ ' ' } }
    @points.each do |p|
    key_points.each do |p|
    set_symmetric_points(p[0], p[1])
    end
    end
    @@ -17,6 +16,14 @@ def draw

    private

    def key_points
    unless @key_points
    q = (1 - 0.5 * (2 ** 0.5)) * @radius # (1 - cos45) * radius
    @key_points = [*q.floor..@radius].product([*0..q.ceil]).select{|x, y| lies_on_circle? x, y }
    end
    @key_points
    end

    def diameter
    @diameter ||= 2 * @radius
    end
    @@ -36,22 +43,4 @@ def set_symmetric_points(x, y)
    end
    end

    Circle.new(ARGV[0].to_i).draw


    #> ruby circle.rb 7
    # * * * * *
    # * * * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * * * *
    # * * * * *
    Circle.new(ARGV[0].to_i).draw
  7. RKushnir revised this gist Feb 12, 2012. 1 changed file with 19 additions and 1 deletion.
    20 changes: 19 additions & 1 deletion circle.rb
    Original file line number Diff line number Diff line change
    @@ -36,4 +36,22 @@ def set_symmetric_points(x, y)
    end
    end

    Circle.new(15).draw
    Circle.new(ARGV[0].to_i).draw


    #> ruby circle.rb 7
    # * * * * *
    # * * * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * *
    # * * * *
    # * * * * *
  8. RKushnir revised this gist Feb 12, 2012. 1 changed file with 5 additions and 11 deletions.
    16 changes: 5 additions & 11 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,7 @@
    class Circle
    def initialize(radius)
    @radius = radius

    @points = []
    0.upto(radius) do |x|
    0.upto(radius / 2) do |y|
    @points << [x , y] if lies_on_circle?(x, y)
    end
    end
    @points = [*0..radius].product([*0..radius / 2]).select{|x, y| lies_on_circle? x, y }
    end

    def draw
    @@ -21,15 +15,15 @@ def draw
    puts @canvas.map(&:join).join "\n"
    end

    private

    def diameter
    @diameter ||= 2 * @radius
    end

    private

    # Detects if point is closest to the circle edge by checking the devitaion of points above and below
    def lies_on_circle?(x, y)
    cx, cy = @radius, @radius
    def lies_on_circle?(x ,y)
    cx, cy = @radius, @radius # central point
    dx2, r2 = (x - cx) ** 2, @radius ** 2
    [y - 1, y, y + 1].min_by {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 } == y
    end
  9. RKushnir revised this gist Feb 12, 2012. 1 changed file with 34 additions and 23 deletions.
    57 changes: 34 additions & 23 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,45 @@
    def circle(radius)
    cx, cy = radius, radius
    class Circle
    def initialize(radius)
    @radius = radius

    points = []
    0.upto(radius) do |x|
    0.upto(radius / 2) do |y|
    points << [x , y] if lies_on_circle?(x, y, cx, cy, radius)
    @points = []
    0.upto(radius) do |x|
    0.upto(radius / 2) do |y|
    @points << [x , y] if lies_on_circle?(x, y)
    end
    end
    end

    # Draw it
    diameter = 2 * radius
    canvas = Array.new(diameter + 1){ Array.new(2 * (diameter + 1)){ ' ' } }
    points.each do |p|
    set_symmetric_points(canvas, p[0], p[1], diameter)
    def draw
    unless @canvas
    @canvas = Array.new(diameter + 1){ Array.new(2 * (diameter + 1)){ ' ' } }
    @points.each do |p|
    set_symmetric_points(p[0], p[1])
    end
    end

    puts @canvas.map(&:join).join "\n"
    end

    canvas
    end
    def diameter
    @diameter ||= 2 * @radius
    end

    # Detects if point is closest to the circle edge by checking the devitaion of points above and below
    def lies_on_circle?(x, y, cx, cy, r)
    dx2, r2 = (x - cx) ** 2, r ** 2
    [y - 1, y, y + 1].min_by {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 } == y
    end
    private

    # Detects if point is closest to the circle edge by checking the devitaion of points above and below
    def lies_on_circle?(x, y)
    cx, cy = @radius, @radius
    dx2, r2 = (x - cx) ** 2, @radius ** 2
    [y - 1, y, y + 1].min_by {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 } == y
    end

    # Sets 8 points symmetrically to circle center
    def set_symmetric_points(canvas, x, y, diameter)
    [x, diameter - x].product([y, diameter - y]).each do |p|
    canvas[p[0]][2 * p[1]] = canvas[p[1]][2 * p[0]] = '*'
    # Sets 8 points symmetrically to circle center
    def set_symmetric_points(x, y)
    [x, diameter - x].product([y, diameter - y]).each do |p|
    @canvas[p[0]][2 * p[1]] = @canvas[p[1]][2 * p[0]] = '*'
    end
    end
    end

    puts circle(15).map(&:join).join "\n"
    Circle.new(15).draw
  10. RKushnir revised this gist Feb 12, 2012. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,7 @@ def circle(radius)
    points = []
    0.upto(radius) do |x|
    0.upto(radius / 2) do |y|
    deviations = deviations_y(x, y, cx, cy, radius)
    points << [x , y] if deviations.min == deviations[1]
    points << [x , y] if lies_on_circle?(x, y, cx, cy, radius)
    end
    end

    @@ -19,10 +18,10 @@ def circle(radius)
    canvas
    end

    # Calculates the deviation of points [x, y - 1], [x, y] and [x, y + 1] from the circle line
    def deviations_y(x, y, cx, cy, r)
    # Detects if point is closest to the circle edge by checking the devitaion of points above and below
    def lies_on_circle?(x, y, cx, cy, r)
    dx2, r2 = (x - cx) ** 2, r ** 2
    [y - 1, y, y + 1].map {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 }
    [y - 1, y, y + 1].min_by {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 } == y
    end

    # Sets 8 points symmetrically to circle center
  11. RKushnir revised this gist Feb 12, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -27,8 +27,8 @@ def deviations_y(x, y, cx, cy, r)

    # Sets 8 points symmetrically to circle center
    def set_symmetric_points(canvas, x, y, diameter)
    ([x, y, diameter - x, diameter - y] * 2).zip([y, x] * 2 + [diameter - y, diameter - x] * 2) do |idx|
    canvas[idx[0]][2 * idx[1]] = '*'
    [x, diameter - x].product([y, diameter - y]).each do |p|
    canvas[p[0]][2 * p[1]] = canvas[p[1]][2 * p[0]] = '*'
    end
    end

  12. RKushnir revised this gist Feb 12, 2012. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,21 @@
    def circle(radius)
    diameter = 2 * radius
    canvas = Array.new(diameter + 1){ Array.new(2 * (diameter + 1)){ ' ' } }
    cx, cy = radius, radius

    points = []
    0.upto(radius) do |x|
    0.upto(radius / 2) do |y|
    deviations = deviations_y(x, y, cx, cy, radius)
    set_symmetric_points(canvas, x, y, diameter) if deviations.min == deviations[1]
    points << [x , y] if deviations.min == deviations[1]
    end
    end

    # Draw it
    diameter = 2 * radius
    canvas = Array.new(diameter + 1){ Array.new(2 * (diameter + 1)){ ' ' } }
    points.each do |p|
    set_symmetric_points(canvas, p[0], p[1], diameter)
    end

    canvas
    end

  13. RKushnir revised this gist Feb 12, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion circle.rb
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def circle(radius)
    canvas
    end

    # Calculates the deviation of points [x, y - 1], [x, y] and [x, y+ 1] from the circle line
    # Calculates the deviation of points [x, y - 1], [x, y] and [x, y + 1] from the circle line
    def deviations_y(x, y, cx, cy, r)
    dx2, r2 = (x - cx) ** 2, r ** 2
    [y - 1, y, y + 1].map {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 }
  14. RKushnir revised this gist Feb 12, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ def circle(radius)
    canvas
    end

    # Calculates the deviation of points [x, y - 1], [x, y] and [x, y+ 1] from the circle line
    def deviations_y(x, y, cx, cy, r)
    dx2, r2 = (x - cx) ** 2, r ** 2
    [y - 1, y, y + 1].map {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 }
  15. RKushnir revised this gist Feb 12, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion circle.rb
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ def deviations_y(x, y, cx, cy, r)
    [y - 1, y, y + 1].map {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 }
    end

    # Sets 8 points symmetricaly to circle center
    # Sets 8 points symmetrically to circle center
    def set_symmetric_points(canvas, x, y, diameter)
    ([x, y, diameter - x, diameter - y] * 2).zip([y, x] * 2 + [diameter - y, diameter - x] * 2) do |idx|
    canvas[idx[0]][2 * idx[1]] = '*'
  16. RKushnir revised this gist Feb 12, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,7 @@ def deviations_y(x, y, cx, cy, r)
    [y - 1, y, y + 1].map {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 }
    end

    # Sets 8 points symmetricaly to circle center
    def set_symmetric_points(canvas, x, y, diameter)
    ([x, y, diameter - x, diameter - y] * 2).zip([y, x] * 2 + [diameter - y, diameter - x] * 2) do |idx|
    canvas[idx[0]][2 * idx[1]] = '*'
  17. RKushnir created this gist Feb 12, 2012.
    27 changes: 27 additions & 0 deletions circle.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    def circle(radius)
    diameter = 2 * radius
    canvas = Array.new(diameter + 1){ Array.new(2 * (diameter + 1)){ ' ' } }
    cx, cy = radius, radius

    0.upto(radius) do |x|
    0.upto(radius / 2) do |y|
    deviations = deviations_y(x, y, cx, cy, radius)
    set_symmetric_points(canvas, x, y, diameter) if deviations.min == deviations[1]
    end
    end

    canvas
    end

    def deviations_y(x, y, cx, cy, r)
    dx2, r2 = (x - cx) ** 2, r ** 2
    [y - 1, y, y + 1].map {|y| (dx2 + (y - cy) ** 2 - r2) ** 2 }
    end

    def set_symmetric_points(canvas, x, y, diameter)
    ([x, y, diameter - x, diameter - y] * 2).zip([y, x] * 2 + [diameter - y, diameter - x] * 2) do |idx|
    canvas[idx[0]][2 * idx[1]] = '*'
    end
    end

    puts circle(15).map(&:join).join "\n"