auto-accept/www/js/svg.js

91 lines
3 KiB
JavaScript
Raw Normal View History

2020-12-17 12:09:22 +01:00
function getPathShapeBetweenPoints(p1, p2, corners) {
2020-12-22 12:04:42 +01:00
let cornerPoints = [/*[p1.x,p1.y]*/];
2020-12-17 12:09:22 +01:00
if(corners == 4){
2020-12-22 12:04:42 +01:00
// draw inbetween p1 & p2.
// let d = `M ${p1.x},${p1.y} L `;
// d += ` ${p1.x},${p2.y} ${p2.x},${p2.y} ${p2.x},${p1.y} ${p1.x},${p1.y} `;
cornerPoints.push([p1.x, p1.y]);
cornerPoints.push([p1.x, p2.y]);
cornerPoints.push([p2.x, p2.y]);
cornerPoints.push([p2.x, p1.y]);
2020-12-17 12:09:22 +01:00
}
else{
// find center of circle that describes the polygon of which p1 and p2 are part
// it's _not_ the center between the two points, as p1 and p2 are corners on the shape
2020-12-22 12:04:42 +01:00
2020-12-17 12:09:22 +01:00
// distance between p1 and p2
const length = Math.sqrt(Math.pow(p1.x-p2.x,2)+ Math.pow(p1.y - p2.y, 2));
const segmentAngle = Math.PI * 2 / corners;
2020-12-17 12:12:37 +01:00
// TODO: can we find a way to draw not from the first 3 segments, but have the
// cursor span as many segment as possible
2020-12-17 12:09:22 +01:00
// const segmentsAngle = Math.floor(180/segmentAngle) * segmentAngle;
const radius = (length*.5)/Math.sin(segmentAngle);
const rotation = Math.atan2((p2.y -p1.y),(p2.x - p1.x)) + (Math.PI/2-segmentAngle);
const dx = Math.cos(rotation) * radius;// * Math.sign(p2.x - p1.x);
const dy = Math.sin(rotation) * radius;// * Math.sign(p2.y - p1.y);
// we now have the center:
const cx = p2.x - dx;
const cy = p2.y - dy;
// startpoint in relation to circle
const sx = cx - p1.x;
const sy = cy - p1.y;
let startRot = Math.asin(sx / radius);
if(sy>0){startRot = -1 * startRot + Math.PI;}
2020-12-17 12:12:37 +01:00
2020-12-17 12:09:22 +01:00
for (let a = 0; a < Math.PI*2; a += segmentAngle) {
let x = radius * Math.sin(a-startRot) + cx;
let y = radius * Math.cos(a-startRot) + cy;
2020-12-22 12:04:42 +01:00
cornerPoints.push([x,y])
// d += ` ${x},${y}`;
// d += ` A 50 50 100 1 1 ${x} ${y+0.1} L `
2020-12-17 12:09:22 +01:00
}
}
2020-12-22 12:04:42 +01:00
2020-12-22 12:08:49 +01:00
// teken punten tussen alle punten
let allCornerPoints = [];
for (let index = 0; index < cornerPoints.length; index++) {
const c = cornerPoints[index];
const nextC = cornerPoints[(index+1)%cornerPoints.length];
allCornerPoints.push(c);
allCornerPoints.push([.5*(nextC[0]+c[0]), .5*(nextC[1]+c[1])]);
}
cornerPoints = allCornerPoints;
2020-12-22 12:04:42 +01:00
// create d:
let d = "";
const r = 10;
for (let index = 0; index < cornerPoints.length; index++) {
const c = cornerPoints[index];
const nextC = cornerPoints[(index+1)%cornerPoints.length];
const angle = Math.atan2(nextC[1] - c[1], nextC[0] - c[0]);
const dx = Math.cos(angle) * r;
const dy = Math.sin(angle) * r;
d += ` M ${c[0]+dx},${c[1]+dy} L `
d += ` ${nextC[0]-dx},${nextC[1]-dy}`;
// Thanks Anthony: https://stackoverflow.com/a/10477334
d += ` a ${r} ${r} 0 1,0 ${dx*2},${dy*2}`;
d += ` a ${r} ${r} 0 1,0 ${-dx*2},${-dy*2}`;
}
// console.log(d);
2020-12-17 12:09:22 +01:00
return d + ' Z';
}
export {getPathShapeBetweenPoints};