mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-08 01:13:27 +01:00
arc work
This commit is contained in:
parent
08bddc3a95
commit
c9bb9f20aa
4 changed files with 102 additions and 10 deletions
|
|
@ -7,6 +7,7 @@ TCAD.TWO.Arc = function(a, b, c) {
|
|||
a.parent = this;
|
||||
b.parent = this;
|
||||
c.parent = this;
|
||||
this.r = new TCAD.TWO.Ref();
|
||||
};
|
||||
|
||||
TCAD.TWO.utils.extend(TCAD.TWO.Arc, TCAD.TWO.SketchObject);
|
||||
|
|
@ -36,9 +37,17 @@ TCAD.TWO.Arc.prototype.translateImpl = function(dx, dy) {
|
|||
this.c.translate(dx, dy);
|
||||
};
|
||||
|
||||
TCAD.TWO.Arc.prototype.distanceA = function() {
|
||||
return TCAD.math.distance(this.a.x, this.a.y, this.c.x, this.c.y);
|
||||
};
|
||||
|
||||
TCAD.TWO.Arc.prototype.distanceB = function() {
|
||||
return TCAD.math.distance(this.b.x, this.b.y, this.c.x, this.c.y);
|
||||
};
|
||||
|
||||
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);
|
||||
var r = this.distanceA();
|
||||
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));
|
||||
|
|
@ -53,7 +62,7 @@ TCAD.TWO.Arc.prototype.visit = function(h) {
|
|||
};
|
||||
|
||||
TCAD.TWO.Arc.prototype.normalDistance = function(aim) {
|
||||
return 1000;
|
||||
return -1000;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -106,6 +115,9 @@ TCAD.TWO.AddArcTool.prototype.mouseup = function(e) {
|
|||
} else if (this.point.id === this.arc.a.id) {
|
||||
this.point = this.arc.b;
|
||||
} else {
|
||||
var arc = this.arc;
|
||||
this.viewer.parametricManager.system.push(new TCAD.TWO.Constraints.P2PDistanceV(arc.b, arc.c, arc.r));
|
||||
this.viewer.parametricManager.system.push(new TCAD.TWO.Constraints.P2PDistanceV(arc.a, arc.c, arc.r));
|
||||
this.viewer.toolManager.releaseControl();
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -299,6 +299,18 @@ TCAD.TWO.SketchObject.prototype.draw = function(ctx, scale) {
|
|||
if (this.marked) ctx.restore();
|
||||
};
|
||||
|
||||
TCAD.TWO.Ref = function() {
|
||||
this.value = 0;
|
||||
};
|
||||
|
||||
TCAD.TWO.Ref.prototype.set = function(value) {
|
||||
this.value = value;
|
||||
};
|
||||
|
||||
TCAD.TWO.Ref.prototype.get = function() {
|
||||
return this.value;
|
||||
};
|
||||
|
||||
TCAD.TWO.Param = function(obj, prop) {
|
||||
this.id = TCAD.TWO.utils.genID();
|
||||
this.obj = obj;
|
||||
|
|
@ -619,8 +631,16 @@ TCAD.TWO.DragTool.prototype.mousewheel = function(e) {
|
|||
|
||||
TCAD.TWO.DragTool.prototype.solveRequest = function(fineLevel) {
|
||||
var locked;
|
||||
if (this.obj._class == 'TCAD.TWO.EndPoint') {
|
||||
if (this.obj._class === 'TCAD.TWO.EndPoint') {
|
||||
locked = [this.obj._x, this.obj._y];
|
||||
if (this.obj.parent != null
|
||||
&& this.obj.parent._class === 'TCAD.TWO.Arc') {
|
||||
|
||||
if (this.obj.id != this.obj.parent.c.id) {
|
||||
locked.push(this.obj.parent.c._x);
|
||||
locked.push(this.obj.parent.c._y);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
locked = [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
TCAD.constraints = {};
|
||||
|
||||
TCAD.constraints.create = function(name, params, constants) {
|
||||
TCAD.constraints.create = function(name, params, values) {
|
||||
switch (name) {
|
||||
case "equal":
|
||||
return new TCAD.constraints.Equal(params);
|
||||
|
|
@ -9,9 +9,11 @@ TCAD.constraints.create = function(name, params, constants) {
|
|||
case "parallel":
|
||||
return new TCAD.constraints.Parallel(params);
|
||||
case "P2LDistance":
|
||||
return new TCAD.constraints.P2LDistance(params, constants[0]);
|
||||
return new TCAD.constraints.P2LDistance(params, values[0]);
|
||||
case "P2PDistance":
|
||||
return new TCAD.constraints.P2PDistance(params, constants[0]);
|
||||
return new TCAD.constraints.P2PDistance(params, values[0]);
|
||||
case "P2PDistanceV":
|
||||
return new TCAD.constraints.P2PDistanceV(params);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -137,14 +139,14 @@ TCAD.constraints.P2PDistance = function(params, distance) {
|
|||
|
||||
this.get = function(i) {
|
||||
return this.params[i].get();
|
||||
}
|
||||
};
|
||||
|
||||
this.error = function() {
|
||||
var dx = this.get(this.p1x) - this.get(this.p2x);
|
||||
var dy = this.get(this.p1y) - this.get(this.p2y);
|
||||
var d = Math.sqrt(dx * dx + dy * dy);
|
||||
return (d - this.distance);
|
||||
}
|
||||
return (d - this.distance());
|
||||
};
|
||||
|
||||
this.gradient = function(out) {
|
||||
|
||||
|
|
@ -159,6 +161,44 @@ TCAD.constraints.P2PDistance = function(params, distance) {
|
|||
|
||||
};
|
||||
|
||||
|
||||
TCAD.constraints.P2PDistanceV = function(params) {
|
||||
|
||||
this.params = params;
|
||||
|
||||
|
||||
this.p1x = 0;
|
||||
this.p1y = 1;
|
||||
this.p2x = 2;
|
||||
this.p2y = 3;
|
||||
this.d = 4;
|
||||
|
||||
this.get = function(i) {
|
||||
return this.params[i].get();
|
||||
};
|
||||
|
||||
this.error = function() {
|
||||
var dx = this.get(this.p1x) - this.get(this.p2x);
|
||||
var dy = this.get(this.p1y) - this.get(this.p2y);
|
||||
var d = Math.sqrt(dx * dx + dy * dy);
|
||||
return (d - this.get(this.d));
|
||||
};
|
||||
|
||||
this.gradient = function(out) {
|
||||
|
||||
var dx = this.get(this.p1x) - this.get(this.p2x);
|
||||
var dy = this.get(this.p1y) - this.get(this.p2y);
|
||||
var d = Math.sqrt(dx * dx + dy * dy);
|
||||
out[this.p1x] = dx / d;
|
||||
out[this.p1y] = dy / d;
|
||||
out[this.p2x] = -dx / d;
|
||||
out[this.p2y] = -dy / d;
|
||||
out[this.d] = -1;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
TCAD.constraints.Parallel = function(params) {
|
||||
|
||||
this.params = params;
|
||||
|
|
|
|||
|
|
@ -100,6 +100,12 @@ TCAD.TWO.ParametricManager.prototype.p2lDistance = function(objs, promptCallback
|
|||
}
|
||||
};
|
||||
|
||||
TCAD.TWO.utils.constRef = function(value) {
|
||||
return function() {
|
||||
return value;
|
||||
};
|
||||
};
|
||||
|
||||
TCAD.TWO.ParametricManager.prototype.p2pDistance = function(objs, promptCallback) {
|
||||
var p = this._fetchTwoPoints(objs);
|
||||
var distance = new TCAD.Vector(p[1].x - p[0].x, p[1].y - p[0].y).length();
|
||||
|
|
@ -108,7 +114,7 @@ TCAD.TWO.ParametricManager.prototype.p2pDistance = function(objs, promptCallback
|
|||
if (promptDistance != null) {
|
||||
promptDistance = Number(promptDistance);
|
||||
if (promptDistance == promptDistance) { // check for NaN
|
||||
this.add(new TCAD.TWO.Constraints.P2PDistance(p[0], p[1], promptDistance));
|
||||
this.add(new TCAD.TWO.Constraints.P2PDistance(p[0], p[1], TCAD.TWO.utils.constRef(promptDistance)));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -315,3 +321,17 @@ TCAD.TWO.Constraints.P2PDistance.prototype.getSolveData = function() {
|
|||
this.p2.collectParams(params);
|
||||
return ['P2PDistance', params, [this.d]];
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.P2PDistanceV = function(p1, p2, d) {
|
||||
this.p1 = p1;
|
||||
this.p2 = p2;
|
||||
this.d = d;
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.P2PDistanceV.prototype.getSolveData = function() {
|
||||
var params = [];
|
||||
this.p1.collectParams(params);
|
||||
this.p2.collectParams(params);
|
||||
params.push(this.d);
|
||||
return ['P2PDistanceV', params];
|
||||
};
|
||||
Loading…
Reference in a new issue