more c- lesson sinus.
#include <windows.h>
#include <stdio.h>
#include <math.h>
char text[1000];
int imax, jmax;
const double pi = 3.14159265358979323846;
double frequenz = 1.0;
int n = 100;
void Draw(HDC hdc)
{
double x, y, dx, xmin, xmax, ymin, ymax;
double xfaktor, yfaktor;
int i, j, i0, j0;
xmin = 0;
xmax = 2*pi*frequenz;
ymin = -1;
ymax = +1;
// Intervall
dx = (xmax - xmin)/n;
// Zoomfaktor
xfaktor = imax/(xmax-xmin);
yfaktor = jmax/(ymax-ymin);
// move
i0 = 0;
j0 = jmax/2;
MoveToEx(hdc, i0, j0, 0);
for (x = xmin; x <= xmax + dx/2; x += dx) {
// Funktion
y = sin(x);
// Pixel
i = i0 + xfaktor * x;
j = j0 - yfaktor * y;
// lines/boxes
LineTo(hdc, i, j);
Rectangle(hdc, i-2, j-2, i+3, j+3);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT m, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
if (m == WM_KEYDOWN) {
if (wParam == VK_UP && n < 5000) n = 3*n/2;
if (wParam == VK_DOWN && n > 2) n = 2*n/3;
if (wParam == VK_LEFT && frequenz < 1024) frequenz *= 2;
if (wParam == VK_RIGHT && frequenz > 0.25) frequenz /= 2;
InvalidateRect(hwnd, 0, 0);
}
else if (m == WM_PAINT) {
hdc = GetDC(hwnd);
Rectangle(hdc, -1, -1, imax+1, jmax+1);
Draw(hdc);
ReleaseDC(hwnd, hdc);
ValidateRect(hwnd, 0);
sprintf(text, "Sinus: f = %.2f n = %d", frequenz, n);
SetWindowText(hwnd, text);
}
else if (m == WM_SIZE) {
imax = LOWORD(lParam);
jmax = HIWORD(lParam);
}
else if (m == WM_DESTROY)
PostQuitMessage(0);
else
return DefWindowProc(hwnd, m, wParam, lParam);
return 0;
}