Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created December 12, 2008 12:18
Show Gist options
  • Save hitode909/35096 to your computer and use it in GitHub Desktop.
Save hitode909/35096 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby
class Position
def initialize(x, y)
@x = x
@y = y
end
def x
@x
end
def y
@y
end
def to_s
"(#{@x}, #{@y})"
end
def dist_to(pos)
diffx = @x - pos.x
diffy = @y - pos.y
Math::sqrt(diffx ** 2 + diffy ** 2)
end
end
class Circle
def initialize(center, radius)
@center = center
@radius = radius
end
def center
@center
end
def radius
@radius
end
def to_s
"<中心#{@center.to_s}, 半径#{@radius}>"
end
def area
@radius ** 2 * Math::PI
end
def include?(pos)
@radius > @center.dist_to(pos)
end
end
# 座標を作って管理できるようにする
pos1 = Position.new(0.0, 0.0)
pos2 = Position.new(2.0, 0.0)
puts pos1
# 2つの座標の距離
puts pos1.dist_to(pos2)
# 円を作って管理できるようにする
cir = Circle.new(Position.new(0.0, 0.0), 1.0)
puts cir
# 面積
puts cir.area
# 座標を円の中に含むか
puts cir.include?(pos1)
puts cir.include?(pos2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment