Created
February 12, 2012 15:53
-
-
Save RKushnir/1809173 to your computer and use it in GitHub Desktop.
Revisions
-
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal 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 |kx, ky| [kx, diameter - kx].product([ky, diameter - ky]).each do |x, y| @canvas[x][2 * y] = @canvas[y][2 * x] = '*' end end end -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 10 additions and 19 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,25 +5,23 @@ def initialize(radius) 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]] = '*' end end end puts @canvas.join "\n" end private def key_points 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 end Circle.new(ARGV[0].to_i).draw -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 18 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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 # ruby circle.rb 7 # * * * * * # * * * * # * * # * * # * * # * * # * * # * * # * * # * * # * * # * * # * * # * * * * # * * * * * -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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) 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 -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
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 charactersOriginal 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 {|p| set_symmetric_points(*p) } end puts @canvas.map(&:join).join "\n" -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 10 additions and 21 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,12 @@ class Circle def initialize(radius) @radius = radius end 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 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 -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 19 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -36,4 +36,22 @@ def set_symmetric_points(x, y) end end Circle.new(ARGV[0].to_i).draw #> ruby circle.rb 7 # * * * * * # * * * * # * * # * * # * * # * * # * * # * * # * * # * * # * * # * * # * * # * * * * # * * * * * -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 5 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,7 @@ 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 @@ -21,15 +15,15 @@ def draw puts @canvas.map(&:join).join "\n" end private 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 = @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 -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 34 additions and 23 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,34 +1,45 @@ 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 end 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 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 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(15).draw -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 4 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal 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| points << [x , y] if lies_on_circle?(x, y, cx, cy, radius) end end @@ -19,10 +18,10 @@ def circle(radius) canvas 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 # Sets 8 points symmetrically to circle center -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal 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, diameter - x].product([y, diameter - y]).each do |p| canvas[p[0]][2 * p[1]] = canvas[p[1]][2 * p[0]] = '*' end end -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,21 @@ def circle(radius) cx, cy = radius, 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] 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 -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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 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 } -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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 } -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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 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]] = '*' -
RKushnir revised this gist
Feb 12, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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]] = '*' -
RKushnir created this gist
Feb 12, 2012 .There are no files selected for viewing
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 charactersOriginal 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"