ruby-geometry 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class LengthTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_parallel_to_axis
8
+ assert 1 === Segment.new_by_arrays([1, 1], [2, 1]).length
9
+ assert 1 === Segment.new_by_arrays([1, 1], [1, 2]).length
10
+ end
11
+
12
+ def test_inclined
13
+ assert sqrt(2) === Segment.new_by_arrays([1, 1], [2, 2]).length
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class OverlapsTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_overlaps
8
+ segment1 = Segment.new_by_arrays([0, 0], [2, 0])
9
+ segment2 = Segment.new_by_arrays([1, 0], [3, 0])
10
+
11
+ assert segment1.overlaps?(segment2)
12
+ end
13
+
14
+ def test_same_segment
15
+ segment1 = Segment.new_by_arrays([0, 0], [2, 0])
16
+ assert segment1.overlaps?(segment1)
17
+ end
18
+
19
+ def test_intersects_in_one_point
20
+ segment1 = Segment.new_by_arrays([0, 0], [1, 0])
21
+ segment2 = Segment.new_by_arrays([1, 0], [1, 1])
22
+
23
+ assert ! segment1.overlaps?(segment2)
24
+ end
25
+
26
+ def test_lies_on_one_line_and_have_common_endpoint
27
+ segment1 = Segment.new_by_arrays([0, 0], [1, 0])
28
+ segment2 = Segment.new_by_arrays([1, 0], [2, 0])
29
+
30
+ assert ! segment1.overlaps?(segment2)
31
+ end
32
+
33
+ def test_lies_on_one_line
34
+ segment1 = Segment.new_by_arrays([0, 0], [2, 0])
35
+ segment2 = Segment.new_by_arrays([4, 0], [6, 0])
36
+
37
+ assert ! segment1.overlaps?(segment2)
38
+ end
39
+
40
+ def test_lies_on_one_vertical_line
41
+ segment1 = Segment.new_by_arrays([0, 0], [0, 1])
42
+ segment2 = Segment.new_by_arrays([0, 2], [0, 3])
43
+
44
+ assert ! segment1.overlaps?(segment2)
45
+ end
46
+ end
@@ -0,0 +1,27 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class ParallelToTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_segments_are_parallel
8
+ segment1 = Segment.new_by_arrays([0, 0], [2, 1])
9
+ segment2 = Segment.new_by_arrays([1, 1], [5, 3])
10
+
11
+ assert segment1.parallel_to?(segment2)
12
+ end
13
+
14
+ def test_segments_are_not_parallel
15
+ segment1 = Segment.new_by_arrays([0, 0], [2, 1])
16
+ segment2 = Segment.new_by_arrays([1, 1], [2, 2])
17
+
18
+ assert ! segment1.parallel_to?(segment2)
19
+ end
20
+
21
+ def test_segment_lay_on_same_line
22
+ segment1 = Segment.new_by_arrays([0, 0], [1, 1])
23
+ segment2 = Segment.new_by_arrays([2, 2], [3, 3])
24
+
25
+ assert segment1.parallel_to?(segment2)
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class ToVectorTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_regular_case
8
+ segment = Segment.new_by_arrays([1, 1], [3, 2])
9
+
10
+ assert_equal Vector.new(2, 1), segment.to_vector
11
+ end
12
+
13
+ def test_takes_direction_into_account
14
+ end_points = [Point.new(1, 1), Point.new(2, 2)]
15
+
16
+ assert_equal Vector.new(1, 1), Segment.new(end_points[0], end_points[1]).to_vector
17
+ assert_equal Vector.new(-1, -1), Segment.new(end_points[1], end_points[0]).to_vector
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class ArithmeticsTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_summation
8
+ assert_equal Vector.new(4, 6), Vector.new(1, 2) + Vector.new(3, 4)
9
+ end
10
+
11
+ def test_subtraction
12
+ assert_equal Vector.new(-2, -4), Vector.new(1, 0) - Vector.new(3, 4)
13
+ end
14
+
15
+ def test_vector_multiplied_by_scalar
16
+ assert_equal Vector.new(-2, -4), Vector.new(1, 2) * -2
17
+ end
18
+
19
+ def test_scalar_multiplied_by_vector
20
+ assert_equal Vector.new(-2, -4), -2 * Vector.new(1, 2)
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class CollinearWithTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_vectors_are_collinear
8
+ vector1 = Vector.new(1, 2)
9
+ vector2 = Vector.new(2, 4)
10
+
11
+ assert vector1.collinear_with?(vector2)
12
+ end
13
+
14
+ def test_vectors_are_not_collinear
15
+ vector1 = Vector.new(1, 2)
16
+ vector2 = Vector.new(1, 1)
17
+
18
+ assert ! vector1.collinear_with?(vector2)
19
+ end
20
+
21
+ def test_vectors_are_oppositely_directed
22
+ vector1 = Vector.new(2, 2)
23
+ vector2 = Vector.new(-2, -2)
24
+
25
+ assert vector1.collinear_with?(vector2)
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class CrossProductTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_positive
8
+ assert 1 === Vector.new(1, 0).cross_product(Vector.new(0, 1))
9
+ end
10
+
11
+ def test_negative
12
+ assert(-1 === Vector.new(0, 1).cross_product(Vector.new(1, 0)))
13
+ end
14
+
15
+ def test_zero
16
+ assert 0 === Vector.new(1, 1).cross_product(Vector.new(-2, -2))
17
+ end
18
+
19
+ def test_unnormalized
20
+ assert 4 === Vector.new(1, 1).cross_product(Vector.new(-2, 2))
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class EqualsTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_equal
8
+ assert_equal Vector.new(1, 3), Vector.new(1, 3)
9
+ end
10
+
11
+ def test_not_equal
12
+ assert_not_equal Vector.new(1, 3), Vector.new(1, 2)
13
+ assert_not_equal Vector.new(1, 2), Vector.new(0, 2)
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class ModulusTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_parallel_to_axis
8
+ assert 1 === Vector.new(1, 0).modulus
9
+ assert 1 === Vector.new(0, 1).modulus
10
+ end
11
+
12
+ def test_inclined
13
+ assert sqrt(2) === Vector.new(1, 1).modulus
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ require 'test/unit'
2
+ require 'geometry'
3
+
4
+ class ScalarProductTest < Test::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_vectors_are_perpendicular
8
+ assert 0 === Vector.new(1, 1).scalar_product(Vector.new(-1, 1))
9
+ end
10
+
11
+ def test_vectors_are_collinear
12
+ assert(-4 === Vector.new(1, 1).scalar_product(Vector.new(-2, -2)))
13
+ end
14
+
15
+ def test_vectors_are_inclined
16
+ assert 1 === Vector.new(1, 1).scalar_product(Vector.new(0, 1))
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-geometry
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Daniel Vartanov
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-17 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: daniel.vartanov@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/point.rb
31
+ - lib/line.rb
32
+ - lib/comparison_with_precision.rb
33
+ - lib/polygon.rb
34
+ - lib/vector.rb
35
+ - lib/geometry.rb
36
+ - lib/segment.rb
37
+ - test/geometry/distance_test.rb
38
+ - test/polygon/edges_test.rb
39
+ - test/segment/contains_point_test.rb
40
+ - test/segment/overlaps_test.rb
41
+ - test/segment/to_vector_test.rb
42
+ - test/segment/length_test.rb
43
+ - test/segment/intersection_point_with_test.rb
44
+ - test/segment/parallel_to_test.rb
45
+ - test/segment/intersects_with_test.rb
46
+ - test/segment/bounds_test.rb
47
+ - test/segment/initialize_test.rb
48
+ - test/line/x_intercept_test.rb
49
+ - test/line/y_intercept_test.rb
50
+ - test/line/parallel_to_test.rb
51
+ - test/line/angle_to_test.rb
52
+ - test/line/initialize_test.rb
53
+ - test/line/vertical_test.rb
54
+ - test/line/slope_test.rb
55
+ - test/line/intersect_x_test.rb
56
+ - test/line/horizontal_test.rb
57
+ - test/vector/equals_test.rb
58
+ - test/vector/modulus_test.rb
59
+ - test/vector/arithmetics_test.rb
60
+ - test/vector/cross_product_test.rb
61
+ - test/vector/scalar_product_test.rb
62
+ - test/vector/collinear_with_test.rb
63
+ - test/point/equals_test.rb
64
+ - test/point/initialize_test.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/DanielVartanov/ruby-geometry
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.7
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Implementation of basic 2D geometry algorithms in Ruby
97
+ test_files:
98
+ - test/geometry/distance_test.rb
99
+ - test/polygon/edges_test.rb
100
+ - test/segment/contains_point_test.rb
101
+ - test/segment/overlaps_test.rb
102
+ - test/segment/to_vector_test.rb
103
+ - test/segment/length_test.rb
104
+ - test/segment/intersection_point_with_test.rb
105
+ - test/segment/parallel_to_test.rb
106
+ - test/segment/intersects_with_test.rb
107
+ - test/segment/bounds_test.rb
108
+ - test/segment/initialize_test.rb
109
+ - test/line/x_intercept_test.rb
110
+ - test/line/y_intercept_test.rb
111
+ - test/line/parallel_to_test.rb
112
+ - test/line/angle_to_test.rb
113
+ - test/line/initialize_test.rb
114
+ - test/line/vertical_test.rb
115
+ - test/line/slope_test.rb
116
+ - test/line/intersect_x_test.rb
117
+ - test/line/horizontal_test.rb
118
+ - test/vector/equals_test.rb
119
+ - test/vector/modulus_test.rb
120
+ - test/vector/arithmetics_test.rb
121
+ - test/vector/cross_product_test.rb
122
+ - test/vector/scalar_product_test.rb
123
+ - test/vector/collinear_with_test.rb
124
+ - test/point/equals_test.rb
125
+ - test/point/initialize_test.rb