class StdDraw

Constants

BLACK
BLUE
BOOK_BLUE
BOOK_LIGHT_BLUE
BOOK_RED
CYAN
DARK_GRAY
DEFAULT_PEN_RADIUS
GRAY
GREEN
LIGHT_GRAY
MAGENTA
ORANGE
PINK
PRINCETON_ORANGE
RED
WHITE
YELLOW

Public Class Methods

arc(x, y, radius, angle1, angle2) click to toggle source
# File lib/standard_draw_tk.rb, line 82
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) click to toggle source
# File lib/standard_draw_tk.rb, line 102
def self.circle(x, y, radius)
  draw_circle(x, y, radius, outline: color)
end
color() click to toggle source
# File lib/standard_draw_tk.rb, line 70
def self.color
  @@color
end
coords() click to toggle source
# File lib/standard_draw_tk.rb, line 49
def self.coords
  @@coords
end
ellipse(x, y, w, h) click to toggle source
# File lib/standard_draw_tk.rb, line 106
def self.ellipse(x, y, w, h)
  draw_ellipse(x, y, w, h, outline: color)
end
filled_circle(x, y, radius) click to toggle source
# File lib/standard_draw_tk.rb, line 114
def self.filled_circle(x, y, radius)
  draw_circle(x, y, radius, fill: color)
end
filled_ellipse(x, y, w, h) click to toggle source
# File lib/standard_draw_tk.rb, line 110
def self.filled_ellipse(x, y, w, h)
  draw_ellipse(x, y, w, h, fill: color)
end
filled_polygon(x, y) click to toggle source
# File lib/standard_draw_tk.rb, line 148
def self.filled_polygon(x, y)
  draw_polygon(x, y, fill: color)
end
filled_rectangle(x, y, half_width, half_height) click to toggle source
# File lib/standard_draw_tk.rb, line 122
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) click to toggle source
# File lib/standard_draw_tk.rb, line 130
def self.filled_square(x, y, half_length)
  draw_rectangle(x, y, half_length, half_length, fill: color)
end
line(x0, y0, x1, y1) click to toggle source
# File lib/standard_draw_tk.rb, line 74
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() click to toggle source

should make this real

# File lib/standard_draw_tk.rb, line 45
def self.pause
  Tk.mainloop
end
pen_color=(color) click to toggle source
# File lib/standard_draw_tk.rb, line 66
def self.pen_color=(color)
  @@color = color
end
pen_radius=(radius) click to toggle source
# File lib/standard_draw_tk.rb, line 61
def self.pen_radius=(radius)
  throw 'pen radius must be nonnegative' if radius <= 0
  @@pen_radius = radius
end
pixel(x, y) click to toggle source
# File lib/standard_draw_tk.rb, line 138
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) click to toggle source
# File lib/standard_draw_tk.rb, line 134
def self.point(x, y)
  draw_circle(x, y, @@pen_radius/2, outline: color, fill: color)
end
polygon(x, y) click to toggle source
# File lib/standard_draw_tk.rb, line 144
def self.polygon(x, y)
  draw_polygon(x, y, outline: color)
end
rectangle(x, y, half_width, half_height) click to toggle source
# File lib/standard_draw_tk.rb, line 118
def self.rectangle(x, y, half_width, half_height)
  draw_rectangle(x, y, half_width, half_height, outline: color)
end
run() click to toggle source
# File lib/standard_draw_tk.rb, line 40
def self.run
  Tk.mainloop
end
set_xscale(min,max) click to toggle source
# File lib/standard_draw_tk.rb, line 53
def self.set_xscale(min,max)
  coords.set_xscale(min,max)
end
set_yscale(min,max) click to toggle source
# File lib/standard_draw_tk.rb, line 57
def self.set_yscale(min,max)
  coords.set_yscale(min,max)
end
square(x, y, half_length) click to toggle source
# File lib/standard_draw_tk.rb, line 126
def self.square(x, y, half_length)
  draw_rectangle(x, y, half_length, half_length, outline: color)
end

Private Class Methods

draw_circle(x, y, radius, outline: nil, fill: nil) click to toggle source
# File lib/standard_draw_tk.rb, line 187
def self.draw_circle(x, y, radius, outline: nil, fill: nil)
  throw 'radius must be nonnegative' if radius < 0
  xs = coords.scale_x(x)
  ys = coords.scale_y(y)
  scaled_radius = radius * StandardDrawTk::Coordinates::DEFAULT_SIZE
  if scaled_pen_radius <= 1
    pixel(x, y)
  else
    b = coords.box(xs, ys, scaled_radius)
    TkcOval.new(@@canvas, b, outline: outline, fill: fill)
  end
end
draw_ellipse(x, y, w, h, outline: nil, fill: nil) click to toggle source
# File lib/standard_draw_tk.rb, line 169
def self.draw_ellipse(x, y, w, h, outline: nil, fill: nil)
  throw 'semi_major_axis must be nonnegative' if w < 0
  throw 'semi_minor_axis must be nonnegative' if h < 0

  xs = coords.scale_x(x)
  ys = coords.scale_y(y)
  ws = coords.factor_x(2*w)
  hs = coords.factor_y(2*h)
  if ws <= 1 && hs <= 1
    pixel(x, y)
  else
    x1 = xs - ws/2
    y1 = ys - hs/2
    b = [x1, y1, x1 + ws, y1 + hs]
    TkcOval.new(@@canvas, b, outline: outline, fill: fill)
  end
end
draw_polygon(x, y, fill: '', outline: nil) click to toggle source
# File lib/standard_draw_tk.rb, line 158
def self.draw_polygon(x, y, fill: '', outline: nil)
  throw 'x-coordinate array is null' if x.nil?
  throw 'y-coordinate array is null' if y.nil?
  throw 'arrays must be of the same length' if x.size != y.size
  return if x.empty?
  x = x.map{|i| coords.scale_x(i)}
  y = y.map{|i| coords.scale_y(i)}
  points = x.zip(y).flatten
  TkcPolygon.new(@@canvas, *points, width: scaled_pen_radius, fill: fill, outline: outline)
end
draw_rectangle(x, y, half_width, half_height, outline: nil, fill: nil) click to toggle source
# File lib/standard_draw_tk.rb, line 200
def self.draw_rectangle(x, y, half_width, half_height, outline: nil, fill: nil)
  throw 'half width must be nonnegative' if half_width < 0
  throw 'half height must be nonnegative' if half_height < 0

  ws = coords.factor_x(2*half_width)
  hs = coords.factor_y(2*half_height)

  if (ws <= 1 && hs <= 1)
    pixel(x, y)
  else
    xs = coords.scale_x(x)
    ys = coords.scale_y(y)
    x = xs - ws/2
    y = ys - hs/2
    TkcRectangle.new(@@canvas, x, y, x + (ws-1), y + (hs-1), outline: outline, fill: fill)
  end
end
scaled_pen_radius() click to toggle source
# File lib/standard_draw_tk.rb, line 154
def self.scaled_pen_radius
  @@pen_radius * StandardDrawTk::Coordinates::DEFAULT_SIZE
end