Проблема, с которой вы сталкиваетесь, связана с тем, что вы применяете алгоритм сглаживания ко всему квадрату, включая углы.
Поэтому, если вы сделаете снимок экрана перед установкой маркеров углов по горизонтали / вертикали, вы увидите это:
Я думаю, это достаточно ясно объясняет, почему у вашего второго сегмента есть диагональный маркер, потому что у предыдущего сегмента тоже есть один ...
Вот эскиз , демонстрирующий эффект, подобный тому, что вы пытаетесь достичь.
// Define constants.
const POINTS_AMOUNT = 7;
const SQUARE_WIDTH = 500;
const WAVE_AMPLITUDE = 25;
const SQUARE_OFFSET = new Point(100, 100);
// Initialize path variable, we will use this reference to delete the previous
// path on each frame.
let path;
function draw(time) {
// Delete existing path.
if (path) {
path.remove();
}
// Create new path.
path = new Path();
// First, draw the top side.
for (let i = 0; i < POINTS_AMOUNT; i++) {
// For each point of the line, create a wave effect by using sine
// function. Do not apply it on first and last point to make square
// corners stay constant.
const sinus = i === 0 || i === POINTS_AMOUNT - 1
? 0
: Math.sin(time * 3 + i);
const x = SQUARE_WIDTH / POINTS_AMOUNT * i;
const y = sinus * WAVE_AMPLITUDE;
path.add(new Point(x, y));
}
// Apply smoothing algorithm on the line.
path.smooth();
// Duplicate this line 3 times to form a square.
const right = path.clone().rotate(-90, path.lastSegment.point);
right.reverse();
const bottom = right.clone().rotate(-90, right.lastSegment.point);
bottom.reverse();
const left = bottom.clone().rotate(-90, bottom.lastSegment.point);
// Join all parts together.
path.join(right).join(bottom).join(left);
path.closed = true;
// Place the square somewhere we can see it.
path.translate(SQUARE_OFFSET);
// Stylize the square.
path.fullySelected = true;
path.fillColor = 'yellow';
}
function onFrame(event) {
draw(event.time);
}