summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/mx/primitives/mx.polyline.js
blob: 63c0ef8414bdf1c8849b86db8b30801a19c7982e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
MX.Polyline = MX.Object3D.extend({
  init: function(polyline){
    this.faces = []
    this.points = polyline.points
    for (var i = 1; i < this.points.length; i++) {
      var mx = new MX.Object3D()
      var head = this.points[i-1]
      var tail = this.points[i]
      this.move_face(mx, head, tail)
      this.faces.push(mx)
      scene.add(mx)
    }
  },
  
  rebuild: function(){
    for (var i = 1; i < this.points.length; i++) {
      var mx = this.faces[i-1]
      var head = this.points[i-1]
      var tail = this.points[i]
      this.move_face(mx, head, tail)
    }
  },

  move_face: function (mx, head, tail){
    var mid_x = (head.a + tail.a)
    var mid_z = (head.b + tail.b)
    var len = head.distanceTo( tail )
    var angle = atan2( head.b - tail.b, head.a - tail.a )
    mx.move({
      x: mid_x / 2,
      y: wallHeight/2 + 1,
      z: mid_z / 2,
      width: ceil(len),
      height: wallHeight,
      rotationY: angle
    })
    var hue = abs(round( angle / PI * 90 + 300))
    mx.el.style.backgroundColor = 'hsl(' + [hue, "100%", "50%"] + ')'
  },
  
  set_height: function(height){
    for (var i = 0; i < this.faces.length; i++) {
      this.faces[i].height = height
      this.faces[i].y = height / 2 + 1
    }
  },
  
  destroy: function(){
    this.faces.forEach(function(mx){
      scene.remove(mx)
    })
    this.faces = null
    this.points = null
  },
})