rubypost 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/doc/created.rid +1 -0
- data/doc/fr_class_index.html +77 -0
- data/doc/fr_file_index.html +35 -0
- data/doc/fr_method_index.html +142 -0
- data/doc/index.html +24 -0
- data/lib/draw.rb +115 -0
- data/lib/drawable.rb +153 -185
- data/lib/graph.rb +222 -211
- data/lib/objects.rb +132 -98
- data/lib/options.rb +127 -127
- data/lib/revert_float_to_s.rb +6 -6
- data/lib/rubypost.rb +1 -0
- data/tests/{test_cricle.rb → test_circle.rb} +4 -5
- data/tests/test_graph.rb +13 -10
- data/tests/test_pair.rb +2 -4
- data/tests/test_path.rb +46 -62
- data/tests/test_picture.rb +26 -0
- data/tests/test_square.rb +15 -15
- metadata +13 -4
data/lib/drawable.rb
CHANGED
|
@@ -4,227 +4,195 @@ require 'matrix'
|
|
|
4
4
|
|
|
5
5
|
module RubyPost
|
|
6
6
|
|
|
7
|
-
#rubypost drawable
|
|
8
|
-
#base class for anything that actually gets drawn on a figure
|
|
9
|
-
class Drawable < Object
|
|
10
|
-
|
|
11
|
-
attr_reader :draw_type
|
|
12
|
-
|
|
13
|
-
def initialize
|
|
14
|
-
@options = Array.new
|
|
15
|
-
@draw_type = 'draw'
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
#normal path draw
|
|
19
|
-
def draw
|
|
20
|
-
@draw_type = 'draw'
|
|
21
|
-
self
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
#fill the path
|
|
25
|
-
def fill
|
|
26
|
-
@draw_type = 'fill'
|
|
27
|
-
self
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
#arrowhead at end of the path
|
|
31
|
-
def arrow
|
|
32
|
-
@draw_type = 'drawarrow'
|
|
33
|
-
self
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
#arrowhead at both ends
|
|
37
|
-
def dblarrow
|
|
38
|
-
@draw_type = 'drawdblarrow'
|
|
39
|
-
self
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def add_option(o)
|
|
43
|
-
@options.push(o)
|
|
44
|
-
self
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
#utility function to compile all the options
|
|
48
|
-
def compile_options
|
|
49
|
-
str = String.new
|
|
50
|
-
@options.each { |o| str = str + ' ' + o.compile }
|
|
51
|
-
return str
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
#macro for setting the colour of a drawable
|
|
55
|
-
def colour(r,g,b)
|
|
56
|
-
add_option(Colour.new(r,g,b))
|
|
57
|
-
self
|
|
7
|
+
#rubypost drawable
|
|
8
|
+
#base class for anything that actually gets drawn on a figure
|
|
9
|
+
class Drawable < Object
|
|
58
10
|
end
|
|
11
|
+
|
|
12
|
+
#wrapper for the metapost picture
|
|
13
|
+
#pictures are collection of drawables
|
|
14
|
+
class Picture < Drawable
|
|
15
|
+
|
|
16
|
+
attr_writer :name
|
|
17
|
+
|
|
18
|
+
#store a unique picture name unless incase it was not specified.
|
|
19
|
+
@@default_name = 0
|
|
20
|
+
|
|
21
|
+
#intialise a picture with it's name, use a string!
|
|
22
|
+
def initialize(name="picture_number" + @@default_name.to_s)
|
|
23
|
+
super()
|
|
24
|
+
@name = name
|
|
25
|
+
@draw_commands = Array.new
|
|
26
|
+
@@default_name = @@default_name + 1
|
|
27
|
+
@@picture_precompiler.add_picture(self)
|
|
28
|
+
end
|
|
59
29
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
colour(r,g,b)
|
|
64
|
-
end
|
|
30
|
+
def add_drawable(d)
|
|
31
|
+
@draw_commands.push(d)
|
|
32
|
+
end
|
|
65
33
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
34
|
+
#creates the definition of the picture that goes
|
|
35
|
+
#at the start of the file
|
|
36
|
+
def precompile
|
|
37
|
+
str = "picture " + @name + ";\n"
|
|
38
|
+
@draw_commands.each do |d|
|
|
39
|
+
str = str + d.compile + "\n"
|
|
40
|
+
end
|
|
41
|
+
str = str + @name + " := currentpicture; currentpicture := " + @@org_picture + ";\n"
|
|
42
|
+
end
|
|
72
43
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
add_option(Translate.new(x,y))
|
|
77
|
-
self
|
|
78
|
-
end
|
|
44
|
+
def compile
|
|
45
|
+
@name.compile
|
|
46
|
+
end
|
|
79
47
|
|
|
80
|
-
#macro for rotating a drawable. This is the
|
|
81
|
-
#same as using add_option(Rotate.new(a))
|
|
82
|
-
def rotate(a)
|
|
83
|
-
add_option(Rotate.new(a))
|
|
84
|
-
self
|
|
85
48
|
end
|
|
86
|
-
|
|
87
|
-
end
|
|
88
49
|
|
|
89
|
-
#A custom drawable. Send it a metapost string
|
|
90
|
-
class CustomDrawable < Drawable
|
|
50
|
+
#A custom drawable. Send it a metapost string
|
|
51
|
+
class CustomDrawable < Drawable
|
|
91
52
|
|
|
92
|
-
|
|
53
|
+
attr_writer :command
|
|
93
54
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
55
|
+
def initialize(c=String.new)
|
|
56
|
+
super()
|
|
57
|
+
@command = c
|
|
58
|
+
end
|
|
98
59
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
60
|
+
def compile
|
|
61
|
+
@command.compile
|
|
62
|
+
end
|
|
102
63
|
|
|
103
|
-
end
|
|
64
|
+
end
|
|
104
65
|
|
|
105
|
-
#Cartesion point.
|
|
106
|
-
class Pair < Drawable
|
|
66
|
+
#Cartesion point.
|
|
67
|
+
class Pair < Drawable
|
|
107
68
|
|
|
108
|
-
|
|
109
|
-
|
|
69
|
+
attr :x
|
|
70
|
+
attr :y
|
|
110
71
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
72
|
+
#Can set the value and individual scales of the x and y point
|
|
73
|
+
def initialize(x=0,y=0)
|
|
74
|
+
super()
|
|
75
|
+
@x = x
|
|
76
|
+
@y = y
|
|
77
|
+
end
|
|
117
78
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
79
|
+
def compile
|
|
80
|
+
'(' + @x.compile + ', ' + @y.compile + ')'
|
|
81
|
+
end
|
|
121
82
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
83
|
+
#returns the addition of the pairs
|
|
84
|
+
def +(p)
|
|
85
|
+
Pair.new(@x + p.x, @y + p.y)
|
|
86
|
+
end
|
|
126
87
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
88
|
+
#returns the subtraction of the pairs
|
|
89
|
+
def -(p)
|
|
90
|
+
Pair.new(@x - p.x, @y - p.y)
|
|
91
|
+
end
|
|
131
92
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
93
|
+
#returns the pair multiplied by a scalar
|
|
94
|
+
def *(n)
|
|
95
|
+
Pair.new(@x*n, @y*n)
|
|
96
|
+
end
|
|
136
97
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
98
|
+
#returns the pair divided by a scalar
|
|
99
|
+
def /(n)
|
|
100
|
+
Pair.new(@x/n, @y/n)
|
|
101
|
+
end
|
|
141
102
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
103
|
+
#returns true if the pairs are equal
|
|
104
|
+
def ==(p)
|
|
105
|
+
@x == p.x && @y == p.y
|
|
106
|
+
end
|
|
146
107
|
|
|
147
|
-
end
|
|
108
|
+
end
|
|
148
109
|
|
|
149
|
-
#sequence of pairs connected as a metapost path
|
|
150
|
-
class Path < Drawable
|
|
110
|
+
#sequence of pairs connected as a metapost path
|
|
111
|
+
class Path < Drawable
|
|
151
112
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
113
|
+
attr_writer :line_type
|
|
114
|
+
|
|
115
|
+
def initialize
|
|
116
|
+
super()
|
|
117
|
+
@p = Array.new
|
|
118
|
+
straight
|
|
119
|
+
end
|
|
157
120
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
121
|
+
def add_pair(p)
|
|
122
|
+
@p.push(p)
|
|
123
|
+
end
|
|
161
124
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
125
|
+
#returns a pair that is the centroid of the pairs
|
|
126
|
+
#of this path
|
|
127
|
+
def center(p)
|
|
128
|
+
ret = Pair.new
|
|
129
|
+
@p.each { |p| ret = ret + p }
|
|
130
|
+
return ret/p.length
|
|
131
|
+
end
|
|
169
132
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
133
|
+
#reverse the path. <br>
|
|
134
|
+
#Note, this reverses the pairs that have so far
|
|
135
|
+
#been added. Anything added after calling reverse
|
|
136
|
+
#will be appended to the end of the array as usual
|
|
137
|
+
def reverse
|
|
138
|
+
@p = @p.reverse
|
|
139
|
+
self
|
|
140
|
+
end
|
|
178
141
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
142
|
+
#set the path to have straight line segments
|
|
143
|
+
def straight
|
|
144
|
+
@line_type = '--'
|
|
145
|
+
self
|
|
146
|
+
end
|
|
184
147
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
148
|
+
#set the path to have curved line segments
|
|
149
|
+
def curved
|
|
150
|
+
@line_type = '..'
|
|
151
|
+
self
|
|
152
|
+
end
|
|
190
153
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
154
|
+
def compile
|
|
155
|
+
str = '('
|
|
156
|
+
(@p.length-1).times do |i|
|
|
157
|
+
str = str + @p[i].compile + @line_type
|
|
158
|
+
end
|
|
159
|
+
str + @p[-1].compile + ')'
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def clone
|
|
163
|
+
c = Path.new
|
|
164
|
+
@p.each{ |i| c.add_pair(i) }
|
|
165
|
+
c.line_type = @line_type
|
|
166
|
+
c
|
|
195
167
|
end
|
|
196
|
-
str = str + @p[-1].compile + ')'
|
|
197
|
-
str = str + compile_options + ";"
|
|
198
|
-
str
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
end
|
|
202
168
|
|
|
203
|
-
#Wraps teh metapost unitsquare command
|
|
204
|
-
class Square < Path
|
|
205
|
-
|
|
206
|
-
def initialize
|
|
207
|
-
super
|
|
208
|
-
@p.push(Pair.new(-0.5,-0.5))
|
|
209
|
-
@p.push(Pair.new(-0.5,0.5))
|
|
210
|
-
@p.push(Pair.new(0.5,0.5))
|
|
211
|
-
@p.push(Pair.new(0.5,-0.5))
|
|
212
|
-
@p.push('cycle')
|
|
213
169
|
end
|
|
214
|
-
|
|
215
|
-
end
|
|
216
170
|
|
|
217
|
-
#Wraps
|
|
218
|
-
class
|
|
171
|
+
#Wraps teh metapost unitsquare command
|
|
172
|
+
class Square < Path
|
|
173
|
+
|
|
174
|
+
def initialize
|
|
175
|
+
super()
|
|
176
|
+
@p.push(Pair.new(-0.5,-0.5))
|
|
177
|
+
@p.push(Pair.new(-0.5,0.5))
|
|
178
|
+
@p.push(Pair.new(0.5,0.5))
|
|
179
|
+
@p.push(Pair.new(0.5,-0.5))
|
|
180
|
+
@p.push('cycle')
|
|
181
|
+
end
|
|
219
182
|
|
|
220
|
-
def initialize
|
|
221
|
-
super
|
|
222
183
|
end
|
|
184
|
+
|
|
185
|
+
#Wraps the metapost fullcircle command
|
|
186
|
+
class Circle < Drawable
|
|
223
187
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
188
|
+
def initialize
|
|
189
|
+
super()
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def compile
|
|
193
|
+
'fullcircle'
|
|
194
|
+
end
|
|
227
195
|
|
|
228
|
-
end
|
|
196
|
+
end
|
|
229
197
|
|
|
230
198
|
end
|