mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-08 09:24:18 +01:00
extract numeric derivative calculation to reuse by other constraints
This commit is contained in:
parent
cfae03f291
commit
ca45a3fd01
1 changed files with 18 additions and 17 deletions
|
|
@ -514,7 +514,6 @@ function PointOnEllipse(params) {
|
||||||
const R = 6;
|
const R = 6;
|
||||||
|
|
||||||
this.error = function() {
|
this.error = function() {
|
||||||
const sq = x => x * x;
|
|
||||||
const px = params[PX].get();
|
const px = params[PX].get();
|
||||||
const py = params[PY].get();
|
const py = params[PY].get();
|
||||||
const ep1x = params[EP1X].get();
|
const ep1x = params[EP1X].get();
|
||||||
|
|
@ -526,8 +525,10 @@ function PointOnEllipse(params) {
|
||||||
const centerX = ep1x + (ep2x - ep1x) * 0.5;
|
const centerX = ep1x + (ep2x - ep1x) * 0.5;
|
||||||
const centerY = ep1y + (ep2y - ep1y) * 0.5;
|
const centerY = ep1y + (ep2y - ep1y) * 0.5;
|
||||||
const rotation = Math.atan2(ep2y - ep1y, ep2x - ep1x);
|
const rotation = Math.atan2(ep2y - ep1y, ep2x - ep1x);
|
||||||
|
|
||||||
let x = px - centerX;
|
let x = px - centerX;
|
||||||
let y = py - centerY;
|
let y = py - centerY;
|
||||||
|
|
||||||
const polarAngle = Math.atan2(y, x) - rotation;
|
const polarAngle = Math.atan2(y, x) - rotation;
|
||||||
const polarRadius = Math.sqrt(x*x + y*y);
|
const polarRadius = Math.sqrt(x*x + y*y);
|
||||||
const radiusX = Math.sqrt(sq(ep1x - ep2x) + sq(ep1y - ep2y)) * 0.5;
|
const radiusX = Math.sqrt(sq(ep1x - ep2x) + sq(ep1y - ep2y)) * 0.5;
|
||||||
|
|
@ -536,23 +537,21 @@ function PointOnEllipse(params) {
|
||||||
return L - polarRadius
|
return L - polarRadius
|
||||||
};
|
};
|
||||||
|
|
||||||
this.gradient = function(out) {
|
this.gradient = NumericGradient;
|
||||||
const h = 1;
|
}
|
||||||
const approx = (param) => {
|
|
||||||
const fx = this.error();
|
|
||||||
params[param].set(params[param].get() + h);
|
|
||||||
const fhx = this.error();
|
|
||||||
params[param].set(params[param].get() - h);
|
|
||||||
return (fhx - fx) / h;
|
|
||||||
};
|
|
||||||
|
|
||||||
out[PX] = approx(PX);
|
function NumericGradient(out) {
|
||||||
out[PY] = approx(PY);
|
const h = 1;
|
||||||
out[EP1X] = approx(EP1X);
|
const approx = (param) => {
|
||||||
out[EP1Y] = approx(EP1Y);
|
const fx = this.error();
|
||||||
out[EP2X] = approx(EP2X);
|
this.params[param].set(this.params[param].get() + h);
|
||||||
out[EP2Y] = approx(EP2Y);
|
const fhx = this.error();
|
||||||
out[R] = approx(R);
|
this.params[param].set(this.params[param].get() - h);
|
||||||
|
return (fhx - fx) / h;
|
||||||
|
};
|
||||||
|
|
||||||
|
for (var i = 0; i < out.length; i++) {
|
||||||
|
out[i] = approx(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -570,4 +569,6 @@ function rescale(grad, factor) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sq = x => x * x;
|
||||||
|
|
||||||
export {createByConstraintName, EqualsTo, ConstantWrapper}
|
export {createByConstraintName, EqualsTo, ConstantWrapper}
|
||||||
Loading…
Reference in a new issue