-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextrema_rotation.html
175 lines (159 loc) · 6.82 KB
/
extrema_rotation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Sinus Curve with Rotation</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
canvas {
display: block;
margin: 20px auto;
border: 1px solid black;
}
.controls {
margin: 20px;
}
label {
font-size: 14px;
margin-right: 10px;
}
input[type="range"] {
width: 300px;
}
.value {
font-weight: bold;
}
</style>
</head>
<body>
<h1>Interactive Sinus Curve with Rotation</h1>
<canvas id="sinusCanvas" width="800" height="400"></canvas>
<div class="controls">
<label for="vectorX">X-Komponente (Periode):</label>
<input id="vectorX" type="range" min="0.5" max="5" step="0.1" value="2">
<span class="value" id="vectorXValue">2.0</span>
<br>
<label for="vectorY">Y-Komponente (Amplitude):</label>
<input id="vectorY" type="range" min="0.5" max="5" step="0.1" value="1">
<span class="value" id="vectorYValue">1.0</span>
<br>
<label for="rotation">Rotation (Grad):</label>
<input id="rotation" type="range" min="0" max="360" step="1" value="0">
<span class="value" id="rotationValue">0</span>°
</div>
<script>
const canvas = document.getElementById('sinusCanvas');
const ctx = canvas.getContext('2d');
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const vectorXSlider = document.getElementById('vectorX');
const vectorYSlider = document.getElementById('vectorY');
const rotationSlider = document.getElementById('rotation');
const vectorXValue = document.getElementById('vectorXValue');
const vectorYValue = document.getElementById('vectorYValue');
const rotationValue = document.getElementById('rotationValue');
/**
* Rotiert einen Vektor um einen Winkel in Grad.
* @param {number} x - X-Komponente des Vektors.
* @param {number} y - Y-Komponente des Vektors.
* @param {number} angle - Winkel in Grad.
* @returns {Object} - Neuer Vektor nach Rotation.
*/
function rotateVector(x, y, angle) {
const radians = (angle * Math.PI) / 180; // Grad in Radiant umrechnen
const rotatedX = x * Math.cos(radians) - y * Math.sin(radians);
const rotatedY = x * Math.sin(radians) + y * Math.cos(radians);
return { x: rotatedX, y: rotatedY };
}
/**
* Berechnet die Sinuskurve basierend auf einem Vektor.
* @param {number} xKomponente - Die X-Komponente des Vektors (Periode).
* @param {number} yKomponente - Die Y-Komponente des Vektors (Amplitude).
* @param {number} points - Anzahl der Punkte für die Kurve.
* @returns {Object} - Die Punkte der Sinuskurve.
*/
function generateSinusFromVector(xKomponente, yKomponente, points = 500) {
const amplitude = yKomponente;
const period = xKomponente * 4; // Periode basierend auf X-Komponente
const xValues = [];
const yValues = [];
const step = (4 * Math.PI) / points; // Bereich von -2π bis 2π
for (let i = 0; i <= points; i++) {
const x = -2 * Math.PI + i * step; // Start bei -2π
const y = amplitude * Math.sin((2 * Math.PI / period) * x);
xValues.push(x);
yValues.push(y);
}
return { xValues, yValues, vector: { x: xKomponente, y: yKomponente, period } };
}
function drawSinusCurve() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
// Dynamische Skalierung
const xScale = canvasWidth / (4 * Math.PI); // Skalierung der x-Achse
const yScale = canvasHeight / 4; // Skalierung der y-Achse (halbe Höhe)
let xKomponente = parseFloat(vectorXSlider.value);
let yKomponente = parseFloat(vectorYSlider.value);
const rotationAngle = parseFloat(rotationSlider.value);
// Vektor rotieren
const rotatedVector = rotateVector(xKomponente, yKomponente, rotationAngle);
xKomponente = rotatedVector.x;
yKomponente = rotatedVector.y;
const { xValues, yValues, vector } = generateSinusFromVector(xKomponente, yKomponente);
// Achsen zeichnen
ctx.beginPath();
ctx.moveTo(0, canvasHeight / 2);
ctx.lineTo(canvasWidth, canvasHeight / 2);
ctx.strokeStyle = 'black';
ctx.lineWidth = 1;
ctx.stroke();
ctx.beginPath();
ctx.moveTo(canvasWidth / 2, 0);
ctx.lineTo(canvasWidth / 2, canvasHeight);
ctx.strokeStyle = 'black';
ctx.lineWidth = 1;
ctx.stroke();
// Sinuskurve zeichnen
ctx.beginPath();
for (let i = 0; i < xValues.length; i++) {
const x = canvasWidth / 2 + xValues[i] * xScale; // Dynamische x-Skalierung
const y = canvasHeight / 2 - yValues[i] * yScale; // Dynamische y-Skalierung und zentriert
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.strokeStyle = 'blue';
ctx.lineWidth = 2;
ctx.stroke();
// Vektor zeichnen
const peakX = canvasWidth / 2 + (vector.period / 4) * xScale; // Hochpunkt bei Periode/4
const peakY = canvasHeight / 2 - vector.y * yScale; // Amplitude dynamisch skaliert
ctx.beginPath();
ctx.moveTo(canvasWidth / 2, canvasHeight / 2);
ctx.lineTo(peakX, peakY);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.stroke();
// Vektor-Komponenten anzeigen
ctx.fillStyle = 'red';
ctx.font = '14px Arial';
ctx.fillText(`(${rotatedVector.x.toFixed(1)}, ${rotatedVector.y.toFixed(1)})`, peakX + 10, peakY - 10);
// Update Slider-Werte
vectorXValue.textContent = rotatedVector.x.toFixed(1);
vectorYValue.textContent = rotatedVector.y.toFixed(1);
rotationValue.textContent = rotationAngle.toFixed(0);
}
// Event Listener für Slider
vectorXSlider.addEventListener('input', drawSinusCurve);
vectorYSlider.addEventListener('input', drawSinusCurve);
rotationSlider.addEventListener('input', drawSinusCurve);
// Initiale Darstellung
drawSinusCurve();
</script>
</body>
</html>