JSXGraph logo
JSXGraph
JSXGraph share

Share

B-splines
QR code
<iframe 
    src="https://jsxgraph.uni-bayreuth.de/share/iframe/b-splines" 
    style="border: 1px solid black; overflow: hidden; width: 550px; aspect-ratio: 55 / 65;" 
    name="JSXGraph example: B-splines" 
    allowfullscreen
></iframe>
This code has to
<button onClick="addSegment();">Add random segment</button>
<button onClick="removeSegment();">Remove last segment</button>


<div id="board-0-wrapper" class="jxgbox-wrapper " style="width: 100%; ">
   <div id="board-0" class="jxgbox" style="aspect-ratio: 1 / 1; width: 100%;" data-ar="1 / 1"></div>
</div>

<script type = "text/javascript"> 
    /*
    This example is licensed under a 
    Creative Commons Attribution 4.0 International License.
    https://creativecommons.org/licenses/by/4.0/
    
    Please note you have to mention 
    The Center of Mobile Learning with Digital Technology
    in the credits.
    */
    
    const BOARDID = 'board-0';

    const board = JXG.JSXGraph.initBoard(BOARDID, {
        boundingbox: [-4, 4, 4, -4],
        keepaspectratio: true,
        axis: true
    });
    
    var p = [],
        col = 'red';
    
    // Create initial 7 points
    p.push(board.create('point', [2, 1], {color: col}));
    p.push(board.create('point', [0.75, 2.5], {color: col}));
    p.push(board.create('point', [-0.3, 0.3], {color: col}));
    p.push(board.create('point', [-3, 1], {color: col}));
    p.push(board.create('point', [-0.75, -2.5], {color: col}));
    p.push(board.create('point', [1.5, -2.8], {color: col}));
    p.push(board.create('point', [2, -0.5], {color: col}));
    
    // Create B-spline curve
    var c = board.create('curve', JXG.Math.Numerics.bspline(p, 4), {
        strokecolor: 'blue', strokeOpacity: 0.6, strokeWidth: 5});
    
    var addSegment = function() {
        p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
            strokeColor: col,
            fillColor: col
        }));
        board.update();
    };
    
    var removeSegment = function() {
        if (p.length > 2) {
            board.removeObject(p[p.length - 1]); // remove the last point from the list of objects
            p.splice(p.length - 1, 1); // remove the last point from the point array.
            board.update();
        }
    };
 </script> 
/*
This example is licensed under a 
Creative Commons Attribution 4.0 International License.
https://creativecommons.org/licenses/by/4.0/

Please note you have to mention 
The Center of Mobile Learning with Digital Technology
in the credits.
*/

const BOARDID = 'your_div_id'; // Insert your id here!

const board = JXG.JSXGraph.initBoard(BOARDID, {
    boundingbox: [-4, 4, 4, -4],
    keepaspectratio: true,
    axis: true
});

var p = [],
    col = 'red';

// Create initial 7 points
p.push(board.create('point', [2, 1], {color: col}));
p.push(board.create('point', [0.75, 2.5], {color: col}));
p.push(board.create('point', [-0.3, 0.3], {color: col}));
p.push(board.create('point', [-3, 1], {color: col}));
p.push(board.create('point', [-0.75, -2.5], {color: col}));
p.push(board.create('point', [1.5, -2.8], {color: col}));
p.push(board.create('point', [2, -0.5], {color: col}));

// Create B-spline curve
var c = board.create('curve', JXG.Math.Numerics.bspline(p, 4), {
    strokecolor: 'blue', strokeOpacity: 0.6, strokeWidth: 5});

var addSegment = function() {
    p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
        strokeColor: col,
        fillColor: col
    }));
    board.update();
};

var removeSegment = function() {
    if (p.length > 2) {
        board.removeObject(p[p.length - 1]); // remove the last point from the list of objects
        p.splice(p.length - 1, 1); // remove the last point from the point array.
        board.update();
    }
};

B-splines

The points in this example are connected by a cubic B-spline curve (i.e. order=4).
<button onClick="addSegment();">Add random segment</button>
<button onClick="removeSegment();">Remove last segment</button>
// Define the id of your board in BOARDID

const board = JXG.JSXGraph.initBoard(BOARDID, {
    boundingbox: [-4, 4, 4, -4],
    keepaspectratio: true,
    axis: true
});

var p = [],
    col = 'red';

// Create initial 7 points
p.push(board.create('point', [2, 1], {color: col}));
p.push(board.create('point', [0.75, 2.5], {color: col}));
p.push(board.create('point', [-0.3, 0.3], {color: col}));
p.push(board.create('point', [-3, 1], {color: col}));
p.push(board.create('point', [-0.75, -2.5], {color: col}));
p.push(board.create('point', [1.5, -2.8], {color: col}));
p.push(board.create('point', [2, -0.5], {color: col}));

// Create B-spline curve
var c = board.create('curve', JXG.Math.Numerics.bspline(p, 4), {
    strokecolor: 'blue', strokeOpacity: 0.6, strokeWidth: 5});

var addSegment = function() {
    p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
        strokeColor: col,
        fillColor: col
    }));
    board.update();
};

var removeSegment = function() {
    if (p.length > 2) {
        board.removeObject(p[p.length - 1]); // remove the last point from the list of objects
        p.splice(p.length - 1, 1); // remove the last point from the point array.
        board.update();
    }
};

license

This example is licensed under a Creative Commons Attribution 4.0 International License.
Please note you have to mention The Center of Mobile Learning with Digital Technology in the credits.