added double jump and sitting state

This commit is contained in:
nihonium 2023-02-25 19:34:24 +03:00
parent 2e5c5a8dde
commit 90d07dde3f
Signed by: nihonium
GPG key ID: 0251623741027CFC
148 changed files with 13050 additions and 0 deletions

View file

@ -0,0 +1,189 @@
#pragma once
#include <cmath>
#include <iostream>
struct Complex {
float re;
float im;
};
// Передаёмм аргументы через ссылки
// В данном случае можно было передавать по значению
// (так как Complex имеет малый размер)
// Но в общем случае лучше для структур лучше
// всегда использовать ссылки
Complex operator+(const Complex& a, const Complex& b) {
Complex result = {a.re + b.re, a.im + b.im};
return result;
}
Complex operator-(const Complex& a, const Complex& b) {
Complex result = {a.re - b.re, a.im - b.im};
return result;
}
Complex operator*(const Complex& a, const Complex& b) {
Complex result = {a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re};
return result;
}
Complex operator/(const Complex& a, const Complex& b) {
float b_squared = b.re * b.re + b.im * b.im;
Complex result;
result.re = (a.re * b.re + a.im * b.im) / b_squared;
result.im = (a.im * b.re - a.re * b.im) / b_squared;
return result;
}
Complex& operator+=(Complex &a, const Complex &b) {
a.re += b.re;
a.im += b.im;
return a;
}
// Унарный оператор -
// То есть если z - комплексное число x + iy, то -z = - x - iy
Complex operator-(const Complex& a) {
Complex result;
result.re = -a.re;
result.im = -a.im;
return result;
}
// Унарный оператор +
// Ничего не меняет
Complex operator+(const Complex& a) {
Complex result = a;
return result;
}
// Унарный оператор *
// То есть если z - комплексное число x + iy, то *z = x - iy
// Оператор сопряжения
Complex operator*(const Complex& a) {
Complex result;
result.re = a.re;
result.im = -a.im;
return result;
}
// Число + комплексное число (в таком порядке)
Complex operator+(float a, const Complex& b) {
Complex result = b;
result.re += a;
return result;
}
// Комплексное число + число
Complex operator+(const Complex& a, float b) {
Complex result = a;
result.re += b;
return result;
}
// Число - комплексное число (в таком порядке)
Complex operator-(float a, const Complex& b) {
Complex result = -b;
result.re += a;
return result;
}
// Комплексное число - число
Complex operator-(const Complex& a, float b) {
Complex result = a;
result.re -= b;
return result;
}
// Комплексное число * число
Complex operator*(const Complex& a, float b) {
Complex result = a;
result.re *= b;
result.im *= b;
return result;
}
// Число * комплексное число
Complex operator*(float a, const Complex& b) {
Complex result = b;
result.re *= a;
result.im *= a;
return result;
}
// Комплексное число / число
Complex operator/(const Complex& a, float b) {
Complex result = a;
result.re /= b;
result.im /= b;
return result;
}
// Число / комплексное число
Complex operator/(float a, const Complex& b) {
float b_squared = b.re * b.re + b.im * b.im;
return (a * (*b)) / b_squared;
}
// Перегружаем оператор<< между типами
// std::ostream (такой тип имеет std::cout) и Complex
// Обратите внимание, что мы возвращаем ссылку на ostream
// Таким образом результатом выражения cout << a будет cout
// Поэтому можно делать так: cout << a << b << c ...
std::ostream& operator<<(std::ostream& out, const Complex& a) {
if (a.re != 0)
out << a.re;
if (a.im > 0) {
if (a.im != 1.0)
out << " + " << a.im << "i";
else
out << " + i";
}
else if (a.im < 0) {
if (a.im != -1.0)
out << " - " << -a.im << "i";
else
out << " - i";
}
return out;
}
std::istream& operator>>(std::istream& in, Complex& c) {
in >> c.re >> c.im;
return in;
}
float abs(const Complex& a) {
return sqrtf(a.re * a.re + a.im * a.im);
}
Complex exp(const Complex& a) {
Complex result;
result.re = expf(a.re) * cosf(a.im);
result.im = expf(a.re) * sinf(a.im);
return result;
}
Complex sin(const Complex& a) {
Complex result;
result.re = sinf(a.re) * coshf(a.im);
result.im = cosf(a.re) * sinhf(a.im);
return result;
}
Complex cos(const Complex& a) {
Complex result;
result.re = cosf(a.re) * coshf(a.im);
result.im = sinf(a.re) * sinhf(a.im);
return result;
}

