Class: DrawRasem
- Inherits:
-
Object
- Object
- DrawRasem
- Defined in:
- lib/stitchify/draw_rasem.rb
Instance Attribute Summary collapse
-
#color_set ⇒ Object
Returns the value of attribute color_set.
-
#height ⇒ Object
Returns the value of attribute height.
-
#pos_x ⇒ Object
Returns the value of attribute pos_x.
-
#pos_y ⇒ Object
Returns the value of attribute pos_y.
-
#px ⇒ Object
Returns the value of attribute px.
-
#px_arr ⇒ Object
Returns the value of attribute px_arr.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #build_rasem_data ⇒ Object
- #clear_vars ⇒ Object
- #grid ⇒ Object
-
#initialize(px_arr, width = 50, px = 10, color_set = []) ⇒ DrawRasem
constructor
A new instance of DrawRasem.
- #stitch ⇒ Object
- #update_positions ⇒ Object
- #write(rasem, file) ⇒ Object
Constructor Details
#initialize(px_arr, width = 50, px = 10, color_set = []) ⇒ DrawRasem
Returns a new instance of DrawRasem.
5 6 7 8 9 10 11 |
# File 'lib/stitchify/draw_rasem.rb', line 5 def initialize(px_arr, width=50, px=10, color_set=[]) self.px_arr = px_arr self.width = width self.px = px self.color_set = color_set clear_vars end |
Instance Attribute Details
#color_set ⇒ Object
Returns the value of attribute color_set.
3 4 5 |
# File 'lib/stitchify/draw_rasem.rb', line 3 def color_set @color_set end |
#height ⇒ Object
Returns the value of attribute height.
3 4 5 |
# File 'lib/stitchify/draw_rasem.rb', line 3 def height @height end |
#pos_x ⇒ Object
Returns the value of attribute pos_x.
3 4 5 |
# File 'lib/stitchify/draw_rasem.rb', line 3 def pos_x @pos_x end |
#pos_y ⇒ Object
Returns the value of attribute pos_y.
3 4 5 |
# File 'lib/stitchify/draw_rasem.rb', line 3 def pos_y @pos_y end |
#px ⇒ Object
Returns the value of attribute px.
3 4 5 |
# File 'lib/stitchify/draw_rasem.rb', line 3 def px @px end |
#px_arr ⇒ Object
Returns the value of attribute px_arr.
3 4 5 |
# File 'lib/stitchify/draw_rasem.rb', line 3 def px_arr @px_arr end |
#width ⇒ Object
Returns the value of attribute width.
3 4 5 |
# File 'lib/stitchify/draw_rasem.rb', line 3 def width @width end |
Instance Method Details
#build_rasem_data ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/stitchify/draw_rasem.rb', line 30 def build_rasem_data rasem_obj = self Rasem::SVGImage.new(width: 1000000000, height: 100000000) do pos_x = rasem_obj.pos_x pos_y = rasem_obj.pos_y px = rasem_obj.px a = px / 4 b = px / 2 c = px - a d = a / 2 defs do group(id: 'rectangle') do rectangle pos_x, pos_y, px, px end group(id: 'x') do line pos_x, pos_y, pos_x + px, pos_y + px line pos_x, pos_y + px, pos_x + px, pos_y end group(id: 'circle') do circle (pos_x + b), (pos_y + b), a + d end group(id: 'sm_rectangle') do rectangle pos_x + a + 1, pos_y + a + 1, b, b end group(id: 'triangle') do polygon [pos_x + b, pos_y + a], [pos_x + c, pos_y + c], [pos_x + a, pos_y + c] end group(id: 'diamond') do polygon [pos_x + b, pos_y + 1], [pos_x + 1, pos_y + b], [pos_x + b, pos_y + px - 1], [pos_x + px - 1, pos_y + b] end group(id: 'reverse_triangle') do polygon [pos_x + a, pos_y + a], [pos_x + c, pos_y + a], [pos_x + b, pos_y + c] end group(id: 'left_triangle') do polygon [pos_x + a, pos_y + b], [pos_x + c, pos_y + a], [pos_x + c, pos_y + c] end group(id: 'right_triangle') do polygon [pos_x + a, pos_y + a], [pos_x + c, pos_y + b], [pos_x + a, pos_y + c] end group(id: 'star') do polygon [pos_x + a + 1, pos_y + 1], [pos_x + b, pos_y + a + 1], [pos_x + c - 1, pos_y + 1], [pos_x + c - 1, pos_y + a + 1], [pos_x + px - 1, pos_y + a + 1], [pos_x + c - 1, pos_y + b], [pos_x + px - 1, pos_y + c - 1], [pos_x + c - 1, pos_y + c - 1], [pos_x + c - 1, pos_y + px - 1], [pos_x + b, pos_y + c - 1], [pos_x + a + 1, pos_y + px - 1], [pos_x + a + 1, pos_y + c - 1], [pos_x + 1, pos_y + c - 1], [pos_x + a + 1, pos_y + b], [pos_x + 1, pos_y + a + 1], [pos_x + a + 1, pos_y + a + 1], [pos_x + a + 1, pos_y + 1] end group(id: 'heart') do polygon [pos_x + a, pos_y + a], [pos_x + b - d, pos_y + a], [pos_x + b, pos_y + b - d], [pos_x + b + d, pos_y + a], [pos_x + c, pos_y + a], [pos_x + c, pos_y + b], [pos_x + b, pos_y + c], [pos_x + a, pos_y + b] end end # grid group stroke: 'black' do for line_data in rasem_obj.grid line line_data[0], line_data[1], line_data[2], line_data[3], :stroke_width=>line_data[5] end blocks = rasem_obj.width / 10 blocks.times do |i| text((i + 1) * 10 * px + b, c, stroke_width: 1, 'font-size': '8px', class: 'grid-numbers') { raw (i + 1) * 5 } text(1, (i + 1) * 10 * px + px + a, stroke_width: 1, 'font-size': '8px', class: 'grid-numbers') { raw (i + 1) * 5 } end end # content for pixel in rasem_obj.px_arr use(pixel.shape, x: rasem_obj.pos_x - px, y: rasem_obj.pos_y - px, fill: pixel.hex) rasem_obj.update_positions end # legend group stroke: 'black' do legend_pos_x = (rasem_obj.width + 2) * px legend_pos_y = 2 * px legend_height = px * (rasem_obj.color_set.length * 2) rectangle legend_pos_x, legend_pos_y, 10 * px, legend_height, fill: 'white' rasem_obj.color_set.each_with_index do |pixel, index| use(pixel.shape, x: legend_pos_x - b, y: legend_pos_y - b, fill: pixel.hex) text(legend_pos_x + 2 * px, legend_pos_y + px + b) { raw pixel.hex } legend_pos_y += 2 * px end end end end |
#clear_vars ⇒ Object
18 19 20 21 |
# File 'lib/stitchify/draw_rasem.rb', line 18 def clear_vars self.pos_x = self.px self.pos_y = self.px end |
#grid ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/stitchify/draw_rasem.rb', line 157 def grid n = 10 output = [] (self.width + 1).times do |i| x = 1 x = 2 if (i % 10 == 0) output << [(i + 1) * self.px, self.px, (i + 1) * self.px, (self.width + 1) * self.px, 'black', x] end (self.width + 1).times do |i| x = 1 x = 2 if i % 10 == 0 output << [self.px, (i + 1) * self.px, (self.width + 1) * self.px, (i + 1) * self.px, 'black', x] end output end |
#stitch ⇒ Object
13 14 15 16 |
# File 'lib/stitchify/draw_rasem.rb', line 13 def stitch write(build_rasem_data, file='stitchify.svg') clear_vars end |
#update_positions ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/stitchify/draw_rasem.rb', line 173 def update_positions if (self.pos_x / self.px) < self.width self.pos_x = self.pos_x + self.px else self.pos_x = self.px self.pos_y = self.pos_y + self.px end end |
#write(rasem, file) ⇒ Object
23 24 25 26 27 |
# File 'lib/stitchify/draw_rasem.rb', line 23 def write(rasem, file) File.open(file, "w") do |f| rasem.write(f) end end |