Class: StdDraw
- Inherits:
-
Object
- Object
- StdDraw
- Defined in:
- lib/standard_draw_tk.rb
Constant Summary collapse
- @@pen_radius =
DEFAULT_PEN_RADIUS = 0.002
- @@coords =
StandardDrawTk::Coordinates.new
- @@color =
'black'- @@bg_color =
'white'- @@root =
TkRoot.new
- @@canvas =
TkCanvas.new(@@root) do height @@coords.height width @@coords.width bg @@bg_color end
- @@thread =
Thread.new do Tk.mainloop end
Class Method Summary collapse
- .arc(x, y, radius, angle1, angle2) ⇒ Object
- .circle(x, y, radius) ⇒ Object
- .color ⇒ Object
- .coords ⇒ Object
- .ellipse(x, y, w, h) ⇒ Object
- .filled_circle(x, y, radius) ⇒ Object
- .filled_ellipse(x, y, w, h) ⇒ Object
- .filled_polygon(x, y) ⇒ Object
- .filled_rectangle(x, y, half_width, half_height) ⇒ Object
- .filled_square(x, y, half_length) ⇒ Object
- .line(x0, y0, x1, y1) ⇒ Object
- .pause ⇒ Object
- .pen_color=(color) ⇒ Object
- .pen_radius=(radius) ⇒ Object
- .pixel(x, y) ⇒ Object
- .point(x, y) ⇒ Object
- .polygon(x, y) ⇒ Object
- .rectangle(x, y, half_width, half_height) ⇒ Object
- .set_xscale(min, max) ⇒ Object
- .set_yscale(min, max) ⇒ Object
- .square(x, y, half_length) ⇒ Object
Class Method Details
.arc(x, y, radius, angle1, angle2) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/standard_draw_tk.rb', line 58 def self.arc(x, y, radius, angle1, angle2) throw 'arc radius must be nonnegative' if radius < 0 while angle2 < angle1 angle2 += 360 end xs = coords.scale_x(x) ys = coords.scale_y(y) ws = coords.factor_x(2*radius) hs = coords.factor_y(2*radius) if ws <= 1 && hs <= 1 pixel(x, y) else x1 = xs - ws/2 y1 = ys - hs/2 x2 = x1 + ws y2 = y1 + hs TkcArc.new(@@canvas, x1, y1, x2, y2, style: :arc, start: angle1, extent: angle2 - angle1, fill: color, width: scaled_pen_radius, outline: color) end end |
.circle(x, y, radius) ⇒ Object
78 79 80 |
# File 'lib/standard_draw_tk.rb', line 78 def self.circle(x, y, radius) draw_circle(x, y, radius, outline: color) end |
.color ⇒ Object
46 47 48 |
# File 'lib/standard_draw_tk.rb', line 46 def self.color @@color end |
.coords ⇒ Object
25 26 27 |
# File 'lib/standard_draw_tk.rb', line 25 def self.coords @@coords end |
.ellipse(x, y, w, h) ⇒ Object
82 83 84 |
# File 'lib/standard_draw_tk.rb', line 82 def self.ellipse(x, y, w, h) draw_ellipse(x, y, w, h, outline: color) end |
.filled_circle(x, y, radius) ⇒ Object
90 91 92 |
# File 'lib/standard_draw_tk.rb', line 90 def self.filled_circle(x, y, radius) draw_circle(x, y, radius, fill: color) end |
.filled_ellipse(x, y, w, h) ⇒ Object
86 87 88 |
# File 'lib/standard_draw_tk.rb', line 86 def self.filled_ellipse(x, y, w, h) draw_ellipse(x, y, w, h, fill: color) end |
.filled_polygon(x, y) ⇒ Object
128 129 130 |
# File 'lib/standard_draw_tk.rb', line 128 def self.filled_polygon(x, y) draw_polygon(x, y, fill: color) end |
.filled_rectangle(x, y, half_width, half_height) ⇒ Object
98 99 100 |
# File 'lib/standard_draw_tk.rb', line 98 def self.filled_rectangle(x, y, half_width, half_height) draw_rectangle(x, y, half_width, half_height, fill: color) end |
.filled_square(x, y, half_length) ⇒ Object
106 107 108 |
# File 'lib/standard_draw_tk.rb', line 106 def self.filled_square(x, y, half_length) draw_rectangle(x, y, half_length, half_length, fill: color) end |
.line(x0, y0, x1, y1) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/standard_draw_tk.rb', line 50 def self.line(x0, y0, x1, y1) x0 = coords.scale_x(x0) y0 = coords.scale_y(y0) x1 = coords.scale_x(x1) y1 = coords.scale_y(y1) TkcLine.new(@@canvas, x0, y0, x1, y1, width: scaled_pen_radius, fill: color) end |
.pause ⇒ Object
120 121 122 |
# File 'lib/standard_draw_tk.rb', line 120 def self.pause @@thread.join end |
.pen_color=(color) ⇒ Object
42 43 44 |
# File 'lib/standard_draw_tk.rb', line 42 def self.pen_color=(color) @@color = color end |
.pen_radius=(radius) ⇒ Object
37 38 39 40 |
# File 'lib/standard_draw_tk.rb', line 37 def self.pen_radius=(radius) throw 'pen radius must be nonnegative' if radius <= 0 @@pen_radius = radius end |
.pixel(x, y) ⇒ Object
114 115 116 117 118 |
# File 'lib/standard_draw_tk.rb', line 114 def self.pixel(x, y) x1 = coords.scale_x(x).round y1 = coords.scale_y(y).round TkcRectangle.new(@@canvas, [x1, y1, x1 + 1, y1 + 1], outline: color, fill: color) end |
.point(x, y) ⇒ Object
110 111 112 |
# File 'lib/standard_draw_tk.rb', line 110 def self.point(x, y) draw_circle(x, y, @@pen_radius/2, outline: color, fill: color) end |
.polygon(x, y) ⇒ Object
124 125 126 |
# File 'lib/standard_draw_tk.rb', line 124 def self.polygon(x, y) draw_polygon(x, y, outline: color) end |
.rectangle(x, y, half_width, half_height) ⇒ Object
94 95 96 |
# File 'lib/standard_draw_tk.rb', line 94 def self.rectangle(x, y, half_width, half_height) draw_rectangle(x, y, half_width, half_height, outline: color) end |
.set_xscale(min, max) ⇒ Object
29 30 31 |
# File 'lib/standard_draw_tk.rb', line 29 def self.set_xscale(min,max) coords.set_xscale(min,max) end |
.set_yscale(min, max) ⇒ Object
33 34 35 |
# File 'lib/standard_draw_tk.rb', line 33 def self.set_yscale(min,max) coords.set_yscale(min,max) end |
.square(x, y, half_length) ⇒ Object
102 103 104 |
# File 'lib/standard_draw_tk.rb', line 102 def self.square(x, y, half_length) draw_rectangle(x, y, half_length, half_length, outline: color) end |