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) ⇒ DrawRasem

Returns a new instance of DrawRasem.



5
6
7
8
9
10
# File 'lib/stitchify/draw_rasem.rb', line 5

def initialize(px_arr, width=50, px=10)
    self.px_arr = px_arr
    self.width = width
    self.px = px
    clear_vars
end

Instance Attribute Details

#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



29
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
# File 'lib/stitchify/draw_rasem.rb', line 29

def build_rasem_data
    rasem_obj = self

    Rasem::SVGImage.new(width: 1000000000, height: 100000000) 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

        for pixel in rasem_obj.px_arr
            pos_x = rasem_obj.pos_x
            pos_y = rasem_obj.pos_y
            px = rasem_obj.px

            case pixel.shape
            when 'rectangle'
                rectangle pos_x, pos_y, px, px, fill: pixel.hex
            when 'x'
                line pos_x, pos_y, pos_x + px, pos_y + px
                line pos_x, pos_y + px, pos_x + px, pos_y
            when 'circle'
                circle (pos_x + px/2), (pos_y + px/2), px / 2 - 1, fill: pixel.hex
            when 'sm_rectangle'
                rectangle pos_x + 1, pos_y + 1, px - 2, px - 2, fill: pixel.hex
            when 'triangle'
                polygon [pos_x + px/2, pos_y + 1], [pos_x + 1, pos_y + px - 1], [pos_x + px - 1, pos_y + px - 1], fill: pixel.hex
            when 'diamond'
                polygon [pos_x + px/2, pos_y + 1], [pos_x + 1, pos_y + px/2], [pos_x + px/2, pos_y + px - 1], [pos_x + px - 1, pos_y + px/2], fill: pixel.hex
            when 'reverse_triangle'
                polygon [pos_x + px/2, pos_y + px - 1], [pos_x + 1, pos_y + 1], [pos_x + px - 1, pos_y + 1], fill: pixel.hex
            when 'left_triangle'
                polygon [pos_x + 1, pos_y + px/2], [pos_x + px - 1, pos_y + 1], [pos_x + px - 1, pos_y + px - 1], fill: pixel.hex
            when 'right_triangle'
                polygon [pos_x + px - 1, pos_y + px/2], [pos_x + 1, pos_y + 1], [pos_x + 1, pos_y + px - 1], fill: pixel.hex
            else
                rectangle pos_x, pos_y, px, px, fill: pixel.hex
            end

            rasem_obj.update_positions
        end
    end
end

#clear_varsObject



17
18
19
20
# File 'lib/stitchify/draw_rasem.rb', line 17

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

#gridObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/stitchify/draw_rasem.rb', line 72

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



12
13
14
15
# File 'lib/stitchify/draw_rasem.rb', line 12

def stitch
    write(build_rasem_data, file='stitchify.svg')
    clear_vars
end

#update_positionsObject



88
89
90
91
92
93
94
95
# File 'lib/stitchify/draw_rasem.rb', line 88

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



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

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