JSXGraph logo
JSXGraph
JSXGraph share

Share

Bezier curves
QR code
<iframe 
    src="http://jsxgraph.uni-bayreuth.de/share/iframe/bezier-curves" 
    style="border: 1px solid black; overflow: hidden; width: 550px; aspect-ratio: 55 / 65;" 
    name="JSXGraph example: Bezier curves" 
    allowfullscreen
></iframe>
This code has to
<button onClick="addSegment();">Add 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
    });
    
    // Create initial points
    var p = [];
    var col = 'red';
    p.push(board.create('point', [2, 1], {color: col})); // data point
    col = 'blue';
    p.push(board.create('point', [0.75, 2.5], {color: col})); // control point
    p.push(board.create('point', [-0.3, 0.3], {color: col})); // control point
    col = 'red';
    p.push(board.create('point', [-3, 1], {color: col})); // data point
    col = 'blue';
    p.push(board.create('point', [-0.75, -2.5], {color: col})); // control point
    p.push(board.create('point', [1.5, -2.8], {color: col})); // control point
    col = 'red';
    p.push(board.create('point', [2, -0.5], {color: col})); // data point
    
    // Bezier curve
    var c = board.create('curve', JXG.Math.Numerics.bezier(p), {
        strokecolor: 'blue',
        strokeOpacity: 0.6,
        strokeWidth: 5
    });
    
    var addSegment = function() {
        col = 'blue';
        p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
            strokeColor: col,
            fillColor: col
        })); // control point
        p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
            strokeColor: col,
            fillColor: col
        })); // control point
        col = 'red';
        p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
            strokeColor: col,
            fillColor: col
        })); // data point
        board.update();
    };
    var removeSegment = function() {
        if (p.length > 4) {
            board.removeObject(p[p.length - 1]);
            board.removeObject(p[p.length - 2]);
            board.removeObject(p[p.length - 3]);
            p.splice(p.length - 3, 3);
        }
        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
});

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

// Bezier curve
var c = board.create('curve', JXG.Math.Numerics.bezier(p), {
    strokecolor: 'blue',
    strokeOpacity: 0.6,
    strokeWidth: 5
});

var addSegment = function() {
    col = 'blue';
    p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
        strokeColor: col,
        fillColor: col
    })); // control point
    p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
        strokeColor: col,
        fillColor: col
    })); // control point
    col = 'red';
    p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
        strokeColor: col,
        fillColor: col
    })); // data point
    board.update();
};
var removeSegment = function() {
    if (p.length > 4) {
        board.removeObject(p[p.length - 1]);
        board.removeObject(p[p.length - 2]);
        board.removeObject(p[p.length - 3]);
        p.splice(p.length - 3, 3);
    }
    board.update();
};

Bezier curves

The red points are connected by a cubic Bézier curve. The blue points are the control points.
<button onClick="addSegment();">Add 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
});

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

// Bezier curve
var c = board.create('curve', JXG.Math.Numerics.bezier(p), {
    strokecolor: 'blue',
    strokeOpacity: 0.6,
    strokeWidth: 5
});

var addSegment = function() {
    col = 'blue';
    p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
        strokeColor: col,
        fillColor: col
    })); // control point
    p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
        strokeColor: col,
        fillColor: col
    })); // control point
    col = 'red';
    p.push(board.create('point', [Math.random() * 8 - 4, Math.random() * 8 - 4], {
        strokeColor: col,
        fillColor: col
    })); // data point
    board.update();
};
var removeSegment = function() {
    if (p.length > 4) {
        board.removeObject(p[p.length - 1]);
        board.removeObject(p[p.length - 2]);
        board.removeObject(p[p.length - 3]);
        p.splice(p.length - 3, 3);
    }
    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.