Advertisement
Guest User

Untitled

a guest
May 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. /*
  2. * Slightly more convenient way of representing a point or width/height.
  3. */
  4. function Vector(x, y) {
  5. this.x = x
  6. this.y = y
  7. }
  8.  
  9. /*
  10. * Contains three points of the triangle and various helper functions.
  11. */
  12. function Triangle(p1, p2, p3) {
  13. // Quick conversions.
  14. if (p1.constructor == Array) {
  15. p1 = new Vector(p1[0], p1[1])
  16. }
  17. if (p2.constructor == Array) {
  18. p2 = new Vector(p2[0], p2[1])
  19. }
  20. if (p3.constructor == Array) {
  21. p3 = new Vector(p3[0], p3[1])
  22. }
  23.  
  24. this.points = [p1, p2, p3]
  25.  
  26. // Calculate the lengths of the lines opposite each point
  27. this.lines = [
  28. Math.sqrt(Math.pow(p2.x - p3.x, 2) +
  29. Math.pow(p2.y - p3.y, 2)),
  30.  
  31. Math.sqrt(Math.pow(p1.x - p3.x, 2) +
  32. Math.pow(p1.y - p3.y, 2)),
  33.  
  34. Math.sqrt(Math.pow(p2.x - p1.x, 2) +
  35. Math.pow(p2.y - p1.y, 2))
  36. ]
  37.  
  38. // Calculate the angle at each point
  39. this.angles = [
  40. Math.acos((Math.pow(this.lines[1], 2) +
  41. Math.pow(this.lines[2], 2) -
  42. Math.pow(this.lines[0], 2)) /
  43. (2 * this.lines[1] * this.lines[2])),
  44.  
  45. Math.acos((Math.pow(this.lines[2], 2) +
  46. Math.pow(this.lines[0], 2) -
  47. Math.pow(this.lines[1], 2)) /
  48. (2 * this.lines[2] * this.lines[0])),
  49.  
  50. Math.acos((Math.pow(this.lines[0], 2) +
  51. Math.pow(this.lines[1], 2) -
  52. Math.pow(this.lines[2], 2)) /
  53. (2 * this.lines[0] * this.lines[1]))
  54. ]
  55. }
  56. Triangle.prototype.draw = function(ctx) {
  57. // Draw the triangle shape
  58. ctx.moveTo(this.points[0].x, this.points[0].y)
  59. ctx.beginPath()
  60. ctx.lineTo(this.points[1].x, this.points[1].y)
  61. ctx.lineTo(this.points[2].x, this.points[2].y)
  62. ctx.lineTo(this.points[0].x, this.points[0].y)
  63. ctx.closePath()
  64.  
  65. // COLORS!
  66. var r = Math.floor(Math.random() * 7 * 32)
  67. var g = Math.floor(Math.random() * 7 * 32)
  68. ctx.fillStyle = 'rgb(' + r + ',' + g + ',' + 255 + ')'
  69. ctx.fill()
  70. }
  71. Triangle.prototype.split = function() {
  72. // Create two new triangles split along the largest angle
  73. var a = maxi(this.angles)
  74. var b = (a + 1) % 3
  75. var c = (a + 2) % 3
  76. var midpoint = new Vector(
  77. (this.points[b].x + this.points[c].x) / 2,
  78. (this.points[b].y + this.points[c].y) / 2
  79. )
  80.  
  81. return [
  82. new Triangle(midpoint, this.points[a], this.points[b]),
  83. new Triangle(midpoint, this.points[a], this.points[c])
  84. ]
  85. }
  86.  
  87. /*
  88. * Get the index of the largest item in an array.
  89. */
  90. function maxi(arr) {
  91. var res = null;
  92. for (var i = 0; i < arr.length; i++) {
  93. if (res == null || arr[i] > arr[res]) {
  94. res = i
  95. }
  96. }
  97. return res
  98. }
  99.  
  100. // Create a canvas
  101. var c = document.getElementById("canvas")
  102. var ctx = c.getContext("2d")
  103.  
  104. // Create the two basic triangles to start off with
  105. var tris = []
  106. tris.push(new Triangle([0, 0], [0, c.height], [c.width, c.height]))
  107. tris.push(new Triangle([0, 0], [c.width, 0], [c.width, c.height]))
  108.  
  109. // Split random triangles lots of times
  110. for (var i = 0; i < 100; i++) {
  111. var rtri = Math.floor(Math.random() * tris.length)
  112. var both = tris[rtri].split()
  113. tris.splice(rtri, 1)
  114. tris.push(both[0])
  115. tris.push(both[1])
  116. }
  117.  
  118. // Draw the triangles
  119. for (var i = 0; i < tris.length; i++) {
  120. tris[i].draw(ctx)
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement