geom2d 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6b6f6aa211080908b25652229199e279f5785b45
4
- data.tar.gz: a3ea69fb6a9b81edbf18455ba6002dcca224f5ff
2
+ SHA256:
3
+ metadata.gz: 043eb93cd10b8d15d014db835f2ecaed3ec495a600b9c59dcb10f86335ceb22e
4
+ data.tar.gz: 97dfa82e0c56cb6e2a02dc3d9e3b2df2a8402ee9923cc7caf13ee75927a71e41
5
5
  SHA512:
6
- metadata.gz: 126e02e2e6a2b7ae9a4c0d78a9a48fbdedf3f6a7304d4cb67eee2b3da9d5160512d69ee99c1227dc3939eb4c883b19553c4a75da478158722cdb94f7713b2cf4
7
- data.tar.gz: 3e10879164f0785613aec700fb16cd4d1233605518166dd473b24e60e30212be18bf586dc1a14829b218964631febdd0f3c2add5410d489dfa51de65ae6ebe16
6
+ metadata.gz: e507dc3dab4bb5cc4b47e7067a74c89fa11622fa98aefb204a9953d8a2bb84b36caac9e810276723f086b01f6a921de7a99290f5f85d04bc965e6017d56bf8cb
7
+ data.tar.gz: daab0a906fa57068d9444c004ac06839fcb2fa1935c08042d8364e68bb79c23051df440e1cde2600e9ac8be939fb9caf3ba68fc7fe529b98588e27be2f4263cc
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## 0.4.0 - 2023-07-31
2
+
3
+ ### Added
4
+
5
+ * Class Geom2D::Rectangle for a more compact rectangle representation
6
+
7
+ ### Changed
8
+
9
+ * Require at least Ruby 2.6
10
+
11
+
1
12
  ## 0.3.1 - 2019-11-27
2
13
 
3
14
  ### Fixed
data/CONTRIBUTERS CHANGED
@@ -1,3 +1,3 @@
1
1
  Count Name
2
2
  ======= ====
3
- 21 Thomas Leitner <t_leitner@gmx.at>
3
+ 26 Thomas Leitner <t_leitner@gmx.at>
data/LICENSE CHANGED
@@ -1,5 +1,5 @@
1
1
  geom2d - 2D Geometry Objects and Algorithms
2
- Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
2
+ Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a
5
5
  copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -7,8 +7,8 @@ by Bézier curves.
7
7
 
8
8
  ## License
9
9
 
10
- Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>, licensed under the MIT - see the **LICENSE**
11
- file.
10
+ Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>, licensed under the MIT - see the
11
+ **LICENSE** file.
12
12
 
13
13
 
14
14
  ## Features
@@ -18,6 +18,7 @@ file.
18
18
  * Segment
19
19
  * Polygon
20
20
  * PolygonSet
21
+ * Rectangle
21
22
  * Polyline (TODO)
22
23
  * Rectangle (TODO)
23
24
  * QuadraticCurve (TODO)
data/Rakefile CHANGED
@@ -51,8 +51,8 @@ namespace :dev do
51
51
  s.files = PKG_FILES.to_a
52
52
 
53
53
  s.require_path = 'lib'
54
- s.required_ruby_version = '>= 2.4'
55
- s.add_development_dependency('rubocop', '~> 0.58', '>= 0.58.2')
54
+ s.required_ruby_version = '>= 2.6'
55
+ s.add_development_dependency('rubocop', '~> 1.0', '>= 1.41.1')
56
56
 
57
57
  s.author = 'Thomas Leitner'
58
58
  s.email = 't_leitner@gmx.at'
@@ -78,7 +78,7 @@ namespace :dev do
78
78
  statement = <<~STATEMENT
79
79
  #--
80
80
  # geom2d - 2D Geometric Objects and Algorithms
81
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
81
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
82
82
  #
83
83
  # This software may be modified and distributed under the terms
84
84
  # of the MIT license. See the LICENSE file for details.
@@ -91,7 +91,7 @@ namespace :dev do
91
91
  inserted = true
92
92
  puts "Updating file #{file}"
93
93
  old = File.read(file)
