JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/jsxgraph/distrib/jsxgraphcore.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jsxgraph/distrib/jsxgraph.css">
<div id="jxgbox" class="jxgbox" style="width:500px; height:500px">
</div>
JavaScript
const board = JXG.JSXGraph.initBoard('jxgbox', {
boundingbox: [-5, 5, 5, -5], axis:true
});
var isInside = function(pos, path) {
var len = path.numberPoints, i, j,
x = pos[0],
y = pos[1],
c = false;
// Jordan's method, see https://en.wikipedia.org/wiki/Point_in_polygon
for (i = 0, j = len - 1; i < len; j = i++) {
if (( (path.points[i].usrCoords[2] > y) !== (path.points[j].usrCoords[2] > y) ) &&
(x < (path.points[j].usrCoords[1] - path.points[i].usrCoords[1]) * (y - path.points[i].usrCoords[2]) /
(path.points[j].usrCoords[2] - path.points[i].usrCoords[2]) + path.points[i].usrCoords[1])) {
c = !c;
}
}
return c;
};
var pol = board.create('polygon', [[-3, -3], [3, -3], [3, 3], [-3, 3]]);
var li = board.create('line', [[3, -4], [-2, 4]]);
var in1 = board.create('inequality', [li]);
var in2 = board.create('inequality', [li], {inverse: true, fillColor: 'pink'});
var isect1 = board.create('curve', [[], []], {fillColor: 'yellow', fillOpacity: 0.3});
isect1.updateDataArray = function() {
var a = JXG.Math.Clip.intersection(pol, in1, this.board);
this.dataX = a[0];
this.dataY = a[1];
};
var isect2 = board.create('curve', [[], []], {fillColor: 'blue', fillOpacity: 0.3});
isect2.updateDataArray = function() {
var a = JXG.Math.Clip.intersection(pol, in2, this.board);
this.dataX = a[0];
this.dataY = a[1];
};
board.on('down', function(evt) {
var pos = this.getUsrCoordsOfMouse(evt),
path, isIn,
list = [isect1, isect2];
for (path of list) {
isIn = isInside(pos, path);
if (isIn) {
path.setAttribute({fillColor: 'gray'});
}
}
});
board.update();