openscad-text 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openscad-text.rb +4 -0
- data/lib/openscad-text/text.rb +28 -31
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f66bfde16ac04d7f2f1e1e9ccecd8333ef7568e7
|
4
|
+
data.tar.gz: e4fab6444746173adcfdf10693a04d454a397f9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 958160f74e8b355520204ad21aaa3d099ff84b28ed9fe14d0719ff1c346719dad0f15c2127917dcbd9d5372dc2def6721162397718a245b06ff930b0e50578d3
|
7
|
+
data.tar.gz: 4b6480ad3172e6cd4698b30adc2ff5219863c76f8713aee3122dc9f21ffde1b3e8081947f014ee0f2a05dbb77eb476370be7ff6651aff978771ce1a16d62c3b3
|
data/lib/openscad-text.rb
CHANGED
data/lib/openscad-text/text.rb
CHANGED
@@ -34,37 +34,35 @@ class Text
|
|
34
34
|
# checks if a point is already in the points ary
|
35
35
|
# aka if it has already been used and also if a point is surrounded
|
36
36
|
# by too many other black points
|
37
|
-
def point_invalid?(
|
37
|
+
def point_invalid?(point)
|
38
38
|
# white points are always invalid
|
39
|
-
return true if @matrix[
|
39
|
+
return true if @matrix[*point] == :white
|
40
40
|
|
41
41
|
# point already taken
|
42
|
-
return true if @points.any? { |
|
42
|
+
return true if @points.any? { |p| p == point }
|
43
43
|
|
44
44
|
# if not already taken border points are always valid
|
45
|
-
return false if
|
45
|
+
return false if point[0] == 0 or point[1] == 0 or point[0] == @x or point[1] == @y
|
46
46
|
|
47
47
|
# if all non-diagonal neighbours are black, the point must be invalid
|
48
|
-
neighbours = find_direct_neighbours(
|
48
|
+
neighbours = find_direct_neighbours(point)
|
49
49
|
return true if neighbours.count == 4 and neighbours.all? { |neighbour| @matrix[*neighbour] == :black }
|
50
50
|
|
51
51
|
# point is valid
|
52
52
|
false
|
53
53
|
end
|
54
54
|
|
55
|
-
def find_direct_neighbours(
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
55
|
+
def find_direct_neighbours(point)
|
56
|
+
# vecs to the 4 non-daigonal points
|
57
|
+
vecs = [
|
58
|
+
Vector[-1, 0],
|
59
|
+
Vector[ 1, 0],
|
60
|
+
Vector[ 0, 1],
|
61
|
+
Vector[ 0,-1]
|
62
|
+
]
|
61
63
|
|
62
|
-
|
63
|
-
|
64
|
-
[x_max, y ],
|
65
|
-
[x , y_max],
|
66
|
-
[x , y_min]
|
67
|
-
].uniq - [[x,y]]
|
64
|
+
neighbours = vecs.map { |vec| point + vec }
|
65
|
+
neighbours.delete_if { |point| point.any? { |i| i < 0 } }
|
68
66
|
end
|
69
67
|
|
70
68
|
# finds next point in chain from current point
|
@@ -82,11 +80,11 @@ class Text
|
|
82
80
|
]
|
83
81
|
|
84
82
|
# color of the last pixel
|
85
|
-
last_color = @matrix[*
|
83
|
+
last_color = @matrix[*current+vecs.last]
|
86
84
|
|
87
85
|
# turn the vector and find each which touches a white pixel
|
88
86
|
touchy_vecs = vecs.map.with_index do |vec,i|
|
89
|
-
current_color = @matrix[*
|
87
|
+
current_color = @matrix[*current+vec]
|
90
88
|
color_changed = current_color != last_color
|
91
89
|
last_color = current_color
|
92
90
|
|
@@ -107,10 +105,10 @@ class Text
|
|
107
105
|
touchy_vecs.uniq!
|
108
106
|
|
109
107
|
# possible next points
|
110
|
-
touchy_points = touchy_vecs.map { |vec|
|
108
|
+
touchy_points = touchy_vecs.map { |vec| current + vec }
|
111
109
|
|
112
110
|
# remove the invalid ones
|
113
|
-
touchy_points.delete_if { |point| point_invalid?
|
111
|
+
touchy_points.delete_if { |point| point_invalid? point }
|
114
112
|
|
115
113
|
# return the next point or nil
|
116
114
|
touchy_points[0]
|
@@ -118,16 +116,13 @@ class Text
|
|
118
116
|
|
119
117
|
# starting with point(x,y), try to create a path (or chain)
|
120
118
|
# until the starting point is reached again
|
121
|
-
def create_pixel_chain(
|
119
|
+
def create_pixel_chain(current_point)
|
122
120
|
# can't create a chain if the point is invalid
|
123
|
-
return if point_invalid?(
|
121
|
+
return if point_invalid?(current_point)
|
124
122
|
|
125
123
|
# create a new ary in the paths ary
|
126
124
|
@paths << []
|
127
125
|
|
128
|
-
# setup state
|
129
|
-
current_point = [x,y]
|
130
|
-
|
131
126
|
while current_point
|
132
127
|
# add the point to the points array
|
133
128
|
@points << current_point
|
@@ -142,10 +137,12 @@ class Text
|
|
142
137
|
|
143
138
|
# aligns the text to the bottom-left corner of the first quadrant
|
144
139
|
def align_points
|
145
|
-
x_min = @points.map { |
|
146
|
-
y_min = @points.map { |
|
140
|
+
x_min = @points.map { |p| p[0] }.min
|
141
|
+
y_min = @points.map { |p| p[1] }.min
|
142
|
+
|
143
|
+
vec = Vector[x_min,y_min]
|
147
144
|
|
148
|
-
@points.map! { |
|
145
|
+
@points.map! { |p| p - vec }
|
149
146
|
end
|
150
147
|
|
151
148
|
public
|
@@ -159,14 +156,14 @@ class Text
|
|
159
156
|
# go through each point aka pixel to make sure it gets used once
|
160
157
|
# and try to retrace the letters
|
161
158
|
@matrix.each_with_index do |_,x,y|
|
162
|
-
create_pixel_chain(x,y)
|
159
|
+
create_pixel_chain(Vector[x,y])
|
163
160
|
end
|
164
161
|
|
165
162
|
# align them!
|
166
163
|
align_points
|
167
164
|
|
168
165
|
# finished woop woop
|
169
|
-
"polygon(points=#{@points.to_s}, paths=#{@paths.to_s});"
|
166
|
+
"polygon(points=#{@points.map(&:to_a).to_s}, paths=#{@paths.to_s});"
|
170
167
|
end
|
171
168
|
|
172
169
|
=begin !!just for debugging!!
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openscad-text
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Lackner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rmagick
|