1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
<html>
<head>
<title>JSXGraph example</title>
<link rel="stylesheet" type="text/css" href="../distrib/jsxgraph.css" />
<script type="text/javascript" src="/javascript/jquery/jquery.min.js"></script>
<script type="text/javascript" src="../distrib/jsxgraphcore.js"></script>
</head>
<body>
<h2>Create a point with simple clicks. Uses jQuery via Google CDN.</h2>
<div style="width:800px">
<div id="jxgbox" class="jxgbox" style="width:800px; height:800px; float:left; background-image:url(medsand.gif);"></div>
</div>
<div id="debug" style="display:block;"></div>
<script type="text/javascript">
/* <![CDATA[ */
(function() {
JXG.Options.renderer = 'canvas';
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-10, 10, 10, -10], axis:false, grid:true}),
//
// bestFit, the best-fitting circle or line is found by least-squares fitting.
//
bestFit = function(p, testOnly) {
var i, angle, co, si,
delta = 0.8,
isLine = false,
M = [], y = [], MT, B, c, z, n, d, xm, ym, r;
// Having constructed the points, we can fit a circle
// through the point set, consisting of n points.
// The (n times 3) matrix consists of
// x_1, y_1, 1
// x_2, y_2, 1
// ...
// x_n, y_n, 1
// where x_i, y_i is the position of point p_i
// The vector y of length n consists of
// x_i*x_i+y_i*y_i
// for i=1,...n.
n = p.length;
for (i=0;i<n;i++) {
M.push([p[i].length ? p[i][0] : p[i].X(), p[i].length ? p[i][1] : p[i].Y(), 1.0]);
y.push(M[i][0]*M[i][0]+M[i][1]*M[i][1]);
}
// Now, the general linear least-square fitting problem
// min_z || M*z - y||_2^2
// is solved by solving the system of linear equations
// (M^T*M) * z = (M^T*y)
// with Gauss elimination.
MT = JXG.Math.transpose(M);
B = JXG.Math.matMatMult(MT, M);
c = JXG.Math.matVecMult(MT, y);
if (Math.abs(JXG.Math.Numerics.det(B))<Math.sqrt(JXG.Math.eps)) {
isLine = true;
} else {
z = JXG.Math.Numerics.Gauss(B, c);
xm = z[0]*0.5;
ym = z[1]*0.5; // xm, ym : center of the circle
r = Math.sqrt(z[2]+xm*xm+ym*ym); // r: radius
d = JXG.Math.Geometry.distance(points[0], points[points.length-1]);
if (r>20) {
//if (r*Math.sqrt(board.stretchX*board.stretchX+board.stretchY*board.stretchY)>500) {
isLine = true;
}
}
if (isLine) {
if (!testOnly)
board.create('line',[points[0], points[points.length-1]], {strokeColor:'green'});
} else {
if (!testOnly)
board.create('circle', [[xm,ym],r]);
}
return isLine;
}, // End of bestFit
/*
bestFitOld = function(p) {
var i, j, r = [], rbar = [], A = [
[0,0,0],
[0,0,0],
[0,0,0]
], n, d,
eigen, minIndex, minE, ev, c, xm, ym, zm, den, nom, radius;
n = p.length;
for (i = 0; i < n; i++) {
r.push([1.0, p[i].length ? p[i][0] : p[i].X(), p[i].length ? p[i][1] : p[i].Y()]);
d = r[i][0] * r[i][0] + r[i][1] * r[i][1] + r[i][2] * r[i][2];
r[i][0] = 1 - r[i][0] / d;
r[i][1] /= d;
r[i][2] /= d;
}
for (j = 0; j < 3; j++) {
for (i = 0,d = 0; i < n; i++) {
d += r[i][j];
}
d /= n;
rbar[j] = d;
for (i = 0; i < n; i++) {[zm,xm,ym]
r[i][j] -= d;
}
}
for (i = 0; i < n; i++) {
A[0][0] += r[i][0] * r[i][0];
A[0][1] += r[i][0] * r[i][1];
A[0][2] += r[i][0] * r[i][2];
A[1][0] += r[i][1] * r[i][0];
A[1][1] += r[i][1] * r[i][1];
A[1][2] += r[i][1] * r[i][2];
A[2][0] += r[i][2] * r[i][0];
A[2][1] += r[i][2] * r[i][1];
A[2][2] += r[i][2] * r[i][2];
}
eigen = JXG.Math.Numerics.Jacobi(A);
minIndex = 0;
minE = eigen[0][0][0];
for (j = 1; j < 3; j++) {
if (eigen[0][j][j] < minE) {
minIndex = j;
minE = eigen[0][j][j];
}
}
ev = [eigen[1][0][minIndex],eigen[1][1][minIndex],eigen[1][2][minIndex]];
d = Math.sqrt(ev[0]*ev[0] + ev[1]*ev[1] + ev[2]*ev[2]);
ev[0] /= d;
ev[1] /= d;
ev[2] /= d;
c = -(rbar[0] * ev[0] + rbar[1] * ev[1] + rbar[2] * ev[2]);
xm = -ev[1];
ym = -ev[2];
zm = 2.0 * (c + ev[0]);
// If c is close to zero, the best fittting object is a line.
// The best threshold parameter has yet to be determined.
// At the moment it is set to 0.01.
nom = xm * xm + ym * ym - 2.0 * c * zm;
den = zm * zm;
//console.log(c, zm, ev, rbar);
//console.log(nom, 500.0*Math.abs(den));
if (Math.abs(nom) > 500.0*Math.abs(den)) {
board.create('line',[points[0], points[points.length-1]], {strokeColor:'green'});
//board.create('line', [ev[0],xm,ym], {strokeColor:'green'});
} else {
radius = Math.sqrt((xm * xm + ym * ym - 2.0 * c * zm) / (zm * zm));
board.create('circle', [
[zm,xm,ym],
radius
]);
}
}, // end of bestFit()
*/
getMouseCoords = function(e) {
if (!document.all) { // not IE
var em = document.createEvent('MouseEvents'), i = 0;
if(e.targetTouches) { // always false
em.initMouseEvent('mousedown', true, false, this.containerObj, 0,
e.targetTouches[i].screenX, e.targetTouches[i].screenY,
e.targetTouches[i].clientX, e.targetTouches[i].clientY,
false, false, false, false, 0, null);
e = em;
}
}
var cPos = board.getCoordsTopLeftCorner(e),
absPos = JXG.getPosition(e),
dx = absPos[0]-cPos[0],
dy = absPos[1]-cPos[1];
return new JXG.Coords(JXG.COORDS_BY_SCREEN, [dx, dy], board);
},
mousedown,
points = [];
var draftcurve = board.create('curve',[[0],[0]],{curveType:'plot', strokeWidth:14, opacity:0.15});
draftcurve.updateDataArray = function() {
if (points.length<=0) return;
var p = JXG.Math.transpose(points);
this.dataX = p[0];
this.dataY = p[1];
};
draftcurve.highlight = function() {};
draftcurve.noHighlight = function() {};
var down = function(e) {
var coords = getMouseCoords(e),
i;
if(e.shiftKey)
return;
mousedown = true;
for(i in board.objects) {
if(JXG.isPoint(board.objects[i]) && board.objects[i].hasPoint(coords.scrCoords[1], coords.scrCoords[2])) {
mousedown = false;
break;
}
}
if(!mousedown)
return;
points = [];
points.push(coords.usrCoords.slice(1));
draftcurve.prepareUpdate().update().updateRenderer();
// prevent accidental text selection
if (e && e.preventDefault) {
e.preventDefault();
} else {
window.event.returnValue = false;
}
};
var move = function(e, m) {
if(mousedown) {
points.push(getMouseCoords(e).usrCoords.slice(1));
draftcurve.prepareUpdate().update().updateRenderer();
draftcurve.setProperty({strokeColor: (bestFit(points,true))?'green':'blue'});
}
};
var up = function(e){
if((e.fromTouch && points.length==1) || (!e.fromTouch && points.length<=15))
board.create('point', points[0], {fillColor:'#000000', fillOpacity:0.8, strokeWidth:0, size:4 });
else
bestFit(points, false);
mousedown = false;
points = [];
};
board.addHook(down, 'mousedown');
board.addHook(move, 'mousemove');
board.addHook(up, 'mouseup');
})();
/* ]]> */
</script>
</body>
</html>
|