Class: RubyLabs::Canvas::Polygon
Overview
Polygon
A Polygon object is a proxy for a Tk polygon.
In addition to the methods defined in the TkObject base class, a method named +rotate+
will rotate a Polygon by a specified angle.
Instance Attribute Summary
Attributes inherited from TkObject
Instance Method Summary collapse
-
#initialize(a, args = {}) ⇒ Polygon
constructor
Create a new polygon.
-
#rotate(theta) ⇒ Object
Rotate the polygon by an angle
theta
(expressed in degrees).
Methods inherited from TkObject
#erase, #fill=, #lower, nextId, options, #raise, reset
Constructor Details
#initialize(a, args = {}) ⇒ Polygon
Create a new polygon. The first argument is an array of vertex coordinates. If the polygon has n
vertices, the argument should be a flat array of 2*n points. Attributes of the polygon can be passed in a hash object at the end of the argument list.
Example – create a small triangle in the upper left corner of the canvas, with a black outline and green interior:
>> x = Canvas::Polygon.new([10,10,20,20,30,10], :fill => "green", :outline => "black")
=> #<RubyLabs::Canvas::Polygon ... >
:call-seq:
x = Polygon.new(a, args = {})
1000 1001 1002 1003 1004 1005 1006 1007 |
# File 'lib/rubylabs.rb', line 1000 def initialize(a, args = {}) raise "No canvas" unless @@pipe @name = TkObject.nextId @coords = a @penpoint = [0,0] cmnd = "set #{@name} [.canvas create polygon #{@coords.join(" ")} #{TkObject.(args)}]" @@pipe.puts cmnd end |
Instance Method Details
#rotate(theta) ⇒ Object
Rotate the polygon by an angle theta
(expressed in degrees). The object is rotated about the point defined by the first pair of (x,y) coordinates. The return value is an array with the new coordinates.
Example: Suppose x
is a square with coordinates (10,10), (20,10), (20,20), and (10,20). This call will rotate it clockwise by 45 degrees and return the new vertices:
>> x.rotate(45)
=> [10.0, 10.0, 17.07, 17.07, 10.0, 24.14, 2.93, 17.07]
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 |
# File 'lib/rubylabs.rb', line 1018 def rotate(theta) theta = Canvas.radians(theta) a = self.coords x0 = a[0] y0 = a[1] (0...a.length).step(2) do |i| x = a[i] - x0 y = a[i+1] - y0 a[i] = x0 + x * cos(theta) - y * sin(theta) a[i+1] = y0 + x * sin(theta) + y * cos(theta) end self.coords = a return a end |