-
Notifications
You must be signed in to change notification settings - Fork 8
/
centroide.rb
executable file
·63 lines (46 loc) · 935 Bytes
/
centroide.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
#!/usr/bin/ruby
require 'csv'
require 'ostruct'
def centroide v
n = v.length
v << v[0] # El último lo pongo al final así lo puedo acceder como el n-ésimo.
a = 0
for i in 0..(n-1)
a += v[i].x*v[i+1].y - v[i+1].x*v[i].y
end
a *= 0.5
c = OpenStruct.new
c.x = 0
for i in 0..(n-1)
c.x += (v[i].x + v[i+1].x)*(v[i].x*v[i+1].y - v[i+1].x*v[i].y)
end
c.x /= 6*a
c.y = 0
for i in 0..(n-1)
c.y += (v[i].y + v[i+1].y)*(v[i].x*v[i+1].y - v[i+1].x*v[i].y)
end
c.y /= 6*a
c
end
i = 0
shapes = []
shapes[0] = []
CSV.foreach(ARGV[0]) do |row|
unless row[0].eql? "shapeid" # ignoro cabezal
shapeid = row[0].to_i
x = row[1].to_f
y = row[2].to_f
if shapeid > i # cambio de figura
c = centroide(shapes[i])
puts "#{c.x},#{c.y}"
i += 1
shapes[i] = []
end
vertice = OpenStruct.new
vertice.x = x
vertice.y = y
shapes[i] << vertice
end
end
c = centroide(shapes[i])
puts "#{c.x},#{c.y}"