View file

@ -0,0 +1,56 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "complex.h"
using namespace std;
// В этой программе мы рисуем в картинку комплексную функцию,
// которая задаётся в функции func
struct Color {
unsigned char r, g, b;
};
Complex func(Complex z) {
Complex f = 100/(z - 1)*exp(z);
f.re = fabs(f.re);
f.im = fabs(f.im);
if (f.re > 255)
f.re = 255;
if (f.im > 255)
f.im = 255;
return f;
}
int main() {
int width = 800, height = 800;
float x0 = -2.0f, x1 = 2.0f;
float y0 = -2.0f, y1 = 2.0f;
// Выделяем память под пиксели
Color* data = (Color*)malloc(sizeof(Color) * width * height);
// data - это массив цветов размером width * height
// Задаём значения этого массива так, чтобы
// реальная часть функции func соответствовала зелёному цвету,
// а мнимая часть -- синей компоненте цвета
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
Complex z = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j};
Complex f = func(z);
data[i + width * j].r = 0;
data[i + width * j].g = f.re;
data[i + width * j].b = f.im;
}
}
// Сохраняем массив цветов data как картинку в формате .ppm
FILE* file = fopen("complex_image.ppm", "wb");
fprintf(file, "P6\n%d %d\n255\n", width, height);
fwrite(data, sizeof(Color), height * width, file);
fclose(file);
// Освобождаем память
free(data);
}

View file

@ -0,0 +1,63 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "complex.h"
using namespace std;
// Это программа создаёт анимацию (набор картинок)
// которая задаётся как меняющееся во времени
// комплексная функция (описана в функции func)
struct Color {
unsigned char r, g, b;
};
Complex func(Complex z, int time) {
Complex f = 100/(z - (0.02f*time))*exp(z*sin(z));
f.re = fabs(f.re);
f.im = fabs(f.im);
if (f.re > 255)
f.re = 255;
if (f.im > 255)
f.im = 255;
return f;
}
int main() {
int width = 800, height = 800;
float x0 = -2.0f, x1 = 2.0f;
float y0 = -2.0f, y1 = 2.0f;
Color* data = (Color*)malloc(sizeof(Color) * width * height);
// Повторяем 200 раз
int max_time_steps = 200;
for (int time = 0; time < max_time_steps; time++)
{
// Задаём изображение в массиве data
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
Complex z = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j};
Complex f = func(z, time);
data[i + width * j].r = 0;
data[i + width * j].g = f.re;
data[i + width * j].b = f.im;
}
}
// Создаём в строке filename имя изображения
// Папка animation должна существовать!
char filename[100];
sprintf(filename, "animation/complex_%03d.ppm", time);
// Сохраняем изображение в картинке по имени filename
FILE* file = fopen(filename, "wb");
fprintf(file, "P6\n%d %d\n255\n", width, height);
fwrite(data, sizeof(Color), height * width, file);
fclose(file);
}
free(data);
}

Binary file not shown.

View file

@ -0,0 +1,38 @@
#include <iostream>
#include "complex.h"
using namespace std;
// Тут мы тестируем нашу реализацию комплексных чисел
int main() {
Complex a;
Complex b;
cin >> a >> b;
cout << "a = " << a << endl
<< "b = " << b << endl
<< "a + b = " << a + b << endl
<< "a - b = " << a - b << endl
<< "a * b = " << a * b << endl
<< "a / b = " << a / b << endl
<< "-a = " << -a << endl
<< "+a = " << +a << endl
<< "*a = " << *a << endl
<< "a + 5 = " << a + 5 << endl
<< "5 + a = " << 5 + a << endl
<< "a * 5 = " << a * 5 << endl
<< "5 * a = " << 5 * a << endl
<< "Exp(a) = " << exp(a) << endl
<< "Sin(a) = " << sin(a) << endl
<< "Cos(a) = " << cos(a) << endl
<< "Exp((a + b) / a) * Cos(a - b) = " << exp((a + b) / a) * cos(a - b) << endl;
a += b;
cout << "a += b; a = " << a << endl;
// Оператор = мы не перегружали, но это всё равно работает
b = a;
cout << "b = a; b = " << b << endl;
}

