Class: HotCocoa::Graphics::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/hotcocoa/graphics/path.rb

Overview

Make a reusable path. Draw it using canvas.draw(path)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0, &block) ⇒ Path

create a new path, starting at optional x,y



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hotcocoa/graphics/path.rb', line 23

def initialize(x=0, y=0, &block)
  @path = CGPathCreateMutable()
  @transform = CGAffineTransformMakeTranslation(0,0)
  moveto(x, y)

  # set randomized rendering parameters
  @rand = {}
  randomize(:x, 0.0)
  randomize(:y, 0.0)
  randomize(:scale, 1.0)
  randomize(:scalex, 1.0)
  randomize(:scaley, 1.0)
  randomize(:rotation, 0.0)
  randomize(:strokewidth, 1.0)

  # set incremental rendering parameters
  @inc = {}
  increment(:rotation, 0.0)
  increment(:x, 0.0)
  increment(:y, 0.0)
  increment(:scale, 1.0)
  increment(:scalex, 1.0)
  increment(:scaley, 1.0)

  @strokewidth = 1.0
  @x = 0.0
  @y = 0.0

  if block
    case block.arity
    when 0
      self.send(:instance_eval, &block)
    else
      block.call(self)
    end
  end
  self
end

Instance Attribute Details

#fill(colors = nil) ⇒ Object

Returns the value of attribute fill.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def fill
  @fill
end

#imageObject

Returns the value of attribute image.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def image
  @image
end

#incObject

Returns the value of attribute inc.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def inc
  @inc
end

#pathObject

Returns the value of attribute path.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def path
  @path
end

#randObject

Returns the value of attribute rand.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def rand
  @rand
end

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

scale by horizontal/vertical scaling factors sx,sy for subsequent drawing operations



272
273
274
# File 'lib/hotcocoa/graphics/path.rb', line 272

def scale
  @scale
end

#strokeObject

Returns the value of attribute stroke.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def stroke
  @stroke
end

#strokewidthObject

Returns the value of attribute strokewidth.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def strokewidth
  @strokewidth
end

#xObject

Returns the value of attribute x.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def x
  @x
end

#yObject

Returns the value of attribute y.



20
21
22
# File 'lib/hotcocoa/graphics/path.rb', line 20

def y
  @y
end

Instance Method Details

#addpath(p) ⇒ Object

add another path to this path



147
148
149
# File 'lib/hotcocoa/graphics/path.rb', line 147

def addpath(p)
  CGPathAddPath(@path, @transform, p.path)
end

#arc(x, y, radius, start_angle, end_angle) ⇒ Object

