return page history
α-wwwiki
::
history/math_curves/20130921-215736.txt
editor : alpha [82.255.19.110] 2013/09/21 21:57:36 _h1 math Curves _p Begining to play with an interface for defining and drawing simple math curves. A work in progress ! Have a look to [[pForms]] for more complex geometry. {div {@ style="position:relative;"} {canvas {@ id="mycanvas" width="600" height="600" style="background:#ffe; border:1px dashed black; box-shadow:0 0 8px black;" } } } {p First, once {input {@ type="submit" value="include lambda3D" onclick="°° var js = document.createElement('script'); js.src = 'plugins/lambda3D.js'; document.body.appendChild( js ); this.value = 'OK, lambda3D is included !'; this.disabled='disabled'; °°"}} then play with the math editor and {input {@ type="submit" value="draw" onclick="°° var js = document.createElement('script'); var input = getId('code').value; js.innerHTML = decode_html_entities(input); // < ,> document.head.appendChild( js ); function curve4D (curve) { for (var i=0; i < curve.length; i++) curve[i] = {x:curve[i].x, y:curve[i].y, z:0, w:1}; return curve; } display = function ( can ) { var ctx = W3D.init2D( can ); var scale = 2; // can be modified ! W3D.init3D( 0, 0, 0, 500000, scale ); ctx.save(); ctx.beginPath(); ctx.strokeStyle = '#000'; ctx.lineWidth = 3; W3D.draw( curve4D (courbe() ) ); ctx.restore(); } getId('mycanvas').innerHTML = display('mycanvas'); document.head.removeChild( js ); °°"}}.} {div {@ style="position:absolute; top:100px; left:600px;opacity:0.8"}{drag}{textarea {@ id="code" style="width:500px;height:500px;padding:5px;"}°° function cercle (r, N) { var s = 300; var da = 2*Math.PI/N; for (var curve = [], i=0; i <= N; i++) { curve[i] = { x: s*r*Math.cos(da*i), y: s*r*Math.sin(da*i) }; } return curve; } function parabole (k, delta, N) { var s = 300; var dx = (delta[1]-delta[0])/N; for (var curve = [], i=0; i <= N; i++) { var u = delta[0] + dx*i; curve[i] = { x: s*u, y: s*(k[0]*u*u + k[1]*u +k[2]) }; } return curve; } function cubique (k, delta, N) { var s = 300; var dx = (delta[1]-delta[0])/N; for (var curve = [], i=0; i <= N; i++) { var u = delta[0] + dx*i; curve[i] = { x: s*u, y: s*(k[0]*u*u*u + k[1]*u*u +k[2]*u + k[3]) }; } return curve; } function pL3 ( N ) { var s = 300; var pl3 = [ {x:-0.5*s, y:-0.0*s}, {x: 0.0*s, y: 1.0*s}, {x: 0.5*s, y:-0.5*s} ] return PF.build( pl3, N ); } function pL4 ( N ) { var s = 300; var pl4 = [ {x:-0.5*s, y:-0.5*s}, {x:-0.2*s, y: 1.0*s}, {x: 0.2*s, y:-0.5*s}, {x: 0.5*s, y: 0.5*s} ] return PF.build( pl4, N ); } function courbe () { var choix = 5; if (choix == 1) return cercle( 0.5, 30 ); else if (choix == 2) return parabole( [4, 0, -0.5], [-0.5, 0.5], 30 ); else if (choix == 3) return cubique( [12, 4, 0, -0.2], [-0.5, 0.5], 50 ); else if (choix == 4) return pL3( 6 ) else if (choix == 5) return pL4( 6 ) else return "nada" } °°}}