Skip to content

Instantly share code, notes, and snippets.

@et
Created July 28, 2010 21:25
Show Gist options
  • Save et/496358 to your computer and use it in GitHub Desktop.
Save et/496358 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# transform a b c d
# x' = ax + by
# y' = cx + dy
def transform(x, y, vars)
a, b, c, d = vars[0].to_i, vars[1].to_i, vars[2].to_i, vars[3].to_i
x = a*x + b*y
y = c*x + d*y
return x, y
end
# translate X Y
# x' = x + X
# y' = y + Y
def translate(x, y, vars)
big_x = vars[0].to_i, big_y = vars[1].to_i
x += big_x
y += big_y
return x, y
end
# enlarge scale_factor
# x', y' = transform scale_factor, 0, 0, scale_factor
def enlarge(x, y, vars)
scale_factor = vars[0].to_f
x, y = transform(x, y, [scale_factor, 0, 0, scale_factor])
return x, y
end
# reflect y = mx
# x', y' = transform cos(2atan(m)) sin(2atan(m)) sin(2atan(m)) -cos(2atan(m))
def reflect(x, y, vars)
m = vars[2][0..0].to_f
x, y = transform(x, y, [Math.cos(2 * Math.atan(m)), Math.sin(2 * Math.atan(m)),
Math.sin(2 * Math.atan(m)), -1*cos(2 * Math.atan(m))])
return x, y
end
transformations = STDIN.gets('end')
STDIN.gets # call gets again since STDIN still has a newline in it.
coordinates = STDIN.gets
coordinates = coordinates.split
coordinates.map! {|coord| coord.split(',').map!{|point| point.to_f}}
transformations.each do |line|
command, vars = line.strip.split[0], line.strip.split[1..-1]
coordinates = coordinates.each do |coord|
if Object.responds_to?(command) then
coord[0], coord[1] = Object.send(command, coord[0], coord[1], vars)
end
end
end
puts coordinates.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment