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,14 @@
#include <iostream>
using std::cout;
namespace myspace {
void print_n_times(char str[], int n = 10) {
for (int i = 0; i < n; ++i)
cout << str;
}
}
int main() {
char s[] = "nya\n";
myspace::print_n_times(s);
}

View file

@ -0,0 +1,11 @@
#include <iostream>
using std::cout, std::endl;
int cubeV(int x) {
return x * x * x;
}
int main() {
int x = 3;
cout << cubeV(x) << endl;
}

View file

@ -0,0 +1,12 @@
#include <iostream>
using std::cout, std::endl;
int cubeR(int& x) {
return x * x * x;
}
int main() {
int x = 3;
cout << cubeR(x) << endl;
}

View file

@ -0,0 +1,26 @@
#include <iostream>
using std::cout, std::endl;
void count_letters(char str[], int& n_letters, int& n_digits, int& n_other) {
while (*str) {
if (*str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z') {
n_letters += 1;
}
else if (*str >= '0' && *str <= '9') {
n_digits += 1;
}
else {
n_other += 1;
}
str += 1;
}
}
int main() {
int n_letters = 0, n_digits = 0, n_other = 0;
char s[] = "1n!2y#3a$";
count_letters(s, n_letters, n_digits, n_other);
cout << "letters: " << n_letters << endl << "digits: " << n_digits << endl << "n_other: " << n_other << endl;
}

View file

@ -0,0 +1,20 @@
#include <iostream>
using std::cout, std::endl;
struct Book {
char title[100];
int pages;
float price;
};
void addPrice(Book& b, float x) {
b.price += x;
}
int main() {
Book b = {"One Hundred Years of Solitude", 456, 1200};
float x = 15.6;
addPrice(b, x);
cout << b.price << endl;
}

View file

@ -0,0 +1,20 @@
#include <iostream>
using std::cout, std::endl;
struct Book {
char title[100];
int pages;
float price;
};
bool isExpensive(const Book& b) {
if (b.price > 1000) {
return true;
}
return false;
}
int main() {
Book b = {"One Hundred Years of Solitude", 456, 1200};
cout << isExpensive(b) << endl;
}

View file

@ -0,0 +1,38 @@
#include <iostream>
#include "vector3f.h"
using namespace std;
int main() {
Vector3f a = {1.0, 2.0, -2.0};
Vector3f b = {4.0, -1.0, 3.0};
cout << "a = " << a << endl << "b = " << b << endl;
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "0.5 * a = " << 0.5 * a << endl;
cout << "a * 0.5 = " << a * 0.5 << endl;
cout << "(a, b) = " << a * b << endl;
cout << "a / 5 = " << a / 5 << endl;
cout << "-a = " << -a << endl;
cout << "+a = " << +a << endl;
cout << "a == b = " << (a == b) << endl;
cout << "a != b = " << (a != b) << endl;
a += b;
cout << "a += b: " << a << endl;
a -= b;
cout << "a -= b: " << a << endl;
a *= 2;
cout << "a *= 2: " << a << endl;
a /= 2;
cout << "a /= 2: " << a << endl;
normalize(a);
cout << "normalize(a): " << a << " |a| = " << norm(a) << endl;
}

View file

@ -0,0 +1,96 @@
#pragma once
#include <cmath>
#include <iostream>
struct Vector3f {
float x;
float y;
float z;
};
Vector3f operator+(const Vector3f& a, const Vector3f& b) {
Vector3f result = {a.x + b.x, a.y + b.y, a.z + b.z};
return result;
}
Vector3f operator-(const Vector3f& a, const Vector3f& b) {
Vector3f result = {a.x - b.x, a.y - b.y, a.z - b.z};
return result;
}
Vector3f operator*(const Vector3f& a, float f) {
Vector3f result = {f * a.x, f * a.y, f * a.z};
return result;
}
Vector3f operator*(float f, const Vector3f& a) {
Vector3f result = {f * a.x, f * a.y, f * a.z};
return result;
}
int operator*(const Vector3f& a, const Vector3f& b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
Vector3f operator/(const Vector3f& a, float f) {
Vector3f result = {a.x / f, a.y / f, a.z / f};
return result;
}
Vector3f operator+(const Vector3f& a) {
return a;
}
Vector3f operator-(const Vector3f& a) {
Vector3f result = {-a.x, -a.y, -a.z};
return result;
}
bool operator==(const Vector3f& a, const Vector3f& b) {
if (a.x == b.x && a.y == b.y && a.z == b.z) {
return true;
}
return false;
}
bool operator!=(const Vector3f& a, const Vector3f& b) {
return not (a == b);
}
void operator+=(Vector3f& a, const Vector3f& b) {
a = a + b;
}
void operator-=(Vector3f& a, const Vector3f& b) {
a = a - b;
}
void operator*=(Vector3f& a, float f) {
a = f * a;
}
void operator/=(Vector3f& a, float f) {
a = a / f;
}
float squared_norm(const Vector3f& a) {
return a * a;
}
float norm(const Vector3f& a) {
return sqrt(squared_norm(a));
}
void normalize(Vector3f& a) {
a /= norm(a);
}
std::istream& operator>>(std::istream& in, Vector3f& a) {
in >> a.x >> a.y >> a.z;
return in;
}
std::ostream& operator<<(std::ostream& out, const Vector3f& a) {
out << "(" << a.x << ", " << a.y << ", " << a.z << ")";
return out;
}

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);
}

Binary file not shown.