Skip to content
Browse files

Breaking out large script into a better structure on the file system.…

… Making the script that is run from the command line be executable.
  • Loading branch information...
1 parent 3765196 commit 17d77adb5ccc2e8911769cf06257a6318dc915d6 @daphonz committed Apr 23, 2011
View
8 lib/class_extensions.rb
@@ -0,0 +1,8 @@
+module PointLayer
+ module NumericClassExtensions
+ # Extend numeric so we can convert to degrees readily.
+ def to_degrees
+ self * (180.0/Math::PI)
+ end
+ end
+end
View
70 lib/pointlayer2angles.rb → lib/file_reader.rb
@@ -1,5 +1,4 @@
module PointLayer
-
# The FileReader class is responsible for reading in the contents of a PointsLayer file
# and returning enough data to create Line objects.
# There is only one public method beyond the initializer, which iterates through a provided
@@ -66,71 +65,4 @@ def get_line_data(line1, line2)
attr_accessor :file, :x_index, :y_index, :group_index, :start_line, :separator
end
-
- class Line
- attr_accessor :point1, :point2
-
- # A line class needs two points to define it.
- def initialize(point1,point2)
- raise Point::NotAPointError unless point1.responds_like_a_point? && point2.responds_like_a_point?
- self.point1 = point1
- self.point2 = point2
- end
-
- def delta_x
- (point2 - point1).x
- end
-
- def delta_y
- (point2 - point1).y
- end
-
- def slope
- delta_y/delta_x
- end
-
- # Returns the (positive) angle from the horizontal in radians.
- def angle_to_horizontal
- ang = Math::atan2(delta_y, delta_x)
- (ang < 0) ? (ang + Math::PI) : ang
- end
-
- # Returns the (positive) angle from the vertical in radians.
- # Note that if the angle is calculated as negative, add PI
- # so we're looking at the correct angle relative to the veritcal line.
- # This basically allows angles larger than PI/2 to be properly calculated
- def angle_to_vertical
- ang = (Math::PI/2) - Math::atan2(delta_y, delta_x)
- (ang < 0) ? (ang + Math::PI) : ang
- end
- end
-
- class Point < Struct.new(:x, :y)
- class NotAPointError < StandardError; end;
-
- # Helper methods for adding and subtracting points from each other.
- # Point.new(1,3) + Point(1,1) == Point(2,4)
- ['-','+'].each do |operator|
- class_eval <<-"END"
- def #{operator}(point)
- raise Point::NotAPointError unless point.responds_like_a_point?
- Point.new(self.x.send("#{operator}".to_sym, point.x), self.y.send("#{operator}".to_sym, point.y))
- end
- END
- end
-
- # Doesn't have to be a Point object, but it has to quack like one.
- def responds_like_a_point?
- respond_to?(:x) && respond_to?(:y)
- end
-
- end
-
-end
-
-# Extend numeric so we can convert to degrees readily.
-class Numeric
- def to_degrees
- self * (180.0/Math::PI)
- end
-end
+end
View
41 lib/line.rb
@@ -0,0 +1,41 @@
+require 'point'
+
+module PointLayer
+ class Line
+ attr_accessor :point1, :point2
+
+ # A line class needs two points to define it.
+ def initialize(point1, point2)
+ raise Point::NotAPointError unless point1.responds_like_a_point? && point2.responds_like_a_point?
+ self.point1 = point1
+ self.point2 = point2
+ end
+
+ def delta_x
+ (point2 - point1).x
+ end
+
+ def delta_y
+ (point2 - point1).y
+ end
+
+ def slope
+ delta_y/delta_x
+ end
+
+ # Returns the (positive) angle from the horizontal in radians.
+ def angle_to_horizontal
+ ang = Math::atan2(delta_y, delta_x)
+ (ang < 0) ? (ang + Math::PI) : ang
+ end
+
+ # Returns the (positive) angle from the vertical in radians.
+ # Note that if the angle is calculated as negative, add PI
+ # so we're looking at the correct angle relative to the veritcal line.
+ # This basically allows angles larger than PI/2 to be properly calculated
+ def angle_to_vertical
+ ang = (Math::PI/2) - Math::atan2(delta_y, delta_x)
+ (ang < 0) ? (ang + Math::PI) : ang
+ end
+ end
+end
View
22 lib/point.rb
@@ -0,0 +1,22 @@
+module PointLayer
+ class Point < Struct.new(:x, :y)
+ class NotAPointError < StandardError; end;
+
+ # Helper methods for adding and subtracting points from each other.
+ # Point.new(1,3) + Point(1,1) == Point(2,4)
+ ['-','+'].each do |operator|
+ class_eval <<-"END"
+ def #{operator}(point)
+ raise Point::NotAPointError unless point.responds_like_a_point?
+ Point.new(self.x.send("#{operator}".to_sym, point.x), self.y.send("#{operator}".to_sym, point.y))
+ end
+ END
+ end
+
+ # Doesn't have to be a Point object, but it has to quack like one.
+ def responds_like_a_point?
+ respond_to?(:x) && respond_to?(:y)
+ end
+
+ end
+end
View
15 lib/pointlayer.rb
@@ -0,0 +1,15 @@
+# Ensure our load path knows about this directory.
+$: << File.dirname(__FILE__)
+
+require 'point'
+require 'line'
+require 'file_reader'
+require 'class_extensions'
+
+module PointLayer
+end
+
+# Extend the Numeric class with our custom extensions.
+class Numeric
+ include PointLayer::NumericClassExtensions
+end
View
2 pointlayer2angles.rb 100644 → 100755
@@ -12,7 +12,7 @@
# Example usage:
# pointlayer2angles point_layer_file.txt > angle_file.txt
#
-require 'lib/pointlayer.rb'
+require 'lib/pointlayer'
require 'rdoc/usage'
def print_usage
puts "Usage: \t#{$0} points_layer_file.txt > angle_file.txt" #$0 is the name of this file
View
6 test/pointlayer2angles_test.rb
@@ -1,5 +1,9 @@
require 'test/unit'
-require 'pointlayer2angles'
+
+# Make sure our lib is in our load path
+$: << File.expand_path(File.dirname(__FILE__) + "/../lib")
+
+require 'pointlayer'
class NumericTest < Test::Unit::TestCase
# Test our extentions of the Math module

0 comments on commit 17d77ad

Please sign in to comment.
Something went wrong with that request. Please try again.