View file

@ -0,0 +1,64 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "complex.h"
using namespace std;
struct Color {
unsigned char r, g, b;
};
Complex func(Complex z, Complex c) {
return z * z + c;
}
int main(int argc, char** argv) {
Complex c = {0, 0};
int n = 20;
int width = 800, height = 800;
float x0 = -2.0f, x1 = 2.0f;
float y0 = -2.0f, y1 = 2.0f;
if (argc < 4) {
cout << "usage: julia [re] [im] [n]" << endl
<< "using default values: 0 + 0i, n = 20 "<< endl;
}
else {
cout << argv[1] << " + " << argv[2] << "i" << endl << "n = " << n << endl;
sscanf(argv[1], "%f", &c.re);
sscanf(argv[2], "%f", &c.im);
sscanf(argv[3], "%d", &n);
}
Color* data = (Color*)malloc(sizeof(Color) * width * height);
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
Complex z = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j};
for (int k = 0; k < n; ++k) {
z = func(z, c);
}
float r = abs(z.re);
float b = abs(z.im);
if (r > 255) {
r = 255;
}
if (b > 255) {
b = 255;
}
data[i + width * j].r = 255 - r;
data[i + width * j].g = 0;
data[i + width * j].b = 255 - b;
}
}
FILE* file = fopen("julia.ppm", "wb");
fprintf(file, "P6\n%d %d\n255\n", width, height);
fwrite(data, sizeof(Color), height * width, file);
fclose(file);
free(data);
}

View file

@ -0,0 +1,60 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "complex.h"
using namespace std;
struct Color {
unsigned char r, g, b;
};
Complex func(Complex z, Complex c) {
return z * z + c;
}
int main() {
int width = 800, height = 800;
float x0 = -2.0f, x1 = 2.0f;
float y0 = -2.0f, y1 = 2.0f;
int n = 20;
Color* data = (Color*)malloc(sizeof(Color) * width * height);
int max_time_steps = 500;
for (int time = 0; time < max_time_steps; time++)
{
Complex c = {-1.5 + (1.5 / max_time_steps) * time, -1.5 + (1.5 / max_time_steps) * time + 1};
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
Complex z = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j};
for (int k = 0; k < n; ++k) {
z = func(z, c);
}
float r = abs(z.re);
float b = abs(z.im);
if (r > 255) {
r = 255;
}
if (b > 255) {
b = 255;
}
data[i + width * j].r = 255 - r;
data[i + width * j].g = 0;
data[i + width * j].b = 255 - b;
}
}
char filename[100];
sprintf(filename, "animation/complex_%03d.ppm", time);
FILE* file = fopen(filename, "wb");
fprintf(file, "P6\n%d %d\n255\n", width, height);
fwrite(data, sizeof(Color), height * width, file);
fclose(file);
}
free(data);
}

View file

@ -0,0 +1,54 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "complex.h"
using namespace std;
struct Color {
unsigned char r, g, b;
};
Complex func(Complex z, Complex c) {
return z * z + c;
}
int main(int argc, char** argv) {
int n = 20;
Complex z = {0, 0};
int width = 800, height = 800;
float x0 = -2.0f, x1 = 2.0f;
float y0 = -2.0f, y1 = 2.0f;
Color* data = (Color*)malloc(sizeof(Color) * width * height);
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
z = {0, 0};
Complex c = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j};
for (int k = 0; k < n; ++k) {
z = func(z, c);
}
float r = abs(z.re);
float b = abs(z.im);
if (r > 255) {
r = 255;
}
if (b > 255) {
b = 255;
}
data[i + width * j].r = 255 - r;
data[i + width * j].g = 0;
data[i + width * j].b = 255 - b;
}
}
FILE* file = fopen("mandelbrot.ppm", "wb");
fprintf(file, "P6\n%d %d\n255\n", width, height);
fwrite(data, sizeof(Color), height * width, file);
fclose(file);
free(data);
}