Class: Polygonfy::Polygon
- Inherits:
-
Struct
- Object
- Struct
- Polygonfy::Polygon
- Defined in:
- lib/polygonfy/Polygon.rb
Constant Summary collapse
- MARGIN =
12
- STYLES =
{ polygon: "fill:#4f9bff; stroke:#216cce; stroke-width:1; opacity:0.5", circle: "fill:orange" }
Instance Attribute Summary collapse
-
#points ⇒ Object
Returns the value of attribute points.
Instance Method Summary collapse
- #height ⇒ Object
- #point_title(i) ⇒ Object
- #point_x(point) ⇒ Object
- #point_y(point) ⇒ Object
- #polygon_area ⇒ Object
- #to_xml ⇒ Object
- #width ⇒ Object
Instance Attribute Details
#points ⇒ Object
Returns the value of attribute points
4 5 6 |
# File 'lib/polygonfy/Polygon.rb', line 4 def points @points end |
Instance Method Details
#height ⇒ Object
15 16 17 |
# File 'lib/polygonfy/Polygon.rb', line 15 def height points.map(&:y).max + (2 * MARGIN) end |
#point_title(i) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/polygonfy/Polygon.rb', line 23 def point_title(i) prev_point = points[i - 1].id next_point = points[(i + 1) % points.size].id curr_point = "(#{points[i].x}, #{points[i].y})" "#{prev_point} #{curr_point} #{next_point}" end |
#point_x(point) ⇒ Object
31 32 33 |
# File 'lib/polygonfy/Polygon.rb', line 31 def point_x(point) point.x + MARGIN end |
#point_y(point) ⇒ Object
35 36 37 |
# File 'lib/polygonfy/Polygon.rb', line 35 def point_y(point) height - (point.y + MARGIN) end |
#polygon_area ⇒ Object
19 20 21 |
# File 'lib/polygonfy/Polygon.rb', line 19 def polygon_area points.map { |p| "#{p.x + MARGIN},#{height - (p.y + MARGIN)}" }.join(' ') end |
#to_xml ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/polygonfy/Polygon.rb', line 39 def to_xml builder = Nokogiri::XML::Builder.new do |xml| xml.svg(xmlns: "http://www.w3.org/2000/svg", width: width, height: height) { xml.polygon(style: STYLES[:polygon], points: polygon_area) xml.g { points.each_with_index do |p, i| xml.circle(style: STYLES[:circle], cx: point_x(p), cy: point_y(p), r: "12") xml.text_(x: point_x(p), y: point_y(p) + 5, 'text-anchor': 'middle') { xml.text(p.id) xml.title { xml.text(point_title(i)) } } end } } end builder.to_xml end |
#width ⇒ Object
11 12 13 |
# File 'lib/polygonfy/Polygon.rb', line 11 def width points.map(&:x).max + (2 * MARGIN) end |