am building a react 2d room application, where we should draw a line to create the shape of the room, what i want is to create outer line with a gap of 20 ( will act as a wall with wall thickness of 20) for the drawn room the blue line is what am trying to make, but the room also can have a slope lines
`const gap = 20;
const createOuterPathWithGap = (points, gap) => {
const outerPoints = [];
for (let i = 0; i < points.length - 2; i += 2) {
const [x1, y1, x2, y2] = [points[i], points[i + 1], points[i + 2], points[i + 3]];
if (y1 === y2) { // Horizontal segment
if (x1 < x2) { // Moves right
outerPoints.push(x1, y1 - gap, x2, y2 - gap); // Above current segment
} else { // Moves left
outerPoints.push(x1, y1 + gap, x2, y2 + gap); // Below current segment
}
} else if (x1 === x2) { // Vertical segment
if (y1 < y2) { // Moves down
outerPoints.push(x1 + gap, y1, x2 + gap, y2); // To the right of the current segment
} else { // Moves up
outerPoints.push(x1 - gap, y1, x2 - gap, y2); // To the left of the current segment
}
}
}
const firstX = points[0];
const firstY = points[1];
const lastX = outerPoints[outerPoints.length - 2];
const lastY = outerPoints[outerPoints.length - 1];
if (firstY === lastY) {
outerPoints.push(firstX, lastY > firstY ? firstY - gap : firstY + gap);
} else if (firstX === lastX) {
outerPoints.push(lastX > firstX ? firstX - gap : firstX + gap, firstY);
}
return outerPoints;
};
const outerPoints = createOuterPathWithGap(points, gap); `
this is the function that i just created to make the outer Points logic but it did not worked, i have been stuck in this for a week, please some one help
Ahmad Alshurafa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.