-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtabla_exec.rb
237 lines (203 loc) · 4.99 KB
/
tabla_exec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env ruby
# Proyecto CI3725 Traductores e Interpretadores
# Adolfo Jeritson. 12-10523
#
# Definición de la tabla de símbolos usada en la ejecución
#
require_relative 'errores'
# Añadimos conversion a radianos y grados al modulo Math
module Math
def self.to_rad(degrees)
(degrees * Math::PI) / 180
end
def self.to_deg(radians)
radians * (180 / Math::PI)
end
end
class ExecTabla
attr_reader :parent, :tabla
def initialize(parent=nil)
@tabla = {}
@parent = parent
end
def addFunc(var)
@tabla[var.to_sym] = { :cuerpo => nil, :params => nil }
end
def updateFunc(var, cuerpo)
func = find(var.to_sym)
func[:cuerpo] = cuerpo
end
def addParams(var, params)
func = find(var.to_sym)
func[:params] = params
end
def addVal(var)
@tabla[var.to_sym] = { :val => nil }
end
def updateVal(var, value)
key = find(var.to_sym)
key[:val] = value
end
# Retorna la entrada más inmediata del simbolo a buscar
def find(key)
key = key.to_sym
if @tabla.has_key?(key)
return @tabla[key]
elsif [email protected]?
return @parent.find(key)
else
return nil
end
end
end
class MatrizPBM
def initialize()
#temp = Array.new(11, 0)
@m = Array.new(1001) {Array.new(1001, 0)}
@m[500][500] = 1
@actual_x = 500
@actual_y = 500
@grados = 90
@write = true
end
def save_to_file()
$file.match(/(\w+)\.rtn/)
File.open("#{$1}.pbm", "w") do |f|
@m.each { |arr| f.write("#{arr}\n")}
end
end
def save_to_img()
$file.match(/(\w+)\.rtn/)
pixels = @m
image = PNM.create(pixels, :type=>:pbm)
image.write("#{$1}.pbm")
end
def openeye()
@write = true
end
def closeeye()
@write = false
end
# Dada la posicion actual y el grado a moverse, retorna
# los valores en y correspondientes
def recta(m, x, x_o, y_o)
y = m*(x-x_o)+y_o
return y.floor
end
def forward(n)
#puts @grados
if @grados == 90
n.times do
@actual_y -= 1
begin
(@m[@actual_y][@actual_x] = 1) if @write
rescue NoMethodError
end
end
elsif @grados == 270
n.times do
@actual_y += 1
begin
(@m[@actual_y][@actual_x] = 1) if @write
rescue NoMethodError
end
end
else
m = Math.tan(Math.to_rad(@grados))
#puts "m: #{m}"
# Calculamos Y por trigonometria
dist_x = (Math.cos(Math.to_rad(@grados)) * n).round.abs
#puts "dx: #{dist_x}"
x_o = @actual_x
y_o = @actual_y
for x in 1..(dist_x)
dist_y = (Math.tan(Math.to_rad(@grados)) * x).round
#y = recta(m, x, x_o, y_o).round
#puts "tan: #{dist_y}, recta: #{y}, x: #{x}"
if @grados >= 0 and @grados <=90
# PRIMER CUADRANTE
@actual_y=y_o-dist_y
@actual_x=x_o+x
elsif @grados > 90 and @grados <= 180
# SEGUNDO CUADRANTE
@actual_y=y_o+dist_y
@actual_x=x_o-x
elsif @grados > 180 and @grados <= 270
# TERCER CUADRANTE
@actual_y=y_o+dist_y
@actual_x=x_o-x
elsif @grados > 270 and @grados < 360
# CUARTO CUADRANTE
@actual_y=y_o-dist_y
@actual_x=x_o+x
end
begin
(@m[@actual_y][@actual_x] = 1) if @write
rescue NoMethodError
end
end
end
end
def backward(n)
if @grados == 90
n.times do
@actual_y += 1
begin
(@m[@actual_y][@actual_x] = 1) if @write
rescue NoMethodError
end
end
elsif @grados == 270
n.times do
@actual_y -= 1
begin
(@m[@actual_y][@actual_x] = 1) if @write
rescue NoMethodError
end
end
else
m = Math.tan(Math.to_rad(@grados))
#puts "m: #{m}"
dist_x = (Math.cos(Math.to_rad(@grados)) * n).round.abs
#puts "dx: #{dist_x}"
x_o = @actual_x
y_o = @actual_y
for x in 1..(dist_x)
dist_y = (Math.tan(Math.to_rad(@grados)) * x).round
#y = recta(m, x, x_o, y_o).round
#puts "tan: #{dist_y}, recta: #{y}, x: #{x}"
if @grados >= 0 and @grados <=90
# PRIMER CUADRANTE
@actual_y=y_o-dist_y
@actual_x=x_o+x
elsif @grados > 90 and @grados <= 180
# SEGUNDO CUADRANTE
@actual_y=y_o+dist_y
@actual_x=x_o-x
elsif @grados > 180 and @grados <= 270
# TERCER CUADRANTE
@actual_y=y_o+dist_y
@actual_x=x_o-x
elsif @grados > 270 and @grados < 360
# CUARTO CUADRANTE
@actual_y=y_o-dist_y
@actual_x=x_o+x
end
begin
(@m[@actual_y][@actual_x] = 1) if @write
rescue NoMethodError
end
end
end
end
def rotater(n)
@grados = ((@grados-n) % 360).round
end
def rotatel(n)
@grados = ((@grados+n) % 360).round
end
def set_pos(x, y)
@actual_x = x + 500
@actual_y = y + 500
end
end