diff --git a/web/app/arc.js b/web/app/arc.js new file mode 100644 index 00000000..7520de51 --- /dev/null +++ b/web/app/arc.js @@ -0,0 +1,106 @@ + +TCAD.TWO.Arc = function(a, b, c) { + TCAD.TWO.SketchObject.call(this); + this.a = a; + this.b = b; + this.c = c; + a.parent = this; + b.parent = this; + c.parent = this; +}; + +TCAD.TWO.utils.extend(TCAD.TWO.Arc, TCAD.TWO.SketchObject); + +TCAD.TWO.Arc.prototype._class = 'TCAD.TWO.Arc'; + +TCAD.TWO.Arc.prototype.collectParams = function(params) { + this.a.collectParams(params); + this.b.collectParams(params); + this.c.collectParams(params); +}; + +TCAD.TWO.Arc.prototype.draw = function(ctx, scale) { + TCAD.TWO.SketchObject.prototype.draw.call(this, ctx, scale); + this.a.draw(ctx, scale); + this.b.draw(ctx, scale); + this.c.draw(ctx, scale); +}; + +TCAD.TWO.Arc.prototype.getReferencePoint = function() { + return this.c; +}; + +TCAD.TWO.Arc.prototype.translateImpl = function(dx, dy) { + this.a.translate(dx, dy); + this.b.translate(dx, dy); + this.c.translate(dx, dy); +}; + +TCAD.TWO.Arc.prototype.drawImpl = function(ctx, scale) { + ctx.beginPath(); + var r = TCAD.math.distance(this.a.x, this.a.y, this.c.x, this.c.y); + ctx.arc(this.c.x, this.c.y, r, + Math.atan2(this.a.y - this.c.y, this.a.x - this.c.x), + Math.atan2(this.b.y - this.c.y, this.b.x - this.c.x)); + ctx.stroke(); +}; + + +TCAD.TWO.AddArcTool = function(viewer, layer) { + this.viewer = viewer; + this.layer = layer; + this.arc = null; + this.point = null; + this._v = new TCAD.Vector(0, 0, 0); +}; + +TCAD.TWO.AddArcTool.prototype.mousemove = function(e) { + if (this.point != null) { + var p = this.viewer.screenToModel(e); + this.point.x = p.x; + this.point.y = p.y; + + var r = TCAD.math.distance(this.arc.a.x, this.arc.a.y, this.arc.c.x, this.arc.c.y); + if (this.point.id === this.arc.b.id) { + //force placement second point on the arc + var v = this._v; + v.set(this.arc.b.x - this.arc.c.x, this.arc.b.y - this.arc.c.y, 0); + v._normalize()._multiply(r); + this.arc.b.x = v.x + this.arc.c.x; + this.arc.b.y = v.y + this.arc.c.y; + } else { + var ang = Math.atan2(this.point.y - this.arc.c.y, this.point.x - this.arc.c.x) + (2 * Math.PI - 0.3); + + ang %= 2 * Math.PI; + + this.arc.b.x = this.arc.c.x + r * Math.cos(ang); + this.arc.b.y = this.arc.c.y + r * Math.sin(ang); + } + + this.viewer.refresh(); + } +}; + +TCAD.TWO.AddArcTool.prototype.mouseup = function(e) { + if (this.arc == null) { + var p = this.viewer.screenToModel(e); + this.arc = new TCAD.TWO.Arc( + new TCAD.TWO.EndPoint(p.x, p.y), + new TCAD.TWO.EndPoint(p.x, p.y), + new TCAD.TWO.EndPoint(p.x, p.y) + ); + this.point = this.arc.a; + this.layer.objects.push(this.arc); + this.viewer.refresh(); + } else if (this.point.id === this.arc.a.id) { + this.point = this.arc.b; + } else { + this.viewer.toolManager.releaseControl(); + } +}; + +TCAD.TWO.AddArcTool.prototype.mousedown = function(e) { +}; + +TCAD.TWO.AddArcTool.prototype.mousewheel = function(e) { +}; diff --git a/web/app/canvas.js b/web/app/canvas.js index 02f8abd0..b9afdee9 100644 --- a/web/app/canvas.js +++ b/web/app/canvas.js @@ -398,54 +398,6 @@ TCAD.TWO.Segment.prototype.drawImpl = function(ctx, scale) { ctx.stroke(); }; - -TCAD.TWO.Arc = function(a, b, c) { - TCAD.TWO.SketchObject.call(this); - this.a = a; - this.b = b; - this.c = c; - a.parent = this; - b.parent = this; - c.parent = this; -}; - -TCAD.TWO.utils.extend(TCAD.TWO.Arc, TCAD.TWO.SketchObject); - -TCAD.TWO.Arc.prototype._class = 'TCAD.TWO.Arc'; - -TCAD.TWO.Arc.prototype.collectParams = function(params) { - this.a.collectParams(params); - this.b.collectParams(params); - this.c.collectParams(params); -}; - -TCAD.TWO.Arc.prototype.draw = function(ctx, scale) { - TCAD.TWO.SketchObject.prototype.draw.call(this, ctx, scale); - this.a.draw(ctx, scale); - this.b.draw(ctx, scale); - this.c.draw(ctx, scale); -}; - -TCAD.TWO.Arc.prototype.getReferencePoint = function() { - return this.c; -}; - -TCAD.TWO.Arc.prototype.translateImpl = function(dx, dy) { - this.a.translate(dx, dy); - this.b.translate(dx, dy); - this.c.translate(dx, dy); -}; - -TCAD.TWO.Arc.prototype.drawImpl = function(ctx, scale) { - ctx.beginPath(); - ctx.moveTo(this.a.x, this.a.y); - ctx.lineTo(this.b.x, this.b.y); - -// ctx.arc(c.x, c.y, ,0,2*Math.PI); - ctx.stroke(); -}; - - TCAD.TWO.Point = function(x, y, rad) { this.x = x; this.y = y; diff --git a/web/app/main2d.js b/web/app/main2d.js index 32c00808..bc5ef006 100644 --- a/web/app/main2d.js +++ b/web/app/main2d.js @@ -39,6 +39,10 @@ TCAD.App2D = function() { app.viewer.toolManager.takeControl(new TCAD.TWO.AddSegmentTool(app.viewer, layer)); }, + addArc : function () { + app.viewer.toolManager.takeControl(new TCAD.TWO.AddArcTool(app.viewer, layer)); + }, + pan : function() { app.viewer.toolManager.releaseControl(); }, @@ -91,6 +95,7 @@ TCAD.App2D = function() { }; actionsF.add(actions, 'addSegment'); + actionsF.add(actions, 'addArc'); actionsF.add(actions, 'pan'); actionsF.add(actions, 'save'); actionsF.add(actions, 'coincident'); diff --git a/web/app/math/math.js b/web/app/math/math.js new file mode 100644 index 00000000..3bbee53e --- /dev/null +++ b/web/app/math/math.js @@ -0,0 +1,7 @@ +TCAD.math = {}; + +TCAD.math.distance = function(x1, y1, x2, y2) { + var dx = x1 - x2; + var dy = y1 - y2; + return Math.sqrt(dx * dx + dy * dy); +} \ No newline at end of file diff --git a/web/app/vector.js b/web/app/vector.js index 3df7eac4..600e4092 100644 --- a/web/app/vector.js +++ b/web/app/vector.js @@ -27,8 +27,12 @@ TCAD.Vector.prototype.setV = function(data) { return this; }; -TCAD.Vector.prototype.multiply = function(factor) { - return new TCAD.Vector(this.x * factor, this.y * factor, this.z * factor); +TCAD.Vector.prototype.multiply = function(scalar) { + return new TCAD.Vector(this.x * scalar, this.y * scalar, this.z * scalar); +}; + +TCAD.Vector.prototype._multiply = function(scalar) { + return this.set(this.x * scalar, this.y * scalar, this.z * scalar); }; TCAD.Vector.prototype.dot = function(vector) { @@ -59,6 +63,15 @@ TCAD.Vector.prototype.normalize = function() { return new TCAD.Vector(this.x / mag, this.y / mag, this.z / mag); }; + +TCAD.Vector.prototype._normalize = function() { + var mag = this.length(); + if (mag == 0.0) { + return this.set(0, 0, 0) + } + return this.set(this.x / mag, this.y / mag, this.z / mag) +}; + TCAD.Vector.prototype.cross = function(a) { return new TCAD.Vector( this.y * a.z - this.z * a.y, diff --git a/web/canvas.html b/web/canvas.html index eaedb361..908d508a 100644 --- a/web/canvas.html +++ b/web/canvas.html @@ -12,6 +12,8 @@ + +