Class: Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/screen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transform, artist, basis, legend_box = LegendBox.new(artist), output = TextOutput.new) ⇒ Screen

Returns a new instance of Screen.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/screen.rb', line 15

def initialize(transform, artist, basis, legend_box=LegendBox.new(artist), output=TextOutput.new)
	@legend_box = legend_box
	@transform = transform
	@artist = artist
	@basis = basis
	join = false
	@points = []
	@data = []
	@output = output
	@transform_matrix = Matrix.rows([[@transform.scale[:x], 0],[0, @transform.scale[:y]]])
	nhm = @transform_matrix* @basis.basis_matrix
	@affine_transform = PMatrix2D.new(nhm[0,0],nhm[0,1],@transform.origin[:x],nhm[1,0],nhm[1,1],@transform.origin[:y])
	@inverse_affine_transform = @affine_transform.get
	@inverse_affine_transform.invert
end

Instance Attribute Details

#pointsObject

Returns the value of attribute points.



7
8
9
# File 'lib/screen.rb', line 7

def points
  @points
end

Instance Method Details

#at(point, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/screen.rb', line 43

def at(point, &block)
	if (!point[:x] || !point[:y])
		@artist.reset_matrix
		block.call(point, self) if block
		return
	end
	p = transformed(point)
	if (block)
		@artist.reset_matrix
		block.call(point, p, self)
	end
end

#buildObject



31
32
33
# File 'lib/screen.rb', line 31

def build
	KnnBall.build(@data)
end

#draw_axes(x_interval, y_interval, options = {}) ⇒ Object



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
# File 'lib/screen.rb', line 128

def draw_axes(x_interval, y_interval, options = {})
	f = @artist.createFont("Georgia", 24, true);
	@artist.text_font(f,16)
	axis_screen_transform = Transform.new({:x => 800, :y => -800}, @transform.origin)
	origin = {:x => 0, :y => 0}
	screen_origin = @transform.apply(origin)
	x_basis_edge = axis_screen_transform.apply(@basis.x_basis_vector)
	y_basis_edge = axis_screen_transform.apply(@basis.y_basis_vector)

	x_ticks = @basis.x_ticks(x_interval)
	y_ticks = @basis.y_ticks(y_interval)

	x_ticks = x_ticks.collect {|t| {:from => @transform.apply(t[:from]), :to => @transform.apply(t[:to]), :label => t[:label]}}
	y_ticks = y_ticks.collect {|t| {:from => @transform.apply(t[:from]), :to => @transform.apply(t[:to]), :label => t[:label]}}

	@artist.line(x_ticks.first[:from][:x],x_ticks.first[:from][:y],x_ticks.last[:from][:x],x_ticks.last[:from][:y])
	@artist.line(y_ticks.first[:from][:x],y_ticks.first[:from][:y],y_ticks.last[:from][:x],y_ticks.last[:from][:y])

	x_axis_label_position = {:x => (x_ticks.first[:from][:x] + x_ticks.last[:from][:x])/2, :y => (x_ticks.first[:from][:y] + x_ticks.last[:from][:y])/2 + 50}
	y_axis_label_position = {:x => (y_ticks.first[:from][:x] + y_ticks.last[:from][:x])/2 - 60, :y => (y_ticks.first[:from][:y] + y_ticks.last[:from][:y])/2}
	
	@artist.push_matrix
	@artist.translate(x_axis_label_position[:x], x_axis_label_position[:y])
	@artist.rotate(2*Math::PI - Math.atan2(@basis.x_basis_vector[:y], @basis.x_basis_vector[:x]))
	@artist.text(@basis.x_basis_vector[:label], 0, 0)
	@artist.pop_matrix

	@artist.push_matrix
	@artist.translate(y_axis_label_position[:x], y_axis_label_position[:y])
	@artist.rotate(2*Math::PI - Math.atan2(@basis.y_basis_vector[:y], @basis.y_basis_vector[:x]))
	@artist.text(@basis.y_basis_vector[:label], 0, 0)
	@artist.pop_matrix
	
	draw_ticks(x_ticks, {:x => 0, :y => 20}, options[:x])
	draw_ticks(y_ticks, {:x => -50, :y => 0}, options[:y])
	
	@artist.stroke(0.4, 1.0, 0.5, 0.2)
	return if !options[:gridlines].nil? && options[:gridlines] == false
	grid_lines = @basis.grid_lines(x_interval, y_interval).collect {|gl| {:from => @transform.apply(gl[:from]), :to => @transform.apply(gl[:to])}}
	grid_lines.each do |l|
		@artist.line(l[:from][:x],l[:from][:y],l[:to][:x],l[:to][:y])
	end
end

#draw_crosshairs(p) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/screen.rb', line 35

def draw_crosshairs(p)
	@basis.crosshairs(p).each do |hair|
		from = @transform.apply(hair[:from])
		to = @transform.apply(hair[:to])
		@artist.line(from[:x], from[:y], to[:x], to[:y])
	end
end

#in_basis(&blk) ⇒ Object



89
90
91
92
93
# File 'lib/screen.rb', line 89

def in_basis(&blk)
	@artist.apply_matrix(@affine_transform)
	blk.call
	@artist.reset_matrix
end

#join=(should_join) ⇒ Object



10
11
12
13
# File 'lib/screen.rb', line 10

def join=(should_join)
	@should_join = should_join
	@buffer = nil if !@should_join
end

#original(onscreen_point) ⇒ Object



105
106
107
108
# File 'lib/screen.rb', line 105

def original(onscreen_point)
	transformed_p = @inverse_affine_transform.mult([onscreen_point[:x], onscreen_point[:y]].to_java(:float), nil)
	{:x => transformed_p[0], :y => transformed_p[1]}
end

#outside_basis(&blk) ⇒ Object



95
96
97
98
# File 'lib/screen.rb', line 95

def outside_basis(&blk)
	@artist.reset_matrix
	blk.call
end

#plot(points, options = {:bar => false, :track => false, :legend => ''}, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/screen.rb', line 56

def plot(points, options = {:bar => false, :track => false, :legend => ''}, &block)
	if points.kind_of? Array
		points.each {|p| plot_single(p, options, &block)}
	else
		plot_single(points, options, &block)
	end
	return if options[:legend].nil?
	outside_basis do
		@legend_box.draw(options[:legend])
	end
end

#transformed(data_point) ⇒ Object



100
101
102
103
# File 'lib/screen.rb', line 100

def transformed(data_point)
	transformed_p = @affine_transform.mult([data_point[:x], data_point[:y]].to_java(:float), nil)
	{:x => transformed_p[0], :y => transformed_p[1]}
end

#write(p) ⇒ Object



172
173
174
# File 'lib/screen.rb', line 172

def write(p)
	@output.notify(p)
end