summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/shapes/polyline.js
blob: 6c6412806ba324dce0360c162d5dcfb0b2f4d9d5 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var Polyline = Fiber.extend(function(base){
	var exports = {}
	exports.init = function(){
    this.points = []
    this.mx_points = []
    this.closed = false
	}
	exports.add = function(p){
    this.points.push( p )
    this.mx_points.push( new MX.Point(p) )
	}
	exports.firstPoint = function(){
	  return this.points[0]
	}
	exports.lastPoint = function(){
	  return this.points[this.points.length-1]
	}
	exports.canCloseWith = function(p){
    return (this.points.length > 2 && this.points[0].distanceTo( p ) < 10/map.zoom)
	}
	exports.getHeadAtIndex = function(index){
	  if (index == 0) { return null }
	  if (index == this.points.length-1) { return this.clone() }
	  var head = new Polyline()
	  head.points = this.points.slice(0, index+1)
	  return head
	}
	exports.getTailAtIndex = function(index){
	  if (index == this.points.length-1) { return null }
	  if (index == 0) { return this.clone() }
	  var tail = new Polyline()
	  tail.points = this.points.slice(index, this.points.length)
	  return tail
	}
	exports.clone = function(){
	  var clone = new Polyline()
	  clone.points = this.points.concat()
	}
	exports.hasPointNear = function(p){
	  var point
    for (var i = 0; i < this.points.length; i++){
      point = this.points[i]
      if (point.distanceTo( p ) < 10/map.zoom) {
        return point
      }
    }
    return null
	}
	exports.hasEndPointNear = function(p){
	  if (this.closed || ! this.points.length) return null
    if (this.firstPoint().distanceTo( p ) < 10/map.zoom) {
      return this.firstPoint()
    }
    if (this.lastPoint().distanceTo( p ) < 10/map.zoom) {
      return this.lastPoint()
    }
    return null
	}
	exports.hasSegmentNear = function(p, min_dist){
    var p1, p2, d1, d2, sum, rat
    var dx, dy, new_x, new_y, x, y, closest_distance = min_dist || Infinity
    var closest_i = -1
    var points = this.points
    var p1, p2 = points[0]
    for (var i = 1; i < points.length; i++) {
      p1 = p2
      p2 = points[i]
      d1 = p2.a - p1.a
      d2 = p2.b - p1.b
      sum = d1*d1 + d2*d2
      rat = ((p.a - p1.a) * d1 + (p.b - p1.b) * d2) / sum
      rat = rat < 0 ? 0 : rat < 1 ? rat : 1
      new_x = p1.a + rat * d1
      new_y = p1.b + rat * d2
      dx = new_x - p.a
      dy = new_y - p.b
      sum2 = sqrt(dx*dx+dy*dy)
      if (sum2 < closest_distance) {
        x = new_x
        y = new_y
        closest_distance = sum2
        closest_i = i
      }
    }
    if (closest_i == -1) return null
    return {
      x: x,
      y: y,
      distance: closest_distance,
      head: closest_i-1,
      tail: closest_i,
    }
	}
  exports.draw = function(ctx){
    var points = this.points
    if (! points.length) return
    if (points.length == 1) {
      ctx.fillStyle = "#f80"
      map.draw.dot_at(this.points[0].a, points[0].b, 5)
    }
    if (points.length > 1) {
      ctx.fillStyle = "rgba(255,255,0,0.1)"
      ctx.strokeStyle = "#f80"
      ctx.lineWidth = 2 / map.zoom
      ctx.beginPath()
      ctx.moveTo(points[0].a, points[0].b)
      points.forEach(function(point, i){
        i && ctx.lineTo(point.a, point.b)
      })
      ctx.stroke()
      if (! map.ui.placing || this.closed) {
        ctx.fill()
      }
    }
  }
  exports.draw_line = function (ctx, p){
    var last = this.points[this.points.length-1]
    ctx.strokeStyle = "#f80"
    ctx.lineWidth = 2 / map.zoom
    ctx.beginPath()
    ctx.moveTo(last.a, last.b)
    ctx.lineTo(p.a, p.b)
    ctx.stroke()
  }
  exports.close = function(){
    this.points[this.points.length] = this.points[0]
    this.closed = true
  }
  exports.build = function(){
    this.mx_points && this.mx_points.forEach(function(mx){ scene.remove(mx) })
    this.mx = new MX.Polyline(this)
    shapes.add(this)
  }
  exports.rebuild = function(){
    this.mx.rebuild()
  }
  exports.getSegments = function(){
    if (this.points.length == 1) {
      return []
    }
    var segments = []
    for (var i = 1; i < this.points.length; i++) {
      segments.push( [ this.points[i-1], this.points[i] ] )
    }
    return segments
  }
  exports.serialize = function(){
    return this.points.map(function(point){ return [point.a, point.b] })
  }
  exports.deserialize = function(points){
    this.points = points.map(function(point){ return new vec2(point[0], point[1]) })
  }
  exports.reset = function(){
    this.mx_points.forEach(function(mx){ scene.remove(mx) })
    this.mx_points.length = 0
    this.points.length = 0
  }
	exports.destroy = function(){
	  this.reset()
	  this.mx && this.mx.destroy()
	}
  return exports
})