Вот соответствующая часть моего кода JavaScript:< /p>
Код: Выделить всё
function drawTangentLine(x) {
// Current slope and tangent line drawing logic
// Limit x to the range 0 to 2.5
x = Math.min(Math.max(x, 0), 2.5); // Ensure x stays between 0 and 2.5
let y = x * x; // y = x^2 at x
let slope = 2 * x; // Derivative of y = x^2 is 2x
// Display the current slope
slopeValue.textContent = slope.toFixed(2);
// Calculate the tangent line
let tangentStartX = x - 0.5; // Start of tangent line
let tangentEndX = x + 0.5; // End of tangent line
// Ensure that the tangent line remains within canvas bounds (x range -2 to 2.5)
tangentStartX = Math.max(tangentStartX, -2); // Limit to left side of curve
tangentEndX = Math.min(tangentEndX, 2.5); // Limit to right side of curve
let tangentStartY = y - slope * (x - tangentStartX); // Adjust Y for slope
let tangentEndY = y + slope * (tangentEndX - x); // Adjust Y for slope
// Convert graph coordinates to canvas coordinates
let canvasTangentStartX = centerX + tangentStartX * scale;
let canvasTangentStartY = centerY - tangentStartY * scale;
let canvasTangentEndX = centerX + tangentEndX * scale;
let canvasTangentEndY = centerY - tangentEndY * scale;
// Draw the tangent line
ctx.beginPath();
ctx.moveTo(canvasTangentStartX, canvasTangentStartY);
ctx.lineTo(canvasTangentEndX, canvasTangentEndY);
ctx.strokeStyle = '#008080'; // Greenish color for tangent
ctx.lineWidth = 3;
ctx.stroke();
// Draw the point of tangency
let canvasX = centerX + x * scale;
let canvasY = centerY - y * scale;
ctx.beginPath();
ctx.arc(canvasX, canvasY, 6, 0, 2 * Math.PI);
ctx.fillStyle = '#0000FF'; // Blue dot
ctx.fill();
}
Код: Выделить всё
Änderungsfaktor
Änderungsfaktor
Die Steigung beträgt: 0.00
If you drag the tangent line along the curve from \( x = 0 \) to \( x = 2.5 \), the tangent line gets steeper. This also means that the value of the slope becomes greater.
Какие корректировки следует внести, чтобы касательная линия и точка касания оставались внутри холста?

< /p>
И в этом проблема.

Подробнее здесь: https://stackoverflow.com/questions/790 ... ope-calcul