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/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
- #macro for setting the colour of a drawable.
61
- #Spelt incorrectly.
62
- def color(r,g,b)
63
- colour(r,g,b)
64
- end
30
+ def add_drawable(d)
31
+ @draw_commands.push(d)
32
+ end
65
33
 
66
- #macro for scaling a drawable. This is the
67
- #same as using add_option(Scale.new(s))
68
- def scale(s)
69
- add_option(Scale.new(s))
70
- self
71
- end
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
- #macro for translating a drawable. This is the
74
- #same as using add_option(Translate.new(x,y))
75
- def translate(x,y)
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
- attr_writer :command
53
+ attr_writer :command
93
54
 
94
- def initialize(c=String.new)
95
- super()
96
- @command = c
97
- end
55
+ def initialize(c=String.new)
56
+ super()
57
+ @command = c
58
+ end
98
59
 
99
- def compile
100
- @command.compile + compile_options + ";\n"
101
- end
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
- attr :x
109
- attr :y
69
+ attr :x
70
+ attr :y
110
71
 
111
- #Can set the value and individual scales of the x and y point
112
- def initialize(x=0,y=0)
113
- super()
114
- @x = x
115
- @y = y
116
- end
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
- def compile
119
- '(' + @x.compile + ', ' + @y.compile + ')'
120
- end
79
+ def compile
80
+ '(' + @x.compile + ', ' + @y.compile + ')'
81
+ end
121
82
 
122
- #returns the addition of the pairs
123
- def +(p)
124
- Pair.new(@x + p.x, @y + p.y)
125
- end
83
+ #returns the addition of the pairs
84
+ def +(p)
85
+ Pair.new(@x + p.x, @y + p.y)
86
+ end
126
87
 
127
- #returns the subtraction of the pairs
128
- def -(p)
129
- Pair.new(@x - p.x, @y - p.y)
130
- end
88
+ #returns the subtraction of the pairs
89
+ def -(p)
90
+ Pair.new(@x - p.x, @y - p.y)
91
+ end
131
92
 
132
- #returns the pair multiplied by a scalar
133
- def *(n)
134
- Pair.new(@x*n, @y*n)
135
- end
93
+ #returns the pair multiplied by a scalar
94
+ def *(n)
95
+ Pair.new(@x*n, @y*n)
96
+ end
136
97
 
137
- #returns the pair divided by a scalar
138
- def /(n)
139
- Pair.new(@x/n, @y/n)
140
- end
98
+ #returns the pair divided by a scalar
99
+ def /(n)
100
+ Pair.new(@x/n, @y/n)
101
+ end
141
102
 
142
- #returns true if the pairs are equal
143
- def ==(p)
144
- @x == p.x && @y == p.y
145
- end
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
- def initialize
153
- super
154
- @p = Array.new
155
- straight
156
- end
113
+ attr_writer :line_type
114
+
115
+ def initialize
116
+ super()
117
+ @p = Array.new
118
+ straight
119
+ end
157
120
 
158
- def add_pair(p)
159
- @p.push(p)
160
- end
121
+ def add_pair(p)
122
+ @p.push(p)
123
+ end
161
124
 
162
- #returns a pair that is the centroid of the pairs
163
- #of this path
164
- def center(p)
165
- ret = Pair.new
166
- @p.each { |p| ret = ret + p }
167
- return ret/p.length
168
- end
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
- #reverse the path. <br>
171
- #Note, this reverses the pairs that have so far
172
- #been added. Anything added after calling reverse
173
- #will be appended to the end of the array as usual
174
- def reverse
175
- @p = @p.reverse
176
- self
177
- end
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
- #set the path to have straight line segments
180
- def straight
181
- @line_type = '--'
182
- self
183
- end
142
+ #set the path to have straight line segments
143
+ def straight
144
+ @line_type = '--'
145
+ self
146
+ end
184
147
 
185
- #set the path to have curved line segments
186
- def curved
187
- @line_type = '..'
188
- self
189
- end
148
+ #set the path to have curved line segments
149
+ def curved
150
+ @line_type = '..'
151
+ self
152
+ end
190
153
 
191
- def compile
192
- str = '('
193
- (@p.length-1).times do |i|
194
- str = str + @p[i].compile + @line_type
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 the metapost fullcircle command
218
- class Circle < Drawable
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
- def compile
225
- 'fullcircle ' + compile_options + ";\n"
226
- end
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