Advertisement
Bkmz

Untitled

Oct 18th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.07 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'tk'
  3. require 'matrix'
  4.  
  5. # need for correct math operations
  6. require 'mathn'
  7.  
  8. # adding support for 3D conversion
  9. class Point
  10.   attr_reader :x, :y, :z
  11.  
  12.   def initialize(x,y,z)
  13.     @x, @y, @z = x, y, z
  14.     @matr = Matrix[[@x, @y, @z, 1]]
  15.   end
  16.  
  17.   def to_s
  18.     # "x: #{@x}\ny: #{y}\nz: #{z}"
  19.     "(#{@x}, #{@y}, #{z})"
  20.   end
  21.  
  22.   # transfer to a distance
  23.   def moveTo(a,b,c)
  24.     operMatrix = Matrix[
  25.       [1, 0, 0, 0],
  26.       [0, 1, 0, 0],
  27.       [0, 0, 1, 0],
  28.       [a, b, c, 1]
  29.     ]
  30.     @matr *= operMatrix
  31.     # show 3 ways to get data from matrix by given indexeses
  32.     # @TODO: maybe need to call _parseMatr
  33.     @x = @matr.element(0,0)
  34.     @y = @matr[0,1]
  35.     @z = @matr.component(0,2)
  36.     # returning value, actually needs?
  37.     @matr
  38.   end
  39.  
  40.   def rotateZ phi
  41.     phi = phi * Math::PI / 180
  42.     operMatrix = Matrix[
  43.       [Math.cos(phi), Math.sin(phi), 0, 0],
  44.       [-Math.sin(phi), Math.cos(phi), 0, 0],
  45.       [0, 0, 1, 0],
  46.       [0, 0, 0, 1]
  47.     ]
  48.     @matr *= operMatrix
  49.     _parseMatr
  50.  
  51.     @matr
  52.   end
  53.  
  54.   def rotateX phi
  55.     phi = phi * Math::PI / 180
  56.     operMatrix = Matrix[
  57.       [1, 0, 0, 0],
  58.       [0, Math.cos(phi), Math.sin(phi), 0],
  59.       [0, -Math.sin(phi), Math.cos(phi), 0],
  60.       [0, 0, 0, 1]
  61.     ]
  62.     @matr *=  operMatrix
  63.     _parseMatr
  64.  
  65.     @matr
  66.   end
  67.  
  68.   def rotateY phi
  69.     phi = phi * Math::PI / 180
  70.     operMatrix = Matrix[
  71.       [Math.cos(phi), 0, -Math.sin(phi), 0],
  72.       [0, 1, 0, 0],
  73.       [Math.sin(phi), 0, Math.cos(phi), 0],
  74.       [0, 0, 0, 1]
  75.     ]
  76.     @matr *=  operMatrix
  77.     _parseMatr
  78.  
  79.     @matr
  80.   end
  81.  
  82.   def scale(x,y,z)
  83.     operMatrix = Matrix[
  84.       [x, 0, 0, 0],
  85.       [0, y, 0, 0],
  86.       [0, 0, z, 0],
  87.       [0, 0, 0, 1]
  88.     ]
  89.     @matr *= operMatrix
  90.     _parseMatr
  91.  
  92.     @matr
  93.   end
  94.  
  95.  
  96.  
  97. # next methods are private declared
  98. private
  99.  
  100.   def _parseMatr
  101.     @x = @matr[0,0]
  102.     @y = @matr[0,1]
  103.     @z = @matr[0,2]
  104.   end
  105.  
  106.  
  107.   private :_parseMatr
  108. end
  109.  
  110. t = Point.new(50,50,100)
  111.  
  112. # puts t
  113. # t.rotateZ(20)
  114. # t.scale(10, 10, 10)
  115. # puts t
  116.  
  117.  
  118. $width   = 640
  119. $height  = 480
  120.  
  121.  
  122. def _resize()
  123.   # use this to get geometry of main widget
  124.   $width = $root.winfo_width()
  125.   $height = $root.winfo_height()
  126.  
  127.   $canvas.width  $width
  128.   $canvas.height  $height
  129. end
  130. # initializing main window
  131. $root = TkRoot.new do
  132.   title "Machine Graphics. lab3"
  133.   geometry("#{$width}x#{$height}-0+20")
  134. end
  135.  
  136. $canvas = TkCanvas.new do
  137.   bg "white"
  138.   width "#{$width}"
  139.   height "#{$height}"
  140.   bind('Configure', proc  { _resize } )
  141.   pack
  142. end
  143.  
  144. TkcLine.new($canvas, 0, 0, 100, 100, 'tags' => 'Line')
  145.  
  146. $cylinder = []
  147.  
  148. $cylinder.push Point.new(25, -25, 0), Point.new(25, 25, 0), Point.new(0, 25, 0), Point.new(-25, 25, 0), Point.new(-25, -25, 0), Point.new(0, -25, 0)
  149. $cylinder.push Point.new(25, -25, 100), Point.new(25, 25, 100), Point.new(0, 25, 100), Point.new(-25, 25, 100), Point.new(-25, -25, 100), Point.new(0, -25, 100)
  150.  
  151. print $cylinder, "\n"
  152.  
  153.  
  154.  
  155. # need to output all need's data, because in Tk loop nothing will output
  156. $stdout.flush
  157.  
  158. # $root.mainloop
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement