JSXGraph logo
JSXGraph
JSXGraph share

Share

Sketch polynomial
QR code
<iframe 
    src="https://www.jsxgraph.org/share/iframe/sketch-polynomial" 
    style="border: 1px solid black; overflow: hidden; width: 550px; aspect-ratio: 55 / 65;" 
    name="JSXGraph example: Sketch polynomial" 
    allowfullscreen
></iframe>
This code has to
<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 ShareAlike 4.0 International License.
    https://creativecommons.org/licenses/by-sa/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: [-10, 10, 10, -10],
        axis: true,
        pan: {
            enabled: true,
            needTwoFingers: true,
            needShift: true
        }
    });
    
    // Slider to set the degree of the polynomial
    var degree = board.create('slider', [
        [1, 8],
        [7, 8],
        [1, 3, 10]
    ], {
        name: 'degree',
        snapWidth: 1,
        digits: 0
    });
    
    // Define new board mode "sketch"
    board.BOARD_MODE_SKETCH = 0x0100;
    
    // Global variables
    var sketch, curve,
        points = [];
    
    // Init sketch curve
    board.on('down', function(evt) {
        if (board.mode !== board.BOARD_MODE_NONE) {
            return;
        }
        board.mode = board.BOARD_MODE_SKETCH;
        sketch = board.create('curve', [
            [],
            []
        ], {
            strokeColor: '#bbbbbb',
            lineCap: 'round',
            strokeWidth: 10
        });
    });
    
    // Finalize the sketch curve:
    // Take sketch and create spline curve
    board.on('up', function(evt) {
        var coords = [],
            i, p;
    
        if (board.mode !== board.BOARD_MODE_SKETCH) {
            return;
        }
    
        // Remove previous curve if it exists
        if (JXG.exists(curve)) {
            board.removeObject(curve);
            board.removeObject(points);
        }
    
        board.mode = board.BOARD_MODE_NONE;
    
        // Get list of coordinates from sketch curve
        for (i = 0; i < sketch.dataX.length; i++) {
            coords.push(new JXG.Coords(JXG.COORDS_BY_USER, [sketch.dataX[i], sketch.dataY[i]], board));
        }
    
        // Reduce the number of coordinate points to `degree + 1 - 2` inner coordinate points plus start point and end point.
        coords = JXG.Math.Numerics.Visvalingam(coords, degree.Value() - 1);
    
        // Convert the output of Visvalingam to JSXGraph points
        points = [];
        for (i = 0; i < coords.length; i++) {
            points.push(
                board.create('point', [coords[i].usrCoords[1], coords[i].usrCoords[2]], {
                    size: 5,
                    withLabel: false,
                    visible: true
                })
            );
        }
    
        // Create Lagrange polynomial from JSXGraph points
        curve = board.create('functiongraph', [JXG.Math.Numerics.lagrangePolynomial(points)], {
            strokeColor: '#000000',
            strokeWidth: 3,
            lineCap: 'round',
            fixed: false
        });
    
        // Remove the sketch curve 
        board.removeObject(sketch);
        // board.removeObject(points);
    });
    
    // Collect data points for the sketch curve
    board.on('move', function(evt, mode) {
        var pos, c;
    
        if (mode !== board.BOARD_MODE_SKETCH) {
            return;
        }
    
        pos = board.getMousePosition(evt);
        c = new JXG.Coords(JXG.COORDS_BY_SCREEN, pos, board);
        sketch.dataX.push(c.usrCoords[1]);
        sketch.dataY.push(c.usrCoords[2]);
        board.update();
    });
 </script> 
/*
This example is licensed under a 
Creative Commons Attribution ShareAlike 4.0 International License.
https://creativecommons.org/licenses/by-sa/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: [-10, 10, 10, -10],
    axis: true,
    pan: {
        enabled: true,
        needTwoFingers: true,
        needShift: true
    }
});

// Slider to set the degree of the polynomial
var degree = board.create('slider', [
    [1, 8],
    [7, 8],
    [1, 3, 10]
], {
    name: 'degree',
    snapWidth: 1,
    digits: 0
});

// Define new board mode "sketch"
board.BOARD_MODE_SKETCH = 0x0100;

// Global variables
var sketch, curve,
    points = [];

// Init sketch curve
board.on('down', function(evt) {
    if (board.mode !== board.BOARD_MODE_NONE) {
        return;
    }
    board.mode = board.BOARD_MODE_SKETCH;
    sketch = board.create('curve', [
        [],
        []
    ], {
        strokeColor: '#bbbbbb',
        lineCap: 'round',
        strokeWidth: 10
    });
});

// Finalize the sketch curve:
// Take sketch and create spline curve
board.on('up', function(evt) {
    var coords = [],
        i, p;

    if (board.mode !== board.BOARD_MODE_SKETCH) {
        return;
    }

    // Remove previous curve if it exists
    if (JXG.exists(curve)) {
        board.removeObject(curve);
        board.removeObject(points);
    }

    board.mode = board.BOARD_MODE_NONE;

    // Get list of coordinates from sketch curve
    for (i = 0; i < sketch.dataX.length; i++) {
        coords.push(new JXG.Coords(JXG.COORDS_BY_USER, [sketch.dataX[i], sketch.dataY[i]], board));
    }

    // Reduce the number of coordinate points to `degree + 1 - 2` inner coordinate points plus start point and end point.
    coords = JXG.Math.Numerics.Visvalingam(coords, degree.Value() - 1);

    // Convert the output of Visvalingam to JSXGraph points
    points = [];
    for (i = 0; i < coords.length; i++) {
        points.push(
            board.create('point', [coords[i].usrCoords[1], coords[i].usrCoords[2]], {
                size: 5,
                withLabel: false,
                visible: true
            })
        );
    }

    // Create Lagrange polynomial from JSXGraph points
    curve = board.create('functiongraph', [JXG.Math.Numerics.lagrangePolynomial(points)], {
        strokeColor: '#000000',
        strokeWidth: 3,
        lineCap: 'round',
        fixed: false
    });

    // Remove the sketch curve 
    board.removeObject(sketch);
    // board.removeObject(points);
});

// Collect data points for the sketch curve
board.on('move', function(evt, mode) {
    var pos, c;

    if (mode !== board.BOARD_MODE_SKETCH) {
        return;
    }

    pos = board.getMousePosition(evt);
    c = new JXG.Coords(JXG.COORDS_BY_SCREEN, pos, board);
    sketch.dataX.push(c.usrCoords[1]);
    sketch.dataY.push(c.usrCoords[2]);
    board.update();
});
<jsxgraph width="100%" aspect-ratio="1 / 1" title="Sketch polynomial" description="This construction was copied from JSXGraph examples database: BTW HERE SHOULD BE A GENERATED LINKuseGlobalJS="false">
   /*
   This example is licensed under a 
   Creative Commons Attribution ShareAlike 4.0 International License.
   https://creativecommons.org/licenses/by-sa/4.0/
   
   Please note you have to mention 
   The Center of Mobile Learning with Digital Technology
   in the credits.
   */
   
   const board = JXG.JSXGraph.initBoard(BOARDID, {
       boundingbox: [-10, 10, 10, -10],
       axis: true,
       pan: {
           enabled: true,
           needTwoFingers: true,
           needShift: true
       }
   });
   
   // Slider to set the degree of the polynomial
   var degree = board.create('slider', [
       [1, 8],
       [7, 8],
       [1, 3, 10]
   ], {
       name: 'degree',
       snapWidth: 1,
       digits: 0
   });
   
   // Define new board mode "sketch"
   board.BOARD_MODE_SKETCH = 0x0100;
   
   // Global variables
   var sketch, curve,
       points = [];
   
   // Init sketch curve
   board.on('down', function(evt) {
       if (board.mode !== board.BOARD_MODE_NONE) {
           return;
       }
       board.mode = board.BOARD_MODE_SKETCH;
       sketch = board.create('curve', [
           [],
           []
       ], {
           strokeColor: '#bbbbbb',
           lineCap: 'round',
           strokeWidth: 10
       });
   });
   
   // Finalize the sketch curve:
   // Take sketch and create spline curve
   board.on('up', function(evt) {
       var coords = [],
           i, p;
   
       if (board.mode !== board.BOARD_MODE_SKETCH) {
           return;
       }
   
       // Remove previous curve if it exists
       if (JXG.exists(curve)) {
           board.removeObject(curve);
           board.removeObject(points);
       }
   
       board.mode = board.BOARD_MODE_NONE;
   
       // Get list of coordinates from sketch curve
       for (i = 0; i < sketch.dataX.length; i++) {
           coords.push(new JXG.Coords(JXG.COORDS_BY_USER, [sketch.dataX[i], sketch.dataY[i]], board));
       }
   
       // Reduce the number of coordinate points to `degree + 1 - 2` inner coordinate points plus start point and end point.
       coords = JXG.Math.Numerics.Visvalingam(coords, degree.Value() - 1);
   
       // Convert the output of Visvalingam to JSXGraph points
       points = [];
       for (i = 0; i < coords.length; i++) {
           points.push(
               board.create('point', [coords[i].usrCoords[1], coords[i].usrCoords[2]], {
                   size: 5,
                   withLabel: false,
                   visible: true
               })
           );
       }
   
       // Create Lagrange polynomial from JSXGraph points
       curve = board.create('functiongraph', [JXG.Math.Numerics.lagrangePolynomial(points)], {
           strokeColor: '#000000',
           strokeWidth: 3,
           lineCap: 'round',
           fixed: false
       });
   
       // Remove the sketch curve 
       board.removeObject(sketch);
       // board.removeObject(points);
   });
   
   // Collect data points for the sketch curve
   board.on('move', function(evt, mode) {
       var pos, c;
   
       if (mode !== board.BOARD_MODE_SKETCH) {
           return;
       }
   
       pos = board.getMousePosition(evt);
       c = new JXG.Coords(JXG.COORDS_BY_SCREEN, pos, board);
       sketch.dataX.push(c.usrCoords[1]);
       sketch.dataY.push(c.usrCoords[2]);
       board.update();
   });
