make arc tool work

This commit is contained in:
Val Erastov 2014-10-09 19:09:07 -07:00
parent 566ef55dea
commit 917d0610f9
6 changed files with 135 additions and 50 deletions

106
web/app/arc.js Normal file
View file

@ -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) {
};

View file

@ -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;

View file

@ -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');

7
web/app/math/math.js Normal file
View file

@ -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);
}

View file

@ -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,

View file

@ -12,6 +12,8 @@
<script src="lib/three/three.js"></script>
<script src="lib/dat.gui.min.js"></script>
<script src="app/canvas.js"></script>
<script src="app/arc.js"></script>
<script src="app/math/math.js"></script>
<script src="app/math/lm.js"></script>
<script src="app/constr/constraints.js"></script>