Module: Scratchpad

Included in:
Pen, SheetView
Defined in:
lib/scratchpad.rb,
lib/scratchpad/version.rb,
lib/scratchpad/interpolator.rb

Defined Under Namespace

Classes: Interpolator, MainWindow, Pen, Program, SheetModel, SheetView

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#chess_distance(p1, p2) ⇒ Object



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

def chess_distance(p1, p2)
  x1, y1 = p1
  x2, y2 = p2

  return [(x1 - x2).abs, (y1 - y2).abs].max
end

#color_bytes_to_floats(bytes) ⇒ Object



49
50
51
52
53
54
# File 'lib/scratchpad.rb', line 49

def color_bytes_to_floats(bytes)
  raise 'wrong range' unless bytes.all? { |x| x >= 0 && x <= 255 }
  raise 'wrong dimension' unless bytes.size.between?(3, 4)

  return bytes.map { |b| b / 255.0 }
end

#distance(p1, p2) ⇒ Object

便利関数



28
29
30
31
32
33
# File 'lib/scratchpad.rb', line 28

def distance(p1, p2)
  x1, y1 = p1
  x2, y2 = p2

  return Math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
end

#dot_product(v1, v2) ⇒ Object



64
65
66
# File 'lib/scratchpad.rb', line 64

def dot_product(v1, v2)
  return v1.x * v2.x + v1.y * v2.y
end

#midpoint(p1, p2) ⇒ Object



42
43
44
45
46
47
# File 'lib/scratchpad.rb', line 42

def midpoint(p1, p2)
  x1, y1 = p1
  x2, y2 = p2

  return [(x1 + x2) * 0.5, (y1 + y2) * 0.5]
end

#plus(v1, v2) ⇒ Object



56
57
58
# File 'lib/scratchpad.rb', line 56

def plus(v1, v2)
  [v1.x + v2.x, v1.y + v2.y]
end

#scalar_times(factor, v) ⇒ Object



68
69
70
# File 'lib/scratchpad.rb', line 68

def scalar_times(factor, v)
  v.map { |x| factor * x }
end

#vec_lerp(alpha, v1, v2) ⇒ Object



72
73
74
# File 'lib/scratchpad.rb', line 72

def vec_lerp(alpha, v1, v2)
  return plus(scalar_times(alpha, v1), scalar_times(1.0-alpha, v2))
end

#vector(p1, p2) ⇒ Object



60
61
62
# File 'lib/scratchpad.rb', line 60

def vector(p1, p2)
  [p2.x - p1.x, p2.y - p1.y]
end