class PhotoGeometry attr_reader :mid_point, :x1, :y1, :x2, :y2, :rotation def initialize(h_pixels, v_pixels) reset(h_pixels, v_pixels) end def reset(h_pixels, v_pixels) @mid_point = [h_pixels.to_f / 2.0, v_pixels.to_f / 2.0] @rotation = 0 @x1 = @x2 = mid_point[0] @y1 = @y2 = mid_point[1] end def rotate_90_clockwise @x1, @y1, @x2, @y2 = @y2, @x1, @y1, @x2 @rotation += 90 @rotation -= 360 if @rotation >= 360 @mid_point.reverse! end def rotate_90_counterclockwise @x1, @y1, @x2, @y2 = @y1, @x2, @y2, @x1 @rotation -= 90 @rotation += 360 if @rotation < 0 @mid_point.reverse! end def crop(left_percent, top_percent, right_percent, bottom_percent) w = width h = height pixels = left_percent.to_f / 100 * w @x1 -= pixels pixels = top_percent.to_f / 100 * h @y1 -= pixels pixels = right_percent.to_f / 100 * w @x2 -= pixels pixels = bottom_percent.to_f / 100 * h @y2 -= pixels end def x_y_width_height return mid_point[0] - x1, mid_point[1] - y1, width, height end def x_y_width_height_before_rotation case @rotation when 90 return mid_point[1] - y1, mid_point[0] - x2, height, width when 180 return mid_point[0] - x2, mid_point[1] - y2 , width, height when 270 return mid_point[1] - y2, mid_point[0] - x1, height, width else return mid_point[0] - x1, mid_point[1] - y1, width, height end end def x_y_width_height_for(max_width, max_height) s = scale_factor_for(max_width, max_height) return (mid_point[0] - x1) * s, (mid_point[1] - y1) * s, width * s, height * s end def width x1 + x2 end def height y1 + y2 end def total_width 2.0 * mid_point[0] end def total_height 2.0 * mid_point[1] end def scale_factor_for(max_width, max_height) return [max_width.to_f / width, max_height.to_f / height].min end end