Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2.  
  3. require 'curses'
  4. require 'matrix'
  5. require 'pp'
  6.  
  7. include Curses
  8. include Math
  9.  
  10. class Fig
  11. def initialize(matrix)
  12. @matrix = matrix
  13. end
  14.  
  15. def getPairVertexs
  16. end
  17.  
  18. def transform(trMethod)
  19. @matrix = trMethod.call(@matrix)
  20. end
  21. attr_accessor :matrix
  22. end
  23.  
  24. class Square < Fig
  25. def initialize(matrix)
  26. super matrix
  27. end
  28. def getPairVertexs
  29. r = []
  30. rows = @matrix.row_vectors
  31. rows.each_cons(2) { |pair|
  32. r << pair
  33. }
  34. r << [rows[-1], rows[0]]
  35. return r
  36. end
  37. end
  38.  
  39. class Drawer
  40. def initialize(primitives)
  41. @primitives = primitives
  42. end
  43.  
  44. def drawLines(lines)
  45. lines.each { |v|
  46. @primitives[:line].call v[0][0], v[0][1], v[1][0], v[1][1]
  47. }
  48. end
  49. end
  50.  
  51. def putMsg(x, y, ch)
  52. setpos y, x
  53. addstr ch
  54. end
  55.  
  56.  
  57.  
  58. def drawLine(x0, y0, x1, y1)
  59. x0 = x0.round
  60. y0 = y0.round
  61. x1 = x1.round
  62. y1 = y1.round
  63. points = []
  64. steep = ((y1-y0).abs) > ((x1-x0).abs)
  65. if steep
  66. x0,y0 = y0,x0
  67. x1,y1 = y1,x1
  68. end
  69. if x0 > x1
  70. x0,x1 = x1,x0
  71. y0,y1 = y1,y0
  72. end
  73. deltax = x1-x0
  74. deltay = (y1-y0).abs
  75. error = (deltax / 2).to_i
  76. y = y0
  77. ystep = nil
  78. if y0 < y1
  79. ystep = 1
  80. else
  81. ystep = -1
  82. end
  83. for x in x0..x1
  84. if steep
  85. points << {:x => y, :y => x}
  86. else
  87. points << {:x => x, :y => y}
  88. end
  89. error -= deltay
  90. if error < 0
  91. y += ystep
  92. error += deltax
  93. end
  94. end
  95. points.each {|p| putMsg p[:x], p[:y], '.' }
  96. return points
  97. end
  98.  
  99. def centerOfMass(matrix)
  100. sVector = Vector[0, 0]
  101. matrix.row_vectors.each { |v|
  102. sVector += v
  103. }
  104. r = sVector/matrix.row_vectors.size
  105. return r
  106. end
  107.  
  108. dr = Drawer.new({
  109. :line => lambda { |x0, y0, x1, y1| drawLine x0, y0, x1, y1 }
  110. })
  111.  
  112. sqSize = 10
  113.  
  114. cubes = [
  115. Square.new(Matrix[[0, 0], [sqSize, 0], [sqSize, sqSize], [0, sqSize]]),
  116. Square.new(Matrix[[0, 0], [sqSize, 0], [sqSize, sqSize], [0, sqSize]]),
  117. Square.new(Matrix[[0, 0], [sqSize, 0], [sqSize, sqSize], [0, sqSize]]),
  118. Square.new(Matrix[[0, 0], [sqSize, 0], [sqSize, sqSize], [0, sqSize]])
  119. ]
  120.  
  121.  
  122. def genRotateMatrix(angle)
  123. trRotateAroundMass = lambda { |a|
  124. c = centerOfMass a
  125. cMatrix = Matrix.rows(a.row_vectors.map { |v| v - c })
  126. crMatrix = cMatrix * Matrix[[cos(angle), -sin(angle)], [sin(angle), cos(angle)]]
  127. crcMatrix = Matrix.rows(crMatrix.row_vectors.map { |v| v + c } )
  128. }
  129. end
  130.  
  131. def genShiftMatrx(shift)
  132. lambda { |a|
  133. Matrix.rows(a.row_vectors.map { |v| v + shift })
  134. }
  135. end
  136.  
  137. init_screen
  138. nl
  139. noecho
  140.  
  141. maxy = lines
  142. maxx = cols
  143.  
  144.  
  145. cubes.each_with_index { |c, index|
  146. c.transform genShiftMatrx Vector[(index + 1)*maxx/(1 + cubes.size), maxy/2]
  147. }
  148.  
  149. while true
  150. cubes.each { |sq|
  151. sq.transform genRotateMatrix 0.1
  152. dr.drawLines sq.getPairVertexs
  153. }
  154. refresh
  155. sleep 0.03
  156. clear
  157. end
  158.  
  159. getch
  160. close_screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement