4f5d54213efb6502baab202f3fb2f09e

Any comments, corrections, refactorings or suggestions would be fantastic, thanks.
Hopefully we might have some photography nuts out there too :)

# camera related
def frame_diagonal(frame_horizontal, frame_vertical)
	@frame_diagonal = Math.sqrt((frame_horizontal**2) + (frame_vertical**2))
end

def focal_length_multiplier(frame_diagonal_1, frame_diagonal_2)
	@focal_length_multiplier = frame_diagonal_1 / frame_diagonal_2
end

def circle_of_confusion(frame_diagonal)
	@circle_of_confusion = frame_diagonal / 1730
end

# lens related
def hyperfocal_distance(focal_length, f_number, circle_of_confusion)
	@hyperfocal_distance = ((focal_length**2) / (f_number * circle_of_confusion)) + focal_length
end

def near_distance(focus_distance, hyperfocal_distance, focal_length)
	@near_distance = (focus_distance * (hyperfocal_distance - focal_length)) / (hyperfocal_distance + focus_distance - 2 * focal_length)
end

def far_distance(focus_distance, hyperfocal_distance, focal_length)
	@far_distance = (focus_distance * (hyperfocal_distance - focal_length)) / (hyperfocal_distance - focus_distance)
end

def total_distance(near_distance, far_distance)
	@total_distance = far_distance - near_distance
end

def magnification(focal_length, focus_distance)
	@magnification = focal_length/(focus_distance - focal_length)
end

def fov_rectilinear_horizontal(frame_horizontal, focal_length, magnification)
	@fov_rectilinear = 2 * Math.arctan(frame_horizontal/(focal_length * 2 * (magnification+1)))
end

def fov_rectilinear_vertical(frame_vertical, focal_length, magnification)
	@fov_rectilinear = 2 * Math.arctan(frame_vertical/(focal_length * 2 * (magnification+1)))
end

def fov_rectilinear_diagonal(frame_diagonal, focal_length, magnification)
	@fov_rectilinear = 2 * Math.arctan(frame_diagonal/(focal_length * 2 * (magnification+1)))
end

def f_number_to_aperture_diameter(focal_length, f_number)
	@aperture_diameter = focal_length / f_number
end

# exposure related
def exposure_value(f_number, shutter_speed)
	@exposure_value = Math.logn(2, (f_number**2 / shutter_speed))
end

# math related
def Math.logn(x, n)
	Math.log(x) / Math.log(n)
end

def Math.arctan(x)
	Math.atan2(x, 1.0)
end

Refactorings

No refactoring yet !

55502f40dc8b7c769880b10874abc9d0

raggi

December 8, 2008, December 08, 2008 11:34, permalink

1 rating. Login to rate!

If you're using this code inside a class you could use @var ||= some_value in order to get a poor mans memoization on the calculations. If you're using al values as temporaries then I'd strip out all of the assignment.

# camera related
def frame_diagonal(frame_horizontal, frame_vertical)
  Math.sqrt((frame_horizontal**2) + (frame_vertical**2))
end

def focal_length_multiplier(frame_diagonal_1, frame_diagonal_2)
  frame_diagonal_1 / frame_diagonal_2
end

def circle_of_confusion(frame_diagonal)
  frame_diagonal / 1730
end

# lens related
def hyperfocal_distance(focal_length, f_number, circle_of_confusion)
  ((focal_length**2) / (f_number * circle_of_confusion)) + focal_length
end

def near_distance(focus_distance, hyperfocal_distance, focal_length)
  (focus_distance * (hyperfocal_distance - focal_length)) / (hyperfocal_distance + focus_distance - 2 * focal_length)
end

def far_distance(focus_distance, hyperfocal_distance, focal_length)
  (focus_distance * (hyperfocal_distance - focal_length)) / (hyperfocal_distance - focus_distance)
end

def total_distance(near_distance, far_distance)
  far_distance - near_distance
end

def magnification(focal_length, focus_distance)
  focal_length/(focus_distance - focal_length)
end

def fov_rectilinear_horizontal(frame_horizontal, focal_length, magnification)
  2 * Math.arctan(frame_horizontal/(focal_length * 2 * (magnification+1)))
end

def fov_rectilinear_vertical(frame_vertical, focal_length, magnification)
  2 * Math.arctan(frame_vertical/(focal_length * 2 * (magnification+1)))
end

def fov_rectilinear_diagonal(frame_diagonal, focal_length, magnification)
  2 * Math.arctan(frame_diagonal/(focal_length * 2 * (magnification+1)))
end

def f_number_to_aperture_diameter(focal_length, f_number)
  focal_length / f_number
end

# exposure related
def exposure_value(f_number, shutter_speed)
  Math.logn(2, (f_number**2 / shutter_speed))
end

# math related
def Math.logn(x, n)
  Math.log(x) / Math.log(n)
end

def Math.arctan(x)
  Math.atan2(x, 1.0)
end
07e4e448342ef6d2d5c70c8fba26b5c7

Teige

April 23, 2011, April 23, 2011 07:36, permalink

No rating. Login to rate!

Very true! Makes a change to see soemnoe spell it out like that. :)

Very true! Makes a change to see soemnoe spell it out like that. :)
07e4e448342ef6d2d5c70c8fba26b5c7

Teige

April 23, 2011, April 23, 2011 07:36, permalink

No rating. Login to rate!

Very true! Makes a change to see soemnoe spell it out like that. :)

Very true! Makes a change to see soemnoe spell it out like that. :)
07e4e448342ef6d2d5c70c8fba26b5c7

Teige

April 23, 2011, April 23, 2011 07:36, permalink

No rating. Login to rate!

Very true! Makes a change to see soemnoe spell it out like that. :)

Very true! Makes a change to see soemnoe spell it out like that. :)

Your refactoring





Format Copy from initial code

or Cancel