Module: Pathby::Transformations

Included in:
Pathy, Point, Shapy
Defined in:
lib/transformations.rb

Instance Method Summary collapse

Instance Method Details

#centerObject



81
82
83
84
# File 'lib/transformations.rb', line 81

def center
  min,max = simplebbox
  return Point.new((min.x + max.x)/2,(min.y+max.y) /2)
end

#reflect(about = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/transformations.rb', line 44

def reflect(about = nil)
  #rotate by 180 about
  #translate to make rotation point 0
  r = GTransMat.gRotateMatrix(Math::PI)
  if about
    r = GTransMat.gCenterAtMatrix(r,about)
  end
  transform(r)
end

#rezeroObject



86
87
88
89
90
91
# File 'lib/transformations.rb', line 86

def rezero
  min,max = simplebbox
  m = GTransMat.gTranslateMatrix(-min.x,-min.y)
  transform(m)
  return self
end

#rotate(r, about = nil) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/transformations.rb', line 62

def rotate(r,about=nil)
  s = GTransMat.gRotateMatrix(r)
  if about
    s = GTransMat.gCenterAtMatrix(s,about)
  end
  transform(s)
end

#scale(sx, sy, about = nil) ⇒ Object



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

def scale(sx,sy,about=nil)
  s = GTransMat.gScaleMatrix(sx,sy)
  if about
    s = GTransMat.gCenterAtMatrix(s,about)
  end
  transform(s)
end

#scale_to(width, height, lock_aspect = true) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/transformations.rb', line 93

def scale_to(width,height,lock_aspect=true)
  min,max = simplebbox
  current_width = max.x - min.x
  current_height = max.y - min.y
  sx = width/current_width
  sy = height/current_height
  if lock_aspect
    mins = [sx,sy].min
    sx,sy = mins,mins
  end
  scale(sx,sy)
end

#simplebboxObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/transformations.rb', line 70

def simplebbox
  points = allpoints

  maxx = points.max{|a,b| a.x <=> b.x}.x
  minx = points.min{|a,b| a.x <=> b.x}.x
  maxy = points.max{|a,b| a.y <=> b.y}.y
  miny = points.min{|a,b| a.y <=> b.y}.y
  #puts "min  (#{minx},#{miny}) max (#{maxx},#{maxy})"
  return [Point.new(minx,miny),Point.new(maxx,maxy)]
end

#transform(m) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/transformations.rb', line 32

def transform(m)
  case self
  when Point
    applytransform(m)
  else
    allpoints.each do |p|
      p.applytransform(m)
  end

  end
end