Permalink
Browse files

started adding test coverage

  • Loading branch information...
1 parent 2ad7f0c commit c34609bf41e1d9f4d39a42ed4a47aa197ec4abd1 @mattetti committed Jul 12, 2010
Showing with 305 additions and 229 deletions.
  1. +11 −12 examples/canvas_example.rb
  2. +1 −1 examples/image_blend_example.rb
  3. +50 −55 graphics.rb
  4. +84 −95 lib/canvas.rb
  5. +10 −10 lib/color.rb
  6. +4 −4 lib/gradient.rb
  7. +1 −2 lib/image.rb
  8. +42 −50 lib/path.rb
  9. +34 −0 specs/canvas_spec.rb
  10. +67 −0 specs/graphics_spec.rb
  11. +1 −0 specs/tmp/.ignore
View
@@ -7,22 +7,21 @@ class CustomView < NSView
include MRGraphics
def drawRect(rect)
- canvas = Canvas.for_image(:size => [400,400]) do
- background(Color.black)
-
+ canvas = Canvas.for_image(:size => [400,400]) do |c|
+ c.background(Color.black)
white = Color.white
- fill(white)
- stroke(0.2)
- strokewidth(1)
- font("Zapfino")
+ c.fill(white)
+ c.stroke(0.2)
+ c.stroke_width(1)
+ c.font("Zapfino")
80.times do
- fontsize rand(170)
- fill(white.copy.darken(rand(0.8)))
+ c.font_size rand(170)
+ c.fill(white.copy.darken(rand(0.8)))
letters = %W{ g i a n a }
- text(letters[rand(letters.size)],
- rand(width),
- rand(height))
+ c.text(letters[rand(letters.size)],
+ rand(c.width),
+ rand(c.height))
end
end
@@ -12,7 +12,7 @@ def drawRect(rect)
canvas = Canvas.for_image(:size => dimensions) do
background(Color.white)
font('Skia')
- fontsize(14)
+ font_size(14)
# set image width,height
w, h = [95,95]
# set initial drawing position
View
@@ -9,14 +9,13 @@
# in Java and Python, respectively. RCG was created to offer similar functionality using
# the Ruby programming language.
#
-# Author:: James Reynolds (mailto:drtoast@drtoast.com)
+# Author:: James Reynolds (mailto:drtoast@drtoast.com), Matt Aimonetti
# Copyright:: Copyright (c) 2008 James Reynolds
# License:: Distributes under the same terms as Ruby
# More information about Quartz 2D is available on the Apple's website:
# http://developer.apple.com/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_overview/dq_overview.html#//apple_ref/doc/uid/TP30001066-CH202-TPXREF101
-
framework 'Cocoa'
framework 'Quartz'
@@ -26,44 +25,44 @@ module MRGraphics
TEST = 'OK'
# convert degrees to radians
- def radians(deg)
+ def self.radians(deg)
deg * (Math::PI / 180.0)
end
# convert radians to degrees
- def degrees(rad)
+ def self.degrees(rad)
rad * (180 / Math::PI)
end
# return the angle of the line joining the two points
- def angle(x0, y0, x1, y1)
+ def self.angle(x0, y0, x1, y1)
degrees(Math.atan2(y1-y0, x1-x0))
end
# return the distance between two points
- def distance(x0, y0, x1, y1)
+ def self.distance(x0, y0, x1, y1)
Math.sqrt((x1-x0)**2 + (y1-y0)**2)
end
# return the coordinates of a new point at the given distance and angle from a starting point
- def coordinates(x0, y0, distance, angle)
+ def self.coordinates(x0, y0, distance, angle)
x1 = x0 + Math.cos(radians(angle)) * distance
y1 = y0 + Math.sin(radians(angle)) * distance
[x1,y1]
end
# return the lesser of a,b
- def min(a, b)
+ def self.min(a, b)
a < b ? a : b
end
# return the greater of a,b
- def max(a, b)
+ def self.max(a, b)
a > b ? a : b
end
# restrict the value to stay within the range
- def inrange(value, min, max)
+ def self.in_range(value, min, max)
if value < min
min
elsif value > max
@@ -74,7 +73,7 @@ def inrange(value, min, max)
end
# return a random number within the range, or a float from 0 to the number
- def random(left=nil, right=nil)
+ def self.random(left=nil, right=nil)
if right
rand * (right - left) + left
elsif left
@@ -84,14 +83,14 @@ def random(left=nil, right=nil)
end
end
- def reflect(x0, y0, x1, y1, d=1.0, a=180)
+ def self.reflect(x0, y0, x1, y1, d=1.0, a=180)
d *= distance(x0, y0, x1, y1)
a += angle(x0, y0, x1, y1)
x, y = coordinates(x0, y0, d, a)
[x,y]
end
- def choose(object)
+ def self.choose(object)
case object
when Range
case object.first
@@ -108,55 +107,51 @@ def choose(object)
end
# given an object's x,y coordinates and dimensions, return the distance
- # needed to move in order to orient the object at the given location (:center, :bottomleft, etc)
+ # needed to move in order to orient the object at the given location (:center, :bottom_left, etc)
def self.reorient(x, y, w, h, location)
case location
- when :bottomleft
- movex = -x
- movey = -y
- when :centerleft
- movex = -x
- movey = -y - h / 2
- when :topleft
- movex = -x
- movey = -x - h
- when :bottomright
- movex = -x - w
- movey = -y
- when :centerright
- movex = -x - w
- movey = -y - h / 2
- when :topright
- movex = -x - w
- movey = -y - h
- when :bottomcenter
- movex = -x - w / 2
- movey = -y
+ when :bottom_left
+ move_x = -x
+ move_y = -y
+ when :center_left
+ move_x = -x
+ move_y = -y - h / 2
+ when :top_left
+ move_x = -x
+ move_y = -x - h
+ when :bottom_right
+ move_x = -x - w
+ move_y = -y
+ when :center_right
+ move_x = -x - w
+ move_y = -y - h / 2
+ when :top_right
+ move_x = -x - w
+ move_y = -y - h
+ when :bottom_center
+ move_x = -x - w / 2
+ move_y = -y
when :center
- movex = -x - w / 2
- movey = -y - h / 2
- when :topcenter
- movex = -x - w / 2
- movey = -y - h
+ move_x = -x - w / 2
+ move_y = -y - h / 2
+ when :top_center
+ move_x = -x - w / 2
+ move_y = -y - h
else
raise "ERROR: image origin locator not recognized: #{location}"
end
- #newx = oldx + movex
- #newy = oldy + movey
- [movex,movey]
+ [move_x,move_y]
end
end
-require 'pathname'
-here = Pathname.new(File.dirname(__FILE__))
-
-require here.join('lib/canvas')
-require here.join('lib/color')
-require here.join('lib/gradient')
-require here.join('lib/image')
-require here.join('lib/path')
-require here.join('lib/pdf')
-require here.join('lib/elements/particle')
-require here.join('lib/elements/rope')
-require here.join('lib/elements/sandpainter')
+here = File.expand_path(File.dirname(__FILE__))
+require File.join(here, 'lib', 'canvas')
+require File.join(here, 'lib', 'color')
+require File.join(here, 'lib', 'gradient')
+require File.join(here, 'lib', 'image')
+require File.join(here, 'lib', 'path')
+require File.join(here, 'lib', 'pdf')
+require File.join(here, 'lib', 'elements/particle')
+require File.join(here, 'lib', 'elements/rope')
+require File.join(here, 'lib', 'elements/sandpainter')
Oops, something went wrong.

0 comments on commit c34609b

Please sign in to comment.