------ art_4240_3519155.1203263723632
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Hello,
Had a lot of fun with this one :) My solution works by taking the average of
all points as the center, and the farthest outlying point as the radius. An
optimal solution is then found by moving the circle center towards the
outlier as the radius is reduced, while all points still are within the
radius. The same principles could be applied for higher dimensions as well.
class Point < Struct.new(:x, :y)
def self.random
Point.new(rand, rand)
end
def to_s
"(#{x}, #{y})"
end
end
class Circle < Struct.new(:center, :radius)
def to_s
"{center:#{center}, radius:#{radius}}"
end
end
# Calculate distance between points
def distance(pt_a, pt_b)
Math.sqrt((pt_a.x - pt_b.x) * (pt_a.x - pt_b.x) +
(pt_a.y - pt_b.y) * (pt_a.y - pt_b.y))
end
# Determine if given points are all within a circle
def inside_circle?(points, circle)
for point in points
dist istance(point, circle.center)
return false if dist > circle.radius
end
true
end
def encircle(points) # takes array of Point objects
# find the average midpoint of all points
mid oint.new(
points.inject(0){|sum, pt| sum + t.x} / (points.size * 1.0),
points.inject(0){|sum, pt| sum + t.y} / (points.size * 1.0))
# sort points by longest distance from midpoint
# longest point (index 0) is the initial radius
points.sort!{|a,b| distance(mid, a) <