94
- old.sub!(/^#--.*?\n#\+\+\n|\A/m, statement)
94
+ old.sub!(/^#--.*?\n#\+\+\n/m, statement)
95
95
  File.write(file, old)
96
96
  end
97
97
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -240,17 +240,19 @@ module Geom2D
240
240
  # Returns +true+ if the operation is a trivial one, e.g. if one polygon set is empty.
241
241
  def trivial_operation(subject_bb, clipping_bb)
242
242
  if @subject.nr_of_contours * @clipping.nr_of_contours == 0
243
- if @operation == :difference
243
+ case @operation
244
+ when :difference
244
245
  @result = @subject
245
- elsif @operation == :union || @operation == :xor
246
+ when :union, :xor
246
247
  @result = (@subject.nr_of_contours == 0 ? @clipping : @subject)
247
248
  end
248
249
  true
249
250
  elsif subject_bb.min_x > clipping_bb.max_x || clipping_bb.min_x > subject_bb.max_x ||
250
251
  subject_bb.min_y > clipping_bb.max_y || clipping_bb.min_y > subject_bb.max_y
251
- if @operation == :difference
252
+ case @operation
253
+ when :difference
252
254
  @result = @subject
253
- elsif @operation == :union || @operation == :xor
255
+ when :union, :xor
254
256
  @result = @subject + @clipping
255
257
  end
256
258
  true
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -75,7 +75,7 @@ module Geom2D
75
75
  [@min_x, @min_y, @max_x, @max_y]
76
76
  end
77
77
 
78
- def inspect #:nodoc:
78
+ def inspect # :nodoc:
79
79
  "BBox[#{min_x}, #{min_y}, #{max_x}, #{max_y}]"
80
80
  end
81
81
 
data/lib/geom2d/point.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -135,7 +135,7 @@ module Geom2D
135
135
  end
136
136
  alias to_a to_ary
137
137
 
138
- def inspect #:nodoc:
138
+ def inspect # :nodoc:
139
139
  "(#{x}, #{y})"
140
140
  end
141
141
  alias to_s inspect
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -96,7 +96,7 @@ module Geom2D
96
96
  end
97
97
  alias to_a to_ary
98
98
 
99
- def inspect #:nodoc:
99
+ def inspect # :nodoc:
100
100
  "Polygon#{@vertices}"
101
101
  end
102
102
  alias to_s inspect
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -57,7 +57,7 @@ module Geom2D
57
57
  result
58
58
  end
59
59
 
60
- def inspect #:nodoc:
60
+ def inspect # :nodoc:
61
61
  "PolygonSet#{@polygons}"
62
62
  end
63
63
  alias to_s inspect
@@ -0,0 +1,100 @@
1
+ #--
2
+ # geom2d - 2D Geometric Objects and Algorithms
3
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
4
+ #
5
+ # This software may be modified and distributed under the terms
6
+ # of the MIT license. See the LICENSE file for details.
7
+ #++
8
+ # -*- frozen_string_literal: true -*-
9
+ #
10
+ #--
11
+ # geom2d - 2D Geometric Objects and Algorithms
12
+ # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
13
+ #
14
+ # This software may be modified and distributed under the terms
15
+ # of the MIT license. See the LICENSE file for details.
16
+ #++
17
+
18
+ require 'geom2d'
19
+
20
+ module Geom2D
21
+
22
+ # Represents an axis aligned rectangle.
23
+ class Rectangle
24
+
25
+ # The x-coordinate of the bottom-left corner of the rectangle.
26
+ attr_reader :x
27
+
28
+ # The y-coordinate of the bottom-left corner of the rectangle.
29
+ attr_reader :y
30
+
31
+ # The width of the rectangle.
32
+ attr_reader :width
33
+
34
+ # The height of the rectangle.
35
+ attr_reader :height
36
+
37
+ # Creates a new Rectangle object, with (x, y) specifying the bottom-left corner of the rectangle.
38
+ def initialize(x, y, width, height)
39
+ @x = x
40
+ @y = y
41
+ @width = width
42
+ @height = height
43
+ end
44
+
45
+ # Returns one since a rectangle object is a single polygon.
46
+ def nr_of_contours
47
+ 1
48
+ end
49
+
50
+ # Returns four since a rectangle has four vertices.
51
+ def nr_of_vertices
52
+ 4
53
+ end
54
+
55
+ # Calls the given block once for each corner of the rectangle.
56
+ #
57
+ # If no block is given, an Enumerator is returned.
58
+ def each_vertex(&block)
59
+ return to_enum(__method__) unless block_given?
60
+ vertices.each(&block)
61
+ end
62
+
63
+ # Calls the given block once for each segment of the rectangle.
64
+ #
65
+ # If no block is given, an Enumerator is returned.
66
+ def each_segment
67
+ return to_enum(__method__) unless block_given?
68
+
69
+ v = vertices
70
+ v.each_cons(2) {|v1, v2| yield(Geom2D::Segment.new(v1, v2)) }
71
+ yield(Geom2D::Segment.new(v[-1], v[0]))
72
+ end
73
+
74
+ # Returns the BoundingBox of this rectangle.
75
+ def bbox
76
+ BoundingBox.new(x, y, x + width, y + height)
77
+ end
78
+
79
+ # Returns +true+ since the vertices of the rectangle are always ordered in a counterclockwise
80
+ # fashion.
81
+ def ccw?
82
+ true
83
+ end
84
+
85
+ def inspect # :nodoc:
86
+ "Rectangle[(#{@x},#{@y}),width=#{@width},height=#{@height}]"
87
+ end
88
+ alias to_s inspect
89
+
90
+ private
91
+
92
+ # Returns an array with the four corners of the rectangle.
93
+ def vertices
94
+ [Geom2D::Point(x, y), Geom2D::Point(x + width, y),
95
+ Geom2D::Point(x + width, y + height), Geom2D::Point(x, y + height)]
96
+ end
97
+
98
+ end
99
+
100
+ end
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -197,7 +197,7 @@ module Geom2D
197
197
  start_point == other.start_point && end_point == other.end_point
198
198
  end
199
199
 
200
- def inspect #:nodoc:
200
+ def inspect # :nodoc:
201
201
  "Segment[#{start_point}-#{end_point}]"
202
202
  end
203
203
  alias to_s inspect
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -40,7 +40,7 @@ module Geom2D
40
40
  # Yields each value in sorted order.
41
41
  #
42
42
  # If no block is given, an enumerator is returned.
43
- def each(&block) #:yield: value
43
+ def each(&block) # :yield: value
44
44
  @list.each(&block)
45
45
  end
46
46
 
@@ -76,7 +76,7 @@ module Geom2D
76
76
  @list.pop
77
77
  end
78
78
 
79
- def inspect #:nodoc:
79
+ def inspect # :nodoc:
80
80
  "#<#{self.class.name}:0x#{object_id.to_s(16).rjust(0.size * 2, '0')} #{to_a}>"
81
81
  end
82
82
 
data/lib/geom2d/utils.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -11,6 +11,6 @@
11
11
  module Geom2D
12
12
 
13
13
  # The version of Geom2D
14
- VERSION = '0.3.1'
14
+ VERSION = '0.4.0'
15
15
 
16
16
  end
data/lib/geom2d.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #--
4
4
  # geom2d - 2D Geometric Objects and Algorithms
5
- # Copyright (C) 2018 Thomas Leitner <t_leitner@gmx.at>
5
+ # Copyright (C) 2018-2023 Thomas Leitner <t_leitner@gmx.at>
6
6
  #
7
7
  # This software may be modified and distributed under the terms
8
8
  # of the MIT license. See the LICENSE file for details.
@@ -19,6 +19,7 @@ module Geom2D
19
19
  autoload(:Segment, 'geom2d/segment')
20
20
  autoload(:Polygon, 'geom2d/polygon')
21
21
  autoload(:PolygonSet, 'geom2d/polygon_set')
22
+ autoload(:Rectangle, 'geom2d/rectangle')
22
23
 
23
24
  autoload(:BoundingBox, 'geom2d/bounding_box')
24
25
  autoload(:Algorithms, 'geom2d/algorithms')
@@ -67,4 +68,12 @@ module Geom2D
67
68
  PolygonSet.new(polygons)
68
69
  end
69
70
 
71
+ # Creates a Rectangle from the given bottom-left point (x, y) and the provided +width+ and
72
+ # +height+.
73
+ #
74
+ # See: Rectangle.new
75
+ def self.Rectangle(x, y, width, height)
76
+ Rectangle.new(x, y, width, height)
77
+ end
78
+
70
79
  end
@@ -21,7 +21,7 @@ describe Geom2D::BoundingBox do
21
21
  end
22
22
 
23
23
  it "fails if an invalid argument is given" do
24
- assert_raises(ArgumentError) { @bbox + "string" }
24
+ assert_raises(ArgumentError) { @bbox + :string }
25
25
  end
26
26
  end
27
27
 
@@ -66,7 +66,7 @@ describe Geom2D::Point do
66
66
  end
67
67
 
68
68
  it "fails if the argument class is invalid" do
69
- assert_raises(ArgumentError) { @point + "str" }
69
+ assert_raises(ArgumentError) { @point + :test }
70
70
  end
71
71
  end
72
72
 
@@ -0,0 +1,47 @@
1
+ # -*- frozen_string_literal: true -*-
2
+
3
+ require 'test_helper'
4
+ require 'geom2d/rectangle'
5
+
6
+ describe Geom2D::Rectangle do
7
+ before do
8
+ @rectangle = Geom2D::Rectangle(10, 20, 100, 50)
9
+ end
10
+
11
+ it "creates a rectangle using the bottom-left corner and its width and height" do
12
+ assert_equal(10, @rectangle.x)
13
+ assert_equal(20, @rectangle.y)
14
+ assert_equal(100, @rectangle.width)
15
+ assert_equal(50, @rectangle.height)
16
+ end
17
+
18
+ it "returns one for the number of contours" do
19
+ assert_equal(1, @rectangle.nr_of_contours)
20
+ end
21
+
22
+ it "returns four for the number of vertices" do
23
+ assert_equal(4, @rectangle.nr_of_vertices)
24
+ end
25
+
26
+ it "iterates over all corners of the rectangle" do
27
+ assert_equal([[10, 20], [110, 20], [110, 70], [10, 70]], @rectangle.each_vertex.to_a)
28
+ end
29
+
30
+ it "iterates over all four edges of the rectangle" do
31
+ segments = [Geom2D::Segment([10, 20], [110, 20]), Geom2D::Segment([110, 20], [110, 70]),
32
+ Geom2D::Segment([110, 70], [10, 70]), Geom2D::Segment([10, 70], [10, 20])]
33
+ assert_equal(segments, @rectangle.each_segment.to_a)
34
+ end
35
+
36
+ it "returns the bounding box" do
37
+ assert_equal([10, 20, 110, 70], @rectangle.bbox.to_a)
38
+ end
39
+
40
+ it "returns true when asked whether the corners are counterclockwise ordered" do
41
+ assert(@rectangle.ccw?)
42
+ end
43
+
44
+ it "returns a useful inspection string" do
45
+ assert_equal("Rectangle[(10,20),width=100,height=50]", @rectangle.inspect)
46
+ end
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geom2d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Leitner
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-27 00:00:00.000000000 Z
11
+ date: 2023-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -16,21 +16,21 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.58'
19
+ version: '1.0'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.58.2
22
+ version: 1.41.1
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '0.58'
29
+ version: '1.0'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.58.2
33
- description:
32
+ version: 1.41.1
33
+ description:
34
34
  email: t_leitner@gmx.at
35
35
  executables: []
36
36
  extensions: []
@@ -49,6 +49,7 @@ files:
49
49
  - lib/geom2d/point.rb
50
50
  - lib/geom2d/polygon.rb
51
51
  - lib/geom2d/polygon_set.rb
52
+ - lib/geom2d/rectangle.rb
52
53
  - lib/geom2d/segment.rb
53
54
  - lib/geom2d/utils.rb
54
55
  - lib/geom2d/utils/sorted_list.rb
@@ -59,6 +60,7 @@ files:
59
60
  - test/geom2d/test_point.rb
60
61
  - test/geom2d/test_polygon.rb
61
62
  - test/geom2d/test_polygon_set.rb
63
+ - test/geom2d/test_rectangle.rb
62
64
  - test/geom2d/test_segment.rb
63
65
  - test/geom2d/utils/test_sorted_list.rb
64
66
  - test/test_helper.rb
@@ -66,7 +68,7 @@ homepage: https://geom2d.gettalong.org
66
68
  licenses:
67
69
  - MIT
68
70
  metadata: {}
69
- post_install_message:
71
+ post_install_message:
70
72
  rdoc_options: []
71
73
  require_paths:
72
74
  - lib
@@ -74,16 +76,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
76
  requirements:
75
77
  - - ">="
76
78
  - !ruby/object:Gem::Version
77
- version: '2.4'
79
+ version: '2.6'
78
80
  required_rubygems_version: !ruby/object:Gem::Requirement
79
81
  requirements:
80
82
  - - ">="
81
83
  - !ruby/object:Gem::Version
82
84
  version: '0'
83
85
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.6.14.3
86
- signing_key:
86
+ rubygems_version: 3.4.1
87
+ signing_key:
87
88
  specification_version: 4
88
89
  summary: Objects and Algorithms for 2D Geometry
89
90
  test_files: []