Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'tk'
- require 'matrix'
- # need for correct math operations
- require 'mathn'
- # adding support for 3D conversion
- class Point
- attr_reader :x, :y, :z
- def initialize(x,y,z)
- @x, @y, @z = x, y, z
- @matr = Matrix[[@x, @y, @z, 1]]
- end
- def to_s
- # "x: #{@x}\ny: #{y}\nz: #{z}"
- "(#{@x}, #{@y}, #{z})"
- end
- # transfer to a distance
- def moveTo(a,b,c)
- operMatrix = Matrix[
- [1, 0, 0, 0],
- [0, 1, 0, 0],
- [0, 0, 1, 0],
- [a, b, c, 1]
- ]
- @matr *= operMatrix
- # show 3 ways to get data from matrix by given indexeses
- # @TODO: maybe need to call _parseMatr
- @x = @matr.element(0,0)
- @y = @matr[0,1]
- @z = @matr.component(0,2)
- # returning value, actually needs?
- @matr
- end
- def rotateZ phi
- phi = phi * Math::PI / 180
- operMatrix = Matrix[
- [Math.cos(phi), Math.sin(phi), 0, 0],
- [-Math.sin(phi), Math.cos(phi), 0, 0],
- [0, 0, 1, 0],
- [0, 0, 0, 1]
- ]
- @matr *= operMatrix
- _parseMatr
- @matr
- end
- def rotateX phi
- phi = phi * Math::PI / 180
- operMatrix = Matrix[
- [1, 0, 0, 0],
- [0, Math.cos(phi), Math.sin(phi), 0],
- [0, -Math.sin(phi), Math.cos(phi), 0],
- [0, 0, 0, 1]
- ]
- @matr *= operMatrix
- _parseMatr
- @matr
- end
- def rotateY phi
- phi = phi * Math::PI / 180
- operMatrix = Matrix[
- [Math.cos(phi), 0, -Math.sin(phi), 0],
- [0, 1, 0, 0],
- [Math.sin(phi), 0, Math.cos(phi), 0],
- [0, 0, 0, 1]
- ]
- @matr *= operMatrix
- _parseMatr
- @matr
- end
- def scale(x,y,z)
- operMatrix = Matrix[
- [x, 0, 0, 0],
- [0, y, 0, 0],
- [0, 0, z, 0],
- [0, 0, 0, 1]
- ]
- @matr *= operMatrix
- _parseMatr
- @matr
- end
- # next methods are private declared
- private
- def _parseMatr
- @x = @matr[0,0]
- @y = @matr[0,1]
- @z = @matr[0,2]
- end
- private :_parseMatr
- end
- t = Point.new(50,50,100)
- # puts t
- # t.rotateZ(20)
- # t.scale(10, 10, 10)
- # puts t
- $width = 640
- $height = 480
- def _resize()
- # use this to get geometry of main widget
- $width = $root.winfo_width()
- $height = $root.winfo_height()
- $canvas.width $width
- $canvas.height $height
- end
- # initializing main window
- $root = TkRoot.new do
- title "Machine Graphics. lab3"
- geometry("#{$width}x#{$height}-0+20")
- end
- $canvas = TkCanvas.new do
- bg "white"
- width "#{$width}"
- height "#{$height}"
- bind('Configure', proc { _resize } )
- pack
- end
- TkcLine.new($canvas, 0, 0, 100, 100, 'tags' => 'Line')
- $cylinder = []
- $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)
- $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)
- print $cylinder, "\n"
- # need to output all need's data, because in Tk loop nothing will output
- $stdout.flush
- # $root.mainloop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement