Approximate Placement of Points
by Mert Ener
HTML
<canvas id="canvas" width="1000" height="600"></canvas>
CSS
html, body { margin:0; padding:0; }
canvas { display:block; border:1px solid #ccc; }
JavaScript
function generateCoordinates(matrix) {
const n = matrix.length;
const points = [];
points.push({ x: 0, y: 0 }); // P0
points.push({ x: matrix[0][1], y: 0 }); // P1
for (let i = 2; i < n; i++) {
const d0 = matrix[0][i];
const d1 = matrix[1][i];
const x1 = points[1].x;
const x = (d0 ** 2 - d1 ** 2 + x1 ** 2) / (2 * x1);
const yAbs = Math.sqrt(Math.max(0, d0 ** 2 - x ** 2));
// İki olasılığı dene: y+, y-
const candidate1 = { x, y: yAbs };
const candidate2 = { x, y: -yAbs };
// Hangisi mevcut noktalara daha yakınsa onu seç
const error1 = totalError(points, matrix, i, candidate1);
const error2 = totalError(points, matrix, i, candidate2);
const chosen = error1 < error2 ? candidate1 : candidate2;
points.push(chosen);
}
return points;
}
function totalError(points, matrix, i, candidate) {
let error = 0;
for (let j = 0; j < i; j++) {
const expected = matrix[j][i];
const dx = candidate.x - points[j].x;
const dy = candidate.y - points[j].y;
const dist = Math.sqrt(dx * dx + dy * dy);
error += Math.abs(dist - expected);
}
return error;
}
// 3. Noktaları çiz
function drawPoints(points, labels) {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const scale = 2;
const offsetX = canvas.width / 1.5;
const offsetY = canvas.height / 1.6;
ctx.clearRect(0, 0, canvas.width, canvas.height); // Önceki çizimleri temizle
points.forEach((p, i) => {
const x = offsetX - p.x * scale;
const y = offsetY + p.y * scale;
ctx.beginPath();
ctx.arc(x, y, 5, 0, 2 * Math.PI);
ctx.fillStyle = 'blue';
ctx.fill();
ctx.fillStyle = 'black';
ctx.font = '10px Arial';
ctx.fillText(labels[i], x + 6, y - 6); // Burada P0 yerine eyalet adı yazılır
});
}
const jsonText =...