|
| 1 | +// 2D Circle class |
| 2 | +var Circle = makeClass(Curve, { |
| 3 | + constructor: function Circle(center, radius) { |
| 4 | + var self = this, |
| 5 | + _radius = null, |
| 6 | + _length = null, |
| 7 | + _area = null, |
| 8 | + _bbox = null, |
| 9 | + _hull = null |
| 10 | + ; |
| 11 | + |
| 12 | + if (center instanceof Circle) return center; |
| 13 | + if (!(self instanceof Circle)) return new Circle(center, radius); |
| 14 | + _radius = stdMath.abs(Num(radius)); |
| 15 | + Curve.call(self, [center]); |
| 16 | + |
| 17 | + Object.defineProperty(self, 'center', { |
| 18 | + get() { |
| 19 | + return self.points[0]; |
| 20 | + }, |
| 21 | + set(center) { |
| 22 | + self.points[0] = center; |
| 23 | + }, |
| 24 | + enumerable: true |
| 25 | + }); |
| 26 | + Object.defineProperty(self, 'radius', { |
| 27 | + get() { |
| 28 | + return _radius; |
| 29 | + }, |
| 30 | + set(radius) { |
| 31 | + radius = stdMath.abs(Num(radius)); |
| 32 | + if (_radius !== radius) |
| 33 | + { |
| 34 | + var isDirty = false; |
| 35 | + if (!is_almost_equal(_radius, radius)) |
| 36 | + { |
| 37 | + isDirty = true; |
| 38 | + } |
| 39 | + _radius = radius; |
| 40 | + if (isDirty) |
| 41 | + { |
| 42 | + if (!self.isDirty()) |
| 43 | + { |
| 44 | + self.isDirty(true); |
| 45 | + self.triggerChange(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + }, |
| 50 | + enumerable: true |
| 51 | + }); |
| 52 | + Object.defineProperty(self, 'length', { |
| 53 | + get() { |
| 54 | + if (null == _length) |
| 55 | + { |
| 56 | + _length = 2 * stdMath.PI * _radius; |
| 57 | + } |
| 58 | + return _length; |
| 59 | + }, |
| 60 | + enumerable: true |
| 61 | + }); |
| 62 | + Object.defineProperty(self, 'area', { |
| 63 | + get() { |
| 64 | + if (null == _area) |
| 65 | + { |
| 66 | + _area = stdMath.PI * _radius * _radius; |
| 67 | + } |
| 68 | + return _area; |
| 69 | + }, |
| 70 | + enumerable: true |
| 71 | + }); |
| 72 | + Object.defineProperty(self, '_bbox', { |
| 73 | + get() { |
| 74 | + if (null == _bbox) |
| 75 | + { |
| 76 | + var c = self.center, r = _radius; |
| 77 | + _bbox = { |
| 78 | + top: c.y - r, |
| 79 | + left: c.x - r, |
| 80 | + bottom: c.y + r, |
| 81 | + right: c.x + r |
| 82 | + }; |
| 83 | + } |
| 84 | + return _bbox; |
| 85 | + }, |
| 86 | + enumerable: false |
| 87 | + }); |
| 88 | + Object.defineProperty(self, '_hull', { |
| 89 | + get() { |
| 90 | + if (null == _hull) |
| 91 | + { |
| 92 | + var c = self.center, r = _radius; |
| 93 | + _hull = [ |
| 94 | + new Point(c.x-r, c.y-r), |
| 95 | + new Point(c.x+r, c.y-r), |
| 96 | + new Point(c.x+r, c.y+r), |
| 97 | + new Point(c.x-r, c.y+r) |
| 98 | + ]; |
| 99 | + } |
| 100 | + return _hull; |
| 101 | + }, |
| 102 | + enumerable: false |
| 103 | + }); |
| 104 | + self.isDirty = function(isDirty) { |
| 105 | + if (true === isDirty) |
| 106 | + { |
| 107 | + _length = null; |
| 108 | + _area = null; |
| 109 | + _bbox = null; |
| 110 | + _hull = null; |
| 111 | + } |
| 112 | + return self.$super.isDirty.apply(self, arguments); |
| 113 | + }; |
| 114 | + }, |
| 115 | + clone: function() { |
| 116 | + return new Circle(this.center.clone(), this.radius); |
| 117 | + }, |
| 118 | + transform: function(matrix) { |
| 119 | + var c = this.center, |
| 120 | + r = this.radius, |
| 121 | + ct = c.transform(matrix), |
| 122 | + p = new Point(c.x+r, c.y).transform(matrix) |
| 123 | + ; |
| 124 | + return new Circle(ct, dist(ct, pt)); |
| 125 | + }, |
| 126 | + isClosed: function() { |
| 127 | + return true; |
| 128 | + }, |
| 129 | + getBoundingBox: function() { |
| 130 | + return this._bbox; |
| 131 | + }, |
| 132 | + getConvexHull: function() { |
| 133 | + return this._hull; |
| 134 | + }, |
| 135 | + getPoint: function(t) { |
| 136 | + t = Num(t); |
| 137 | + if (0 > t || 1 < t) return null; |
| 138 | + var c = this.center, r = this.radius; |
| 139 | + return new Point( |
| 140 | + c.x + r*stdMath.cos(t*2*stdMath.PI), |
| 141 | + c.y + r*stdMath.sin(t*2*stdMath.PI) |
| 142 | + ); |
| 143 | + }, |
| 144 | + hasPoint: function(point, notInside) { |
| 145 | + var center = this.center, |
| 146 | + radius2 = this.radius*this.radius, |
| 147 | + dx = point.x - center.x, |
| 148 | + dy = point.y - center.y, |
| 149 | + d = dx*dx + dy*dy |
| 150 | + ; |
| 151 | + return notInside ? is_almost_equal(d, radius2) : d <= radius2; |
| 152 | + }, |
| 153 | + intersects: function(other) { |
| 154 | + if (other instanceof Point) |
| 155 | + { |
| 156 | + return this.hasPoint(other) ? other : false; |
| 157 | + } |
| 158 | + else if (other instanceof Line) |
| 159 | + { |
| 160 | + return line_circle_intersection(other.start, other.end, this.center, this.radius); |
| 161 | + } |
| 162 | + else if (other instanceof Polyline) |
| 163 | + { |
| 164 | + } |
| 165 | + else if (other instanceof Circle) |
| 166 | + { |
| 167 | + return circle_circle_intersection(other.center, other.radius, this.center, this.radius); |
| 168 | + } |
| 169 | + else if ((other instanceof Primitive) && is_function(other.intersects)) |
| 170 | + { |
| 171 | + return other.intersects(this); |
| 172 | + } |
| 173 | + return false; |
| 174 | + }, |
| 175 | + toTex: function() { |
| 176 | + return '\\text{Circle:}'+'(x - '+Str(this.center.x)+')^2 + (y - '+Str(this.center.y)+')^2 = '+Str(this.radius)+'^2'; |
| 177 | + }, |
| 178 | + toString: function() { |
| 179 | + return 'Circle('+[Str(this.center), Str(this.radius)].join(',')+')'; |
| 180 | + } |
| 181 | +}); |
0 commit comments