class Polygonfy::Polygon

Constants

MARGIN
STYLES

Public Instance Methods

height() click to toggle source
# File lib/polygonfy/Polygon.rb, line 15
def height
  points.map(&:y).max + (2 * MARGIN)
end
point_title(i) click to toggle source
# 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) click to toggle source
# File lib/polygonfy/Polygon.rb, line 31
def point_x(point)
  point.x + MARGIN
end
point_y(point) click to toggle source
# File lib/polygonfy/Polygon.rb, line 35
def point_y(point)
  height - (point.y + MARGIN)
end
polygon_area() click to toggle source
# File lib/polygonfy/Polygon.rb, line 19
def polygon_area
  points.map { |p| "#{p.x + MARGIN},#{height - (p.y + MARGIN)}" }.join(' ')
end
to_xml() click to toggle source
# 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() click to toggle source
# File lib/polygonfy/Polygon.rb, line 11
def width
  points.map(&:x).max + (2 * MARGIN)
end