polygonfy 0.1.3 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90898057ee33cf3393b81492af4efbf7997684fd
4
- data.tar.gz: 1ca6af93f2874d593d741874ec71e09b341a2065
3
+ metadata.gz: 247cf21b400ac82f595a073c2732329262d65688
4
+ data.tar.gz: 3c81babcff85d424a2cd827e38411a7855b34f99
5
5
  SHA512:
6
- metadata.gz: 78e50da7b9970d1c43b707c0ee34376300de9ebef212ef01f5c919cf936671726789e4f587df592411e41fb3479479919690981ae1cd966caa242212223a1075
7
- data.tar.gz: b0b226be659a2d5be613e8cf313251ea3f684433f972ed16ceb2212495e1c121a977838b37000a0772fd2f00e11bd270a815d298a38116ad58a8b7ab353b940a
6
+ metadata.gz: 216d951db1c2bdfb9e995be50aa13694bef3ade071b91983854837a2a051c8803b276923c6bbb02569a958c61c0fd6c188306468032656bdc99f662b758bb70d
7
+ data.tar.gz: e60c7aae70b7e9cd6f015c4b828c948f2fcc12e7879d09fe11449337d4a0248efaa4a6205674e4364f6e53a90a7c5737c97c58fccfc893a6077cfc836ecaf7db
@@ -1,30 +1,14 @@
1
1
  require "polygonfy/version"
2
2
  require "polygonfy/Point"
3
- require "nokogiri"
3
+ require "polygonfy/Polygon"
4
4
 
5
5
  module Polygonfy
6
- MARGIN = 12
7
- STYLES = {
8
- polygon: "fill:#4f9bff; stroke:#216cce; stroke-width:1; opacity:0.5",
9
- circle: "fill:orange"
10
- }
11
-
12
- def self.point_title(array, i)
13
- prev_point = array[i - 1].id
14
- next_point = array[(i + 1) % array.size].id
15
- curr_point = "#{array[i].x}, #{array[i].y}"
16
-
17
- "#{prev_point} (#{curr_point}) #{next_point}"
18
- end
19
-
20
6
  def self.run
21
7
  if ARGV.length == 0
22
8
  puts "Use: polygonfy filename [id,x,y] [id,x,y] [..]"
23
9
  return
24
10
  end
25
11
 
26
- filename = ARGV[0]
27
-
28
12
  points = ARGV.length > 1 ? ARGV.drop(1) : STDIN.readline.split(' ')
29
13
 
30
14
  points.map! do |p|
@@ -32,31 +16,10 @@ module Polygonfy
32
16
  Point.new(id, x.to_i, y.to_i)
33
17
  end
34
18
 
35
- xmlns = "http://www.w3.org/2000/svg"
36
- width = points.map(&:x).max + (2 * MARGIN)
37
- height = points.map(&:y).max + (2 * MARGIN)
38
-
39
- polygon_points = points.map { |p| "#{p.x + MARGIN},#{p.y + MARGIN}" }.join(' ')
40
-
41
- builder = Nokogiri::XML::Builder.new do |xml|
42
- xml.svg(xmlns: xmlns, width: width, height: height) {
43
- xml.polygon(style: STYLES[:polygon], points: polygon_points)
44
- xml.g {
45
- points.each_with_index do |p, i|
46
- xml.circle(style: STYLES[:circle], cx: p.x + MARGIN, cy: p.y + MARGIN, r: "12")
47
- xml.text_(x: p.x + MARGIN, y: p.y + 5 + MARGIN, 'text-anchor': 'middle') {
48
- xml.text(p.id)
49
- xml.title {
50
- xml.text(point_title(points, i))
51
- }
52
- }
53
- end
54
- }
55
- }
56
- end
19
+ polygon = Polygon.new(points)
57
20
 
58
- File.open(filename, 'w') do |f|
59
- f.write(builder.to_xml)
21
+ File.open(ARGV[0], 'w') do |f|
22
+ f.write(polygon.to_xml)
60
23
  end
61
24
  end
62
25
  end
@@ -0,0 +1,61 @@
1
+ require "nokogiri"
2
+
3
+ module Polygonfy
4
+ class Polygon < Struct.new(:points)
5
+ MARGIN = 12
6
+ STYLES = {
7
+ polygon: "fill:#4f9bff; stroke:#216cce; stroke-width:1; opacity:0.5",
8
+ circle: "fill:orange"
9
+ }
10
+
11
+ def width
12
+ points.map(&:x).max + (2 * MARGIN)
13
+ end
14
+
15
+ def height
16
+ points.map(&:y).max + (2 * MARGIN)
17
+ end
18
+
19
+ def polygon_area
20
+ points.map { |p| "#{p.x + MARGIN},#{height - (p.y + MARGIN)}" }.join(' ')
21
+ end
22
+
23
+ def point_title(i)
24
+ prev_point = points[i - 1].id
25
+ next_point = points[(i + 1) % points.size].id
26
+ curr_point = "(#{points[i].x}, #{points[i].y})"
27
+
28
+ "#{prev_point} #{curr_point} #{next_point}"
29
+ end
30
+
31
+ def point_x(point)
32
+ point.x + MARGIN
33
+ end
34
+
35
+ def point_y(point)
36
+ height - (point.y + MARGIN)
37
+ end
38
+
39
+ def to_xml
40
+ builder = Nokogiri::XML::Builder.new do |xml|
41
+ xml.svg(xmlns: "http://www.w3.org/2000/svg", width: width, height: height) {
42
+ xml.polygon(style: STYLES[:polygon], points: polygon_area)
43
+ xml.g {
44
+ points.each_with_index do |p, i|
45
+ xml.circle(style: STYLES[:circle], cx: point_x(p), cy: point_y(p), r: "12")
46
+ xml.text_(x: point_x(p), y: point_y(p) + 5, 'text-anchor': 'middle') {
47
+ xml.text(p.id)
48
+ xml.title {
49
+ xml.text(point_title(i))
50
+ }
51
+ }
52
+ end
53
+ }
54
+ }
55
+ end
56
+
57
+ builder.to_xml
58
+ end
59
+
60
+ end
61
+ end
@@ -1,3 +1,3 @@
1
1
  module Polygonfy
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polygonfy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Magro
@@ -86,6 +86,7 @@ files:
86
86
  - bin/setup
87
87
  - lib/polygonfy.rb
88
88
  - lib/polygonfy/Point.rb
89
+ - lib/polygonfy/Polygon.rb
89
90
  - lib/polygonfy/version.rb
90
91
  - polygonfy.gemspec
91
92
  homepage: https://github.com/alexandremagro/polygonfy