Class: StandardDrawTk::Coordinates

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

Constant Summary collapse

BORDER =
0.00
DEFAULT_MIN =
0.0
DEFAULT_MAX =
1.0
DEFAULT_SIZE =
512

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoordinates

Returns a new instance of Coordinates.



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

def initialize
  @height = @width = DEFAULT_SIZE
  @xmin = @ymin = DEFAULT_MIN
  @xmax = @ymax = DEFAULT_MAX
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#box(x, y, r) ⇒ Object



44
45
46
# File 'lib/coordinates.rb', line 44

def box(x, y, r)
  [x - r, y - r, x + r, y + r]
end

#factor_x(w) ⇒ Object



28
29
30
# File 'lib/coordinates.rb', line 28

def factor_x(w)
  w * @width  / (@xmax - @xmin).abs
end

#factor_y(h) ⇒ Object



32
33
34
# File 'lib/coordinates.rb', line 32

def factor_y(h)
  h * @height / (@ymax - @ymin).abs
end

#initObject



15
16
17
18
# File 'lib/coordinates.rb', line 15

def init
  set_xscale(DEFAULT_XMIN, DEFAULT_XMAX)
  set_yscale(DEFAULT_YMIN, DEFAULT_YMAX)
end

#scale_x(x) ⇒ Object



20
21
22
# File 'lib/coordinates.rb', line 20

def scale_x(x)
  @width  * (x - @xmin) / (@xmax - @xmin)
end

#scale_y(y) ⇒ Object



24
25
26
# File 'lib/coordinates.rb', line 24

def scale_y(y)
  @height * (@ymax - y) / (@ymax - @ymin)
end

#set_xscale(min, max) ⇒ Object



48
49
50
51
52
53
# File 'lib/coordinates.rb', line 48

def set_xscale(min, max)
  size = max - min
  throw 'the min and max are the same' if size == 0.0
  @xmin = min - BORDER * size
  @xmax = max + BORDER * size
end

#set_yscale(min, max) ⇒ Object



55
56
57
58
59
60
# File 'lib/coordinates.rb', line 55

def set_yscale(min, max)
  size = max - min
  throw 'the min and max are the same' if size == 0.0
  @ymin = min - BORDER * size
  @ymax = max + BORDER * size
end

#user_x(x) ⇒ Object



36
37
38
# File 'lib/coordinates.rb', line 36

def user_x(x)
  @xmin + x * (@xmax - @xmin) / @width
end

#user_y(y) ⇒ Object



40
41
42
# File 'lib/coordinates.rb', line 40

def user_y(y)
  @ymax - y * (@ymax - @ymin) / @height
end