Published
Edited
Feb 2, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
lp2d(['x_1',-5,15,'x_2',-5,15],[500,500],[1,3,10,zexducours],[ [1,1,14] , [-2,3,12] , [2,-1,12] , [-1,0,0] , [0,-1,0] ])
Insert cell
Insert cell
Insert cell
Insert cell
viewof zexo5 = Inputs.range([-30, 100], { label: tex`z:`, value: -30, step: 1 })
Insert cell
lp2d(['x_1',-3,10,'x_2',-3,10],[400,400],[1,4,42,zexo5],[ [-3,1,3] , [1,-3,3] , [3,-1,17] , [1,3,19] , [-1,0,0] , [0,-1,0] ])
Insert cell
//lp2d(['x_1',-3,10,'x_2',-3,10],[800,800],[1,4,42,zexo5],[ [-3,1,3] , [1,-3,3] , [3,-1,17] , [-1,0,0] , [0,-1,0] ])
Insert cell
function lp2d(zoneaffichee,fenetre,zeco,contraintes) {
// la zone à afficher et à graduer
var xnom=zoneaffichee[0] , xmin=zoneaffichee[1] , xmax=zoneaffichee[2] ,
ynom=zoneaffichee[3] , ymin=zoneaffichee[4] , ymax=zoneaffichee[5]
// la fenêtre à l'écran (en pixels)
var width=fenetre[0], height=fenetre[1]
// zone de dessin (3 fois plus lmarge et plus haute que la zone affichée !)
var _xmin=xmin-(xmax-xmin) , _xmax=xmax+(xmax-xmin) , _ymin=xmin-(ymax-ymin) , _ymax=xmax+(ymax-ymin)
function contrainte(t) {
var a = t[0] , b = t[1] , c = t[2] ;
// contrainte a*x + b*y <= c
//////////////////////////////////////////////////
/////////////
// contrainte 0*x + b*y <= c LORSQUE a==0
// <=> y <= c/b LORSQUE a==0 ET b>0
// <=> y >= c/b LORSQUE a==0 ET b<0
if (a==0 && b>0) // on colorie AU-DESSUS
{ var _points = [ [_xmin,c/b],[_xmax,c/b] , [_xmax,_ymax],[_xmin,_ymax],[_xmin,c/b] ]}
else if (a==0 && b<0) // on colorie EN-DESSOUS
{ var _points = [ [_xmin,c/b],[_xmax,c/b] , [_xmax,_ymin],[_xmin,_ymin],[_xmin,c/b] ]}
// contrainte a*x + b*y <= c
// <=> a*x <= (c - b*y)
// <=> x <= (c - b*y)/a LORSQUE a>0,
// <=> x >= (c - b*y)/a LORSQUE a<0
else if (a>0) // on colorie À DROITE
{ _points = [ [(c-b*_ymin)/a,_ymin] , [(c-b*_ymax)/a,_ymax] , [_xmax,_ymax], [_xmax,_ymin], [(c-b*_ymin)/a,_ymin] ] }
else { _points = [ [(c-b*_ymin)/a,_ymin] , [(c-b*_ymax)/a,_ymax] , [_xmin,_ymax], [_xmin,_ymin], [(c-b*_ymin)/a,_ymin] ] }
// console.log(_points)
return Plot.line( _points , { fill: "lightblue", opacity : 0.25 , stroke: "blue", strokeWidth: 3 } )
}
///////
var marks = []
////////// fonction éco a*x + b*y + c == z [*]
// si a == 0 :
// [*] <=> b*y + c = z <=> y == (z-c)/b
// sinon [*] <=> x == (z - c - b*y)/a
var a=zeco[0] , b=zeco[1] , c=zeco[2] , z=zeco[3]
if (a==0)
{marks.push( Plot.line( [ [_xmin, (z-c)/b], [_xmax, (z-c)/b] ] , { stroke: "red", strokeWidth: 2} ) )}
else
{marks.push( Plot.line( [ [(z-c-b*_xmin)/a, _xmin] , [(z-c-b*_xmax)/a, _xmax] ] , { stroke: "red", strokeWidth: 2} ) )}
/////// les contraintes
for (let t of contraintes)
{ marks.push( contrainte(t) ) }
//////// les axes et graduations
marks.push( Plot.ruleX([xmin]) )
marks.push( Plot.ruleY([ymin]) )
//////// le gradient (vecteur colinéaire et de même sens que le gradient…)

////////
return Plot.plot({
grid : true,
x: { domain: [xmin, xmax] , label: xnom},
y: { domain: [ymin, ymax] , label: ynom},
width: width ,
height: height ,
marks: marks ,
grid : true
})}
Insert cell
//Plotly = require("plotly.js-dist")
Insert cell
// d3 = require("d3@6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more