Class: DrawRasem

Inherits:
Object
  • Object
show all
Defined in:
lib/stitchify/draw_rasem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(px_arr, width = 50, px = 10, color_set = [], file_path = 'stitchify.svg') ⇒ DrawRasem

Returns a new instance of DrawRasem.



12
13
14
15
16
17
18
19
# File 'lib/stitchify/draw_rasem.rb', line 12

def initialize(px_arr, width=50, px=10, color_set=[], file_path='stitchify.svg')
    self.px_arr = px_arr
    self.width = width
    self.px = px
    self.color_set = color_set
    self.file_path = file_path
    clear_vars
end

Instance Attribute Details

#color_setObject

Returns the value of attribute color_set.



3
4
5
# File 'lib/stitchify/draw_rasem.rb', line 3

def color_set
  @color_set
end

#file_pathObject

Returns the value of attribute file_path.



3
4
5
# File 'lib/stitchify/draw_rasem.rb', line 3

def file_path
  @file_path
end

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/stitchify/draw_rasem.rb', line 3

def height
  @height
end

#pos_xObject

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_yObject

Returns the value of attribute pos_y.



3
4
5
# File 'lib/stitchify/draw_rasem.rb', line 3

def pos_y
  @pos_y
end

#pxObject

Returns the value of attribute px.



3
4
5
# File 'lib/stitchify/draw_rasem.rb', line 3

def px
  @px
end

#px_arrObject

Returns the value of attribute px_arr.



3
4
5
# File 'lib/stitchify/draw_rasem.rb', line 3

def px_arr
  @px_arr
end

#widthObject

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_dataObject



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
156
157
158
159
160
161
162
163
# File 'lib/stitchify/draw_rasem.rb', line 38

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) * 10 }
                text(1, (i + 1) * 10 * px + px + a, stroke_width: 1, 'font-size': '8px', class: 'grid-numbers') { raw (i + 1) * 10 }
            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_varsObject



26
27
28
29
# File 'lib/stitchify/draw_rasem.rb', line 26

def clear_vars
    self.pos_x = self.px
    self.pos_y = self.px
end

#gridObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/stitchify/draw_rasem.rb', line 165

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

#stitchObject



21
22
23
24
# File 'lib/stitchify/draw_rasem.rb', line 21

def stitch
    write(build_rasem_data, self.file_path)
    clear_vars
end

#update_positionsObject



181
182
183
184
185
186
187
188
# File 'lib/stitchify/draw_rasem.rb', line 181

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



31
32
33
34
35
# File 'lib/stitchify/draw_rasem.rb', line 31

def write(rasem, file)
    File.open(file, "w") do |f|
        rasem.write(f)
    end
end