draw the arc of a circle with center point x,y, radius, start angle (0 deg = 12 o'clock) and end angle



212
213
214
215
216
217
218
# File 'lib/hotcocoa/graphics/path.rb', line 212

def arc(x, y, radius, start_angle, end_angle)
  start_angle = radians(90 - start_angle)
  end_angle   = radians(90 - end_angle)
  clockwise   = 1 # 1 = clockwise, 0 = counterclockwise
  CGPathAddArc(@path, @transform, x, y, radius, start_angle, end_angle, clockwise)
  self
end

#arcto(x1, y1, x2, y2, radius) ⇒ Object

draw an arc given the endpoints of two tangent lines and a radius



253
254
255
256
# File 'lib/hotcocoa/graphics/path.rb', line 253

def arcto(x1, y1, x2, y2, radius)
  CGPathAddArcToPoint(@path, @transform, x1, y1, x2, y2, radius)
  self
end

#cloneObject

return a mutable clone of this path



81
82
83
84
85
# File 'lib/hotcocoa/graphics/path.rb', line 81

def clone
  newpath = self.dup
  newpath.path = CGPathCreateMutableCopy(@path)
  newpath
end

#contains(x, y) ⇒ Object

true if the path contains the current point # doesn't work?



138
139
140
141
# File 'lib/hotcocoa/graphics/path.rb', line 138

def contains(x,y)
  eorule = true
  CGPathContainsPoint(@path, @transform, CGPointMake(x, y), eorule)
end

#currentpointObject

return the current point



133
134
135
# File 'lib/hotcocoa/graphics/path.rb', line 133

def currentpoint
  CGPathGetCurrentPoint(@path)
end

#curveto(cp1x, cp1y, cp2x, cp2y, x, y) ⇒ Object

draw a bezier curve from the current point, given the coordinates of two handle control points and an end point



241
242
243
244
# File 'lib/hotcocoa/graphics/path.rb', line 241

def curveto(cp1x, cp1y, cp2x, cp2y, x,  y)
  CGPathAddCurveToPoint(@path, @transform, cp1x, cp1y, cp2x, cp2y, x, y)
  self
end

#endpathObject

end the current path



259
260
261
# File 'lib/hotcocoa/graphics/path.rb', line 259

def endpath
  CGPathCloseSubpath(@path)
end

#heightObject

return the height of the path's bounding box



128
129
130
# File 'lib/hotcocoa/graphics/path.rb', line 128

def height
  CGPathGetBoundingBox(@path).size.height
end

#increment(parameter, value) ⇒ Object

increment the specified parameter within the specified range



76
77
78
# File 'lib/hotcocoa/graphics/path.rb', line 76

def increment(parameter, value)
  inc[parameter] = value
end

#kaleidoscope(path, qty) ⇒ Object

duplicate and rotate the Path object the specified number of times



300
301
302
303
304
305
306
# File 'lib/hotcocoa/graphics/path.rb', line 300

def kaleidoscope(path,qty)
  deg = 360 / qty
  qty.times do
    addpath(path)
    rotate(deg)
  end
end

#line(x1, y1, x2, y2) ⇒ Object

draw a line from x1,x2 to x2,y2



206
207
208
209
# File 'lib/hotcocoa/graphics/path.rb', line 206

def line(x1, y1, x2, y2)
  CGPathAddLines(@path, @transform, [NSMakePoint(x1, y1), NSMakePoint(x2, y2)])
  self
end

#lines(points) ⇒ Object

draw lines connecting the array of points



221
222
223
224
# File 'lib/hotcocoa/graphics/path.rb', line 221

def lines(points)
  CGPathAddLines(@path, @transform, points)
  self
end

#lineto(x, y) ⇒ Object

draw a line from the current point to x,y



235
236
237
238
# File 'lib/hotcocoa/graphics/path.rb', line 235

def lineto(x,y)
  CGPathAddLineToPoint(@path, @transform, x, y)
  self
end

#moveto(x, y) ⇒ Object

move the "pen" to x,y



229
230
231
232
# File 'lib/hotcocoa/graphics/path.rb', line 229

def moveto(x, y)
  CGPathMoveToPoint(@path, @transform,x,y)
  self
end

#nostrokeObject

draw without stroke



101
102
103
# File 'lib/hotcocoa/graphics/path.rb', line 101

def nostroke
  @stroke = nil
end

#originxObject

return the x coordinate of the path's origin



113
114
115
# File 'lib/hotcocoa/graphics/path.rb', line 113

def originx
  CGPathGetBoundingBox(@path).origin.x
end

#originyObject

return the y coordinate of the path's origin



118
119
120
# File 'lib/hotcocoa/graphics/path.rb', line 118

def originy
  CGPathGetBoundingBox(@path).origin.y
end

#oval(x = 0, y = 0, w = 20, h = 20, reg = @registration) ⇒ Object

create an oval starting at x,y with dimensions w x h, optionally registered at :center



188
189
190
191
192
193
194
195
196
# File 'lib/hotcocoa/graphics/path.rb', line 188

def oval(x=0, y=0, w=20, h=20, reg=@registration)
  if (reg == :center)
    x = x - w / 2
    y = y - h / 2
  end
  puts "path.oval at [#{x},#{y}] with #{w}x#{h}" if @verbose
  CGPathAddEllipseInRect(@path, @transform, CGRectMake(x, y, w, h))
  self
end

#petal(x = 0, y = 0, w = 10, h = 50, bulge = h/2) ⇒ Object

draw a petal starting at x,y with w x h and center bulge height using quadratic curves



291
292
293
294
295
296
297
# File 'lib/hotcocoa/graphics/path.rb', line 291

def petal(x=0, y=0, w=10, h=50, bulge=h/2)
  moveto(x,y)
  qcurveto(x - w, y + bulge, x, y + h)
  qcurveto(x + w, y + bulge, x, y)
  endpath
  self
end

#qcurveto(cpx, cpy, x, y) ⇒ Object

draw a quadratic curve given a single control point and an end point



247
248
249
250
# File 'lib/hotcocoa/graphics/path.rb', line 247

def qcurveto(cpx, cpy, x, y)
  CGPathAddQuadCurveToPoint(@path, @transform, cpx, cpy, x, y)
  self
end

#randomize(parameter, value) ⇒ Object

randomize the specified parameter within the specified range



71
72
73
# File 'lib/hotcocoa/graphics/path.rb', line 71

def randomize(parameter, value)
  rand[parameter] = value
end

#rect(x = 0, y = 0, w = 20, h = 20, reg = @registration) ⇒ Object

add a rectangle starting at [x,y] with dimensions w x h



152
153
154
155
156
157
158
159
160
# File 'lib/hotcocoa/graphics/path.rb', line 152

def rect(x=0, y=0, w=20, h=20, reg=@registration)
  if reg == :center
    x = x - w / 2
    y = y - h / 2
  end
  puts "path.rect at [#{x},#{y}] with #{w}x#{h}" if @verbose
  CGPathAddRect(@path, @transform, CGRectMake(x,y,w,h))
  self
end

#registration(mode = :center) ⇒ Object

set registration mode to :center or :corner



91
92
93
# File 'lib/hotcocoa/graphics/path.rb', line 91

def registration(mode=:center)
  @registration = mode
end

#rotate(deg) ⇒ Object

specify rotation for subsequent operations



266
267
268
269
# File 'lib/hotcocoa/graphics/path.rb', line 266

def rotate(deg)
  puts "path.rotate #{deg}" if @verbose
  @transform = CGAffineTransformRotate(@transform, radians(deg))
end

#roundrect(x = 0, y = 0, width = 20, height = 20, roundness = 0, reg = @registration) ⇒ Object

draw a rounded rectangle using quadratic curved corners (FIXME)



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hotcocoa/graphics/path.rb', line 163

def roundrect(x=0, y=0, width=20, height=20, roundness=0, reg=@registration)
  if roundness == 0
    p.rect(x, y, width, height, reg)
  else
    if reg == :center
      x = x - self.width / 2
      y = y - self.height / 2
    end
    curve = min(width * roundness, height * roundness)
    p = Path.new
    p.moveto(x, y+curve)
    p.curveto(x, y, x, y, x+curve, y)
    p.lineto(x+width-curve, y)
    p.curveto(x+width, y, x+width, y, x+width, y+curve)
    p.lineto(x+width, y+height-curve)
    p.curveto(x+width, y+height, x+width, y+height, x+width-curve, y+height)
    p.lineto(x+curve, y+height)
    p.curveto(x, y+height, x, y+height, x, y+height-curve)
    p.endpath
  end
  addpath(p)
  self
end

#spiral(path = nil, rotation = 20, scalex = 0.95, scaley = 0.95, tx = 10, ty = 10, iterations = 30) ⇒ Object

duplicate and rotate the Path object the specified number of times path, rotation, scale, translation, iterations



310
311
312
313
314
315
316
317
# File 'lib/hotcocoa/graphics/path.rb', line 310

def spiral(path=nil, rotation=20, scalex=0.95, scaley=0.95, tx=10, ty=10, iterations=30)
  iterations.times do
    addpath(path)
    rotate(rotation)
    scale(scalex, scaley)
    translate(tx, ty)
  end
end

#to_sObject

print origin and dimensions



108
109
110
# File 'lib/hotcocoa/graphics/path.rb', line 108

def to_s
  "path.to_s: bounding box at [#{originx},#{originy}] with #{width}x#{height}, current point [#{currentpoint[0]},#{currentpoint[1]}]"
end

#translate(x, y) ⇒ Object

specify translation by x,y for subsequent drawing operations



283
284
285
286
# File 'lib/hotcocoa/graphics/path.rb', line 283

def translate(x,y)
  puts "path.translate #{x}x#{y}" if @verbose
  @transform = CGAffineTransformTranslate(@transform, x, y)
end

#verbose(tf = true) ⇒ Object

print drawing operations if verbose is true



96
97
98
# File 'lib/hotcocoa/graphics/path.rb', line 96

def verbose(tf=true)
  @verbose = tf
end

#widthObject

return the width of the path's bounding box



123
124
125
# File 'lib/hotcocoa/graphics/path.rb', line 123

def width
  CGPathGetBoundingBox(@path).size.width
end