Все, что вам для этого нужно, - тригонометрия.
Например, учитывая заданное вами расстояние ...
const distance = 15;
... вы вычисляете угол между двумяуказывает с Math.atan2
и умножает его синус или косинус на желаемое расстояние, синус для x
позиций и косинус для y
позиций:
distance * Math.sin(Math.atan(y, x))//for x
distance * Math.cos(Math.atan(y, x))//for y
Вот демос вашими точками данных:
const points = [
[83.5, 144],
[172.5, 71],
[281.5, 133],
[385.5, 77],
[437.5, 152]
];
const distance = 15;
const svg = d3.select("svg");
const polyline = svg.append("polyline")
.attr("points", points.join(" "));
const lines = svg.selectAll(null)
.data(d3.pairs(points))
.enter()
.append("line")
.attr("x1", d => d[0][0] - (distance * Math.sin(Math.atan2(d[0][1] - d[1][1], d[0][0] - d[1][0]))))
.attr("x2", d => d[1][0] - (distance * Math.sin(Math.atan2(d[0][1] - d[1][1], d[0][0] - d[1][0]))))
.attr("y1", d => d[0][1] + (distance * Math.cos(Math.atan2(d[0][1] - d[1][1], d[0][0] - d[1][0]))))
.attr("y2", d => d[1][1] + (distance * Math.cos(Math.atan2(d[0][1] - d[1][1], d[0][0] - d[1][0]))))
polyline {
fill: none;
stroke: blue;
stroke-width: 2px;
}
line {
stroke: red;
stroke-width: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="200"></svg>