viewof u1_sub = Inputs. range ([- 8 , 8 ], {
step : 0.5 ,
value : 4 ,
label : tex `u_1` ,
width : 180
})
viewof u2_sub = Inputs. range ([- 8 , 8 ], {
step : 0.5 ,
value : 3 ,
label : tex `u_2` ,
width : 180
})
// Vector v components for subtraction
viewof v1_sub = Inputs. range ([- 8 , 8 ], {
step : 0.5 ,
value : 1 ,
label : tex `v_1` ,
width : 180
})
viewof v2_sub = Inputs. range ([- 8 , 8 ], {
step : 0.5 ,
value : 2 ,
label : tex `v_2` ,
width : 180
})
// Display vectors and difference
html `<div style="text-align: center; font-size: 1.2em; margin: 1.5em 0;">
<p>
${ tex ` \m athbf{u} = \b egin{bmatrix} ${ u1_sub} \\ ${ u2_sub} \e nd{bmatrix}, \q uad \m athbf{v} = \b egin{bmatrix} ${ v1_sub} \\ ${ v2_sub} \e nd{bmatrix}, \q uad \m athbf{u} - \m athbf{v} = \b egin{bmatrix} ${ u1_sub - v1_sub} \\ ${ u2_sub - v2_sub} \e nd{bmatrix}` }
</p>
</div>`
Plot. plot ({
style : "overflow: visible; display: block; margin: 0 auto;" ,
width : 600 ,
height : 500 ,
grid : true ,
x : {label : "x_1" , domain : [- 12 , 12 ]},
y : {label : "x_2" , domain : [- 12 , 12 ]},
marks : [
Plot. ruleY ([0 ], {stroke : "#ddd" }),
Plot. ruleX ([0 ], {stroke : "#ddd" }),
// Vector u (blue)
Plot. arrow ([{x1 : 0 , y1 : 0 , x2 : u1_sub, y2 : u2_sub}], {
x1 : "x1" , y1 : "y1" , x2 : "x2" , y2 : "y2" ,
stroke : "steelblue" , strokeWidth : 3 , fill : "steelblue"
}),
// Vector v (red)
Plot. arrow ([{x1 : 0 , y1 : 0 , x2 : v1_sub, y2 : v2_sub}], {
x1 : "x1" , y1 : "y1" , x2 : "x2" , y2 : "y2" ,
stroke : "crimson" , strokeWidth : 3 , fill : "crimson"
}),
// Vector -v (red dashed)
Plot. arrow ([{x1 : 0 , y1 : 0 , x2 : - v1_sub, y2 : - v2_sub}], {
x1 : "x1" , y1 : "y1" , x2 : "x2" , y2 : "y2" ,
stroke : "crimson" , strokeWidth : 2 , fill : "crimson" ,
strokeDasharray : "5,5" , fillOpacity : 0.6
}),
// Vector difference u - v (purple)
Plot. arrow ([{x1 : 0 , y1 : 0 , x2 : u1_sub - v1_sub, y2 : u2_sub - v2_sub}], {
x1 : "x1" , y1 : "y1" , x2 : "x2" , y2 : "y2" ,
stroke : "purple" , strokeWidth : 3 , fill : "purple"
}),
// Vector from v to u (alternative representation)
Plot. arrow ([{x1 : v1_sub, y1 : v2_sub, x2 : u1_sub, y2 : u2_sub}], {
x1 : "x1" , y1 : "y1" , x2 : "x2" , y2 : "y2" ,
stroke : "#ff6600" , strokeWidth : 2 , fill : "#ff6600" ,
strokeDasharray : "3,3"
}),
// Labels
Plot. text ([
{x : u1_sub/ 2 , y : u2_sub/ 2 + 0.5 , text : "u" },
{x : v1_sub/ 2 , y : v2_sub/ 2 - 0.5 , text : "v" },
{x : - v1_sub/ 2 , y : - v2_sub/ 2 + 0.5 , text : "-v" },
{x : (u1_sub - v1_sub)/ 2 , y : (u2_sub - v2_sub)/ 2 + 0.8 , text : "u-v" }
], {
x : "x" , y : "y" , text : "text" , fontSize : 14 , fontWeight : "bold"
})
]
})