hw3p helper
Bezier curve
by 蔡 育曄
HTML
<div id="info">THREE.JS 2D
<br>cubic Bezier curve
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js"></script>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
overflow: hidden;
}
JavaScript
var camera, scene, renderer;
var mouse = new THREE.Vector2();
var halfSize = 25;
var bezier, ctrlPolygon, ctrlPtMesh, ctrlPts;
var intersect;
//var p0x,p0y,p1x,p1y,p2x,p2y,p3x,p3y;
init();
animate();
function makeBezier() {
var curve = new THREE.CubicBezierCurve3(ctrlPts[0], ctrlPts[1], ctrlPts[2], ctrlPts[3]);
var geometry = new THREE.Geometry();
geometry.vertices = curve.getSpacedPoints(26);
var material = new THREE.LineBasicMaterial({
color: 0xffff00
});
bezier = new THREE.Line(geometry, material);
return bezier;
}
function makeCircle(r) {
let geometry = new THREE.Geometry();
let theta = Math.PI*2/64;
for (let i = 0; i <= 64; i++) {
geometry.vertices.push (
new THREE.Vector3(r*Math.cos(theta*i), r*Math.sin(theta*i),0));
}
return new THREE.Line (geometry, new THREE.LineBasicMaterial({color:0xffff00}));
}
function uniroot ( func, lowerLimit, upperLimit, errorTol, maxIter ) {
var a = lowerLimit
, b = upperLimit
, c = a
, fa = func(a)
, fb = func(b)
, fc = fa
, s = 0
, fs = 0
, tol_act // Actual tolerance
, new_step // Step at this iteration
, prev_step // Distance from the last but one to the last approximation
, p // Interpolation step is calculated in the form p/q; division is delayed until the last moment
, q
;
errorTol = errorTol || 0;
maxIter = maxIter || 1000;
while ( maxIter-- > 0 ) {
prev_step = b - a;
if ( Math.abs(fc) < Math.abs(fb) ) {
// Swap data for b to be the best approximation
a = b, b = c, c = a;
fa = fb, fb = fc, fc = fa;
}
tol_act = 1e-15 * Math.abs(b) + errorTol / 2;
new_step = ( c - b ) / 2;
if ( Math.abs(new_step) <= tol_act || fb === 0 ) {
return b; // Acceptable approx. is found
}
// Decide if the interpolation can be tried
if ( Math.abs(prev_step) >= tol_act && Math.abs(fa) > Math.abs(fb) ) {
// If prev_step was large enough and was in true...