<div class="eulerSimulation">
<canvas id="maybeSVG">
<div class="info" id="formulation">
<div class="UI elements to manipulate with formulation"
<div class="info" id="detailed explanation to formulation">
here you go in js/css which is in one html.
Assistant
Certainly! Below is an example of a simple HTML document that demonstrates Euler's formula using plain JavaScript and CSS. Euler's formula states that for any real number ( x ):
[ e^{ix} = \cos(x) + i\sin(x) ]
In this simulation, we'll visualize this concept using a canvas to draw the unit circle and display both the real and imaginary parts of the complex exponential. The JavaScript will handle the drawing, and the CSS will style the elements.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Euler's Formula Visualization</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f4f4f9;
}
.eulerSimulation {
text-align: center;
}
canvas {
border: 1px solid #333;
border-radius: 8px;
margin-top: 20px;
}
#formulation {
margin: 15px 0;
}
.info {
color: #333;
}
.tooltip {
margin-top: 10px;
font-style: italic;
color: #666;
}
</style>
</head>
<body>
<div class="eulerSimulation">
<h1>Euler's Formula Visualization</h1>
<canvas id="maybeSVG" width="400" height="400"></canvas>
<div class="info" id="formulation">
Formula: \( e^{ix} = \cos(x) + i\sin(x) \)
</div>
<div class="tooltip" id="detailedExplanation">
Move the slider to change the angle x and see the corresponding position on the unit circle.
</div>
<input type="range" id="angleSlider" min="0" max="6.28319" step="0.01" value="0" />
</div>
<script>
const canvas = document.getElementById('maybeSVG');
const ctx = canvas.getContext('2d');
const angleSlider = document.getElementById('angleSlider');
function drawEuler(angle) {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw unit circle
ctx.beginPath();
ctx.arc(200, 200, 150, 0, Math.PI * 2);
ctx.strokeStyle = '#007bff';
ctx.stroke();
// Calculate real and imaginary parts
const x = Math.cos(angle);
const y = Math.sin(angle);
// Draw projection on the real axis
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.lineTo(200 + x * 150, 200 - y * 150);
ctx.strokeStyle = 'red';
ctx.stroke();
// Draw the point
ctx.beginPath();
ctx.arc(200 + x * 150, 200 - y * 150, 5, 0, Math.PI * 2);
ctx.fillStyle = 'green';
ctx.fill();
// Draw coordinates text
ctx.fillStyle = '#333';
ctx.font = '16px Arial';
ctx.fillText(`(${x.toFixed(2)}, ${y.toFixed(2)})`, 200 + x * 150 + 10, 200 - y * 150);
}
// Update the drawing based on the slider input
angleSlider.addEventListener('input', (event) => {
const angle = event.target.value;
drawEuler(angle);
});
// Initial draw
drawEuler(0);
</script>
</body>
</html>
Explanation:
HTML Structure:
A div container holds the canvas, the formula, a tooltip, and a slider.
The slider allows users to adjust the angle ( x ) from ( 0 ) to ( 2\pi ).
CSS Styles:
Simple styles for layout and presentation, including the canvas.
JavaScript Functionality:
The drawEuler function clears the canvas and draws the unit circle.
It calculates the ( \cos(x) ) and ( \sin(x) ) values based on the angle and draws the point on the circle along with the red line projection to the real axis.
An event listener updates the drawing as the slider is moved, reflecting changes in the angle.
With this code, you can visualize Euler's formula interactively and observe how the point moves around the unit circle as you adjust the angle.