</jsxgraph>

Sketch polynomial

The users can sketch a curve (thick grey line). The resulting point cloud is converted by the Visvalingam algorithm to a list consisting of a start point and an end point, as well as internal points. The overall number of these points is given as the slider value `degree.Value() - 1 + 2`. Then, these `degree.Value() + 1` points are interpolated by a Lagrange polynomial of degree `degree.Value()`.
// Define the id of your board in BOARDID

const board = JXG.JSXGraph.initBoard(BOARDID, {
    boundingbox: [-10, 10, 10, -10],
    axis: true,
    pan: {
        enabled: true,
        needTwoFingers: true,
        needShift: true
    }
});

// Slider to set the degree of the polynomial
var degree = board.create('slider', [
    [1, 8],
    [7, 8],
    [1, 3, 10]
], {
    name: 'degree',
    snapWidth: 1,
    digits: 0
});

// Define new board mode "sketch"
board.BOARD_MODE_SKETCH = 0x0100;

// Global variables
var sketch, curve,
    points = [];

// Init sketch curve
board.on('down', function(evt) {
    if (board.mode !== board.BOARD_MODE_NONE) {
        return;
    }
    board.mode = board.BOARD_MODE_SKETCH;
    sketch = board.create('curve', [
        [],
        []
    ], {
        strokeColor: '#bbbbbb',
        lineCap: 'round',
        strokeWidth: 10
    });
});

// Finalize the sketch curve:
// Take sketch and create spline curve
board.on('up', function(evt) {
    var coords = [],
        i, p;

    if (board.mode !== board.BOARD_MODE_SKETCH) {
        return;
    }

    // Remove previous curve if it exists
    if (JXG.exists(curve)) {
        board.removeObject(curve);
        board.removeObject(points);
    }

    board.mode = board.BOARD_MODE_NONE;

    // Get list of coordinates from sketch curve
    for (i = 0; i < sketch.dataX.length; i++) {
        coords.push(new JXG.Coords(JXG.COORDS_BY_USER, [sketch.dataX[i], sketch.dataY[i]], board));
    }

    // Reduce the number of coordinate points to `degree + 1 - 2` inner coordinate points plus start point and end point.
    coords = JXG.Math.Numerics.Visvalingam(coords, degree.Value() - 1);

    // Convert the output of Visvalingam to JSXGraph points
    points = [];
    for (i = 0; i < coords.length; i++) {
        points.push(
            board.create('point', [coords[i].usrCoords[1], coords[i].usrCoords[2]], {
                size: 5,
                withLabel: false,
                visible: true
            })
        );
    }

    // Create Lagrange polynomial from JSXGraph points
    curve = board.create('functiongraph', [JXG.Math.Numerics.lagrangePolynomial(points)], {
        strokeColor: '#000000',
        strokeWidth: 3,
        lineCap: 'round',
        fixed: false
    });

    // Remove the sketch curve 
    board.removeObject(sketch);
    // board.removeObject(points);
});

// Collect data points for the sketch curve
board.on('move', function(evt, mode) {
    var pos, c;

    if (mode !== board.BOARD_MODE_SKETCH) {
        return;
    }

    pos = board.getMousePosition(evt);
    c = new JXG.Coords(JXG.COORDS_BY_SCREEN, pos, board);
    sketch.dataX.push(c.usrCoords[1]);
    sketch.dataY.push(c.usrCoords[2]);
    board.update();
});

license

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