-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathview.rb
More file actions
156 lines (112 loc) · 3.65 KB
/
view.rb
File metadata and controls
156 lines (112 loc) · 3.65 KB
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
Shoes.setup do
gem 'rbtree'
#gem 'ruby-debug'
end
require 'rbtree'
require 'clusterer.rb'
require 'classes.rb'
WIDTH = 800
HEIGHT= 600
BORDER = 10
MARKER_RADIUS = 7
CLUSTER_RADIUS = 30
#$points = PointReader::random(500)
$points = PointReader::read('data.csv')
#$points.each{|p| puts p}
$canvas = PointCanvas.new(WIDTH,HEIGHT,$points)
#$clusterer = Clusterer.new($points, 6)
$clusterer = Clusterer2.new($canvas)
#$clusterer.clusterize_bottom_up(6)
puts "maxX:#{$canvas.max_x} minX:#{$canvas.min_x} maxY:#{$canvas.max_y} minY:#{$canvas.min_y}"
Shoes.app(:width => WIDTH + BORDER * 2, :height => HEIGHT + BORDER * 2 + 50, :resizable => false) do
@cluster_width = CLUSTER_RADIUS
@cluster_height = CLUSTER_RADIUS
stack do
@menu = flow do
para "set radius of degrees to cluster within"
@limit_field = edit_line :width => 40
@limit_field.text = 4
button "Go!" do
@map.clear
$clusterer.reset
limit = @limit_field.text.to_i
@cluster_width = $canvas.lng_to_px(limit)
@cluster_height = $canvas.lat_to_px(limit)
#$clusterer.clusterize_bottom_up(limit)
$clusterer.clusterize(limit)
draw_clusters
#attach_listers_to_map
end
@status_text = para :width => 200,
:attach => Window,
:left => WIDTH - 200,
:align => 'right'
@status_text.text = "click on clusters"
end
@map = flow do
#para "map"
end
end
def draw_point(point, color)
x, y = $canvas.to_xy(point)
#puts "#{x} #{y} #{color}"
#fill color
#stroke_width 0
@map.stroke color
@map.strokewidth 0
@map.fill color
@map.oval x + BORDER,y + BORDER,MARKER_RADIUS, {:center=>true}
end
@drawn_cluster_centers = []
def draw_cluster(cluster)
x, y = $canvas.to_xy(cluster)
#puts "drawing cluster"
@map.stroke rgb(255,0,0,0.1)
@map.strokewidth 0
@map.fill rgb(255,0,0,0.2)
@map.oval :left => x + BORDER,
:top => y + BORDER,
:width => @cluster_width,
:height => @cluster_height,
:center=> true
clr = rgb(rand,rand,rand,1.0)
cluster.points.each{|point| draw_point(point, clr) }
@drawn_cluster_centers << {:left => x + BORDER, :top => y + BORDER, :size => cluster.points.length}
end
def draw_clusters
@drawn_cluster_centers = []
$clusterer.clusters.each do |cluster|
puts "cluster lat:#{cluster.lat} lng:#{cluster.lng}"
draw_cluster(cluster)
end
end
def draw_all_points(points)
$points.each do |point|
x, y = $canvas.to_xy(point)
draw_point(x,y, rgb(rand, rand, rand, 1))
end
end
#use clicks to show information about the clusters
click do |button, left, top|
#relativize clicks to map pane
left -= @map.left
top -= @map.top
#find if we're in a cluster, AFAIK you cant bind mouse events to art objects
results = @drawn_cluster_centers.select do |pos_hash|
#puts "#{pos_hash[:top]} #{pos_hash[:left]} top:#{top} left:#{left} w:#{@cluster_width} h:#{@cluster_height}"
if left > pos_hash[:left] - @cluster_width / 2 &&
left < pos_hash[:left] + @cluster_width / 2 &&
top > pos_hash[:top] - @cluster_height / 2 &&
top < pos_hash[:top] + @cluster_height / 2
pos_hash
else
nil
end
end
result = results.first
if result
@status_text.text = "#{result[:size]} nodes"
end
end
draw_clusters
end