A convex combination of three vectors in the plane allows us to represent any vector in the convex hull determined by those three vectors.
xxxxxxxxxx
<meta charset="utf-8">
<style>
svg {
pointer-events: all;
cursor: none;
}
.axis path,
.axis line {
fill: none;
stroke: #a2a2a2;
shape-rendering: crispEdges;
}
.axis text {
fill: #a2a2a2;
pointer-events: none;
}
.point {
stroke: #000000;
stroke-width: .5px;
}
#marker {
fill: #5c5c5c;
stroke: none;
stroke-width: 2px;
}
.vector {
stroke: #5c5c5c;
stroke-dasharray: 5, 5;
stroke-width: 2px;
}
.vstar {
marker-end: url(#marker);
stroke: #000000;
stroke-width: 2px;
}
.support {
stroke-linecap: round;
stroke-width: 6px;
}
</style>
<svg width="960" height="500">
<marker id="marker"
viewBox="0 0 6 6"
refY="3"
markerWidth="4"
markerHeight="4"
orient="auto">
<path d="M0,3v-3l6,3l-6,3z"></path>
</marker>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="numeric.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 20, left: 20},
padding = {top: 40, right: 60, bottom: 40, left: 60},
outerWidth = 960,
outerHeight = 500,
innerWidth = outerWidth - margin.left - margin.right,
innerHeight = outerHeight - margin.top - margin.bottom,
width = innerWidth - padding.left - padding.right,
height = innerHeight - padding.top - padding.bottom
radius = 5;
var svg = d3.select("svg").on("mousemove", mousemove);
var svg = d3.select("svg").append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var g = svg.append("g")
.attr("transform", "translate(" + padding.left + "," + padding.top + ")");
var color = d3.scale.category10();
var x = d3.scale.linear().domain([0, width]).range([0, width]);
var y = d3.scale.linear().domain([0, height]).range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickPadding(8);
var yAxis = d3.svg.axis()
.scale(y)
.tickPadding(8)
.orient("left")
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
g.append("g")
.attr("class", "axis axis--y")
.attr("transform", "translate(" + 0 + "," + 0 + ")")
.call(yAxis);
var vertices = [
{x: 300, y: 100},
{x: 400, y: 300},
{x: 500, y: 50}
];
var A;
var λ = d3.range(vertices.length).map(function(d) {return 0}); // weights
var p = [0,0];
update();
function update() {
A_x = [];
A_y = [];
A_sum = d3.range(vertices.length).map(function(d) {return 1});
for(var i = 0; i < vertices.length; i++) {
A_x.push(vertices[i].x);
A_y.push(vertices[i].y)
}
A = [];
A.push(A_x);
A.push(A_y);
A.push(A_sum);
var vectors = g.selectAll(".vector")
.data(vertices)
vectors.enter().append("path");
vectors
.attr("class", "vector")
.attr("d", function(d) {return "M " + x(0) + " " + y(0) + " " + "L " + x(d.x) + " " + y(d.y) });
var points = g.selectAll("circle")
.data(vertices);
points.enter().append("circle");
points
.attr("class", "point")
.style("fill", function(d, i) {return color(i)})
.attr("cx", function(d) {return x(d.x)})
.attr("cy", function(d) {return y(d.y)})
.attr("r", radius);
var supports = g.selectAll(".support")
.data(vertices);
supports.enter().append("path");
supports
.attr("class", "support")
.style("stroke", function(d, i) {return (positive(λ)) ? color(i) : "#f75454"})
.style("stroke-opacity", function(d, i) {return Math.abs(λ[i])})
.attr("d", function(d, i) {return "M " + x(0) + " " + y(0) + " " + "L " + (x(d.x * λ[i])) + " " + (y(d.y * λ[i])) });
var vstar = g.selectAll(".vstar")
.data([p].map(function(d){return {x: d[0], y: d[1]}}))
vstar.enter().append("path");
vstar
.attr("class", "vstar")
.attr("d", function(d) {return "M " + x(0) + " " + y(0) + " " + "L " + x(d.x) + " " + y(d.y) });
}
function mousemove() {
var mouse = d3.mouse(g.node());
p = [x.invert(mouse[0]), y.invert(mouse[1])]; // weights must sum to 1
var b = p.concat([1]);
// x1, x2, ..., xm are the "weights" that must sum to 1
λ = numeric.solve(A, b);
update();
}
function positive(vector) {
var l = vector.length;
for(var i = 0; i < l; i++) {
if(vector[i] < 0) return false;
}
return true;
}
</script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js