Simple pendulum

This commit is contained in:
Ben
2019-02-01 16:29:47 +00:00
parent 9d3472110c
commit ea91e8d476
9 changed files with 2639 additions and 5 deletions

View File

@@ -33,10 +33,8 @@ public:
Zre = tX;
iteration++;
}
long double ratio1 = (long double)iteration - std::log2(std::log2(Zre*Zre + Zim*Zim)) / m_maxIterations;
long double ratio2 = (long double)iteration - std::log(std::log(Zre*Zre * Zim*Zim)) / m_maxIterations;
long double ratio3 = (long double)iteration - std::log10(std::log10(Zre*Zre - Zim*Zim)) / m_maxIterations;
olc::Pixel colour = olc::Pixel(ratio1 + 255.0f, ratio2 + 255.0f, ratio3 + 255.0f);
long double ratio = (long double)iteration - std::log2(std::log2(Zre*Zre + Zim*Zim)) / m_maxIterations;
olc::Pixel colour = olc::Pixel(ratio + 255.0f, ratio + 255.0f, ratio + 255.0f);
DrawRect(pX, pY, 1, 1, colour);
}
}
@@ -61,7 +59,7 @@ private:
long double m_x = 1.0f;
long double m_y = 1.0f;
long double m_scale = 0.7f;
int m_maxIterations = 15;
int m_maxIterations = 100;
std::map<int, olc::Pixel> m_palete;
};

BIN
C++/Mandelbrot explorer/output.o Executable file

Binary file not shown.

View File

@@ -0,0 +1,6 @@
{
"files.associations": {
"ostream": "cpp",
"cmath": "cpp"
}
}

View File

@@ -0,0 +1,101 @@
#include <iostream>
#include <sstream>
#include <math.h>
#include "rect.h"
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#define PI 3.14159265359
class SimplePendulum : public olc::PixelGameEngine {
public:
SimplePendulum() {
sAppName = "Simple Pendulum";
}
bool OnUserCreate() override {
return true;
}
bool OnUserUpdate(float fElapsedTime) override {
/* START CALCULATONS */
if (!m_isStopped) {
m_aAccel = ((-1 * m_gravity / m_pLength) * sin(m_theta) * m_pMass) * fElapsedTime;
m_aVelocity += m_aAccel *= m_pDamping;
m_theta += m_aVelocity;
}
m_x = m_pLength * sin(m_theta) + m_pivotX;
m_y = m_pLength * cos(m_theta) + m_pivotY;
/* END CALCULATIONS */
draw();
std::stringstream angularVel;
angularVel << "Angular velocity: " << m_aVelocity;
DrawString(0, 0, angularVel.str(), olc::BLACK);
std::stringstream angularAccel;
angularAccel << "Angular acceleration: " << m_aAccel;
DrawString(0, 10, angularAccel.str(), olc::BLACK);
std::stringstream coords;
coords << "Pendulum bob pos X:" << m_x << " Y: " << m_y;
DrawString(0, 20, coords.str(), olc::BLACK);
Rect p;
p.SetRect(m_x - 40, m_y - 40, 80, 80);
if (p.Contains(Vec2<int>(GetMouseX(), GetMouseY()))) {
DrawRect(p.x, p.y, p.w, p.h, olc::DARK_MAGENTA);
if (GetMouse(0).bHeld) {
m_isStopped = true;
m_theta = atan2(GetMouseX() - m_pivotX, GetMouseY() - m_pivotY);
m_aAccel = 0.0f;
m_aVelocity = 0.0f;
} else {
m_isStopped = false;
}
} else {
DrawRect(p.x, p.y, p.w, p.h, olc::RED);
m_isStopped = false;
}
return true;
}
void draw() {
Clear(olc::WHITE);
FillCircle(m_pivotX, m_pivotY, 8, olc::RED);
DrawLine(m_pivotX, m_pivotY, m_x, m_y, olc::BLACK);
if (!m_isStopped) {
FillCircle(m_x, m_y, 20, olc::BLACK);
} else {
FillCircle(m_x, m_y, 20, olc::DARK_GREY);
}
}
private:
bool m_isStopped = false;
float m_theta = PI / 4;
float m_pivotX = 500;
float m_pivotY = 20;
float m_x = 500;
float m_y = 220;
float m_pLength = 300;
float m_aVelocity = 0.0f;
float m_aAccel = 0.0f;
float m_gravity = 9.81;
float m_pMass = 5;
float m_pDamping = 0.9;
};
int main(int argc, char** argv) {
SimplePendulum app;
if (app.Construct(1000, 600, 1, 1))
app.Start();
return 0;
}

223
C++/Simple pendulum/maths.h Normal file
View File

@@ -0,0 +1,223 @@
#ifndef MATH_H_
#define MATH_H_
const float DEG2RAD = 0.01745329251994329576923690768f;
const float RAD2DEG = 57.2957795130823208767981548141f;
inline float ToRadian(const float Degree) {
return (Degree * DEG2RAD);
}
inline float ToDegree(const float Radian) {
return (Radian * RAD2DEG);
}
template<class T>
struct Vec4 {
T x, y, z, w;
template<class P>
Vec4(P x, P y, P z, P w) : x(x), y(y), z(z), w(w) {}
template<class P>
Vec4(P all) : x(all), y(all), z(all), w(all) {}
Vec4() : x(0), y(0), z(0), w(0) {}
inline Vec4& dot(const Vec4<T>& v) {
return (x * v.x + y * v.y + z * v.z + w * v.w);
}
inline const Vec4& operator+() {
return *this;
}
inline Vec4& operator-() {
return Vec4<T>(-x, -y, -z, -w);
}
inline Vec4& operator+(const Vec4<T>& v) {
return new Vec4(x + v.x, y + v.y, z + v.z, w + v.w);
}
inline Vec4& operator-(const Vec4<T>& v) {
return new Vec4(x - v.x, y - v.y, z - v.z, w - v.w);
}
inline Vec4& operator*(const Vec4<T>& v) {
return new Vec4(x * v.x, y * v.y, z * v.z, w * v.w);
}
inline Vec4& operator/(const Vec4<T>& v) {
return new Vec4(x / v.x, y / v.y, z / v.z, w / v.w);
}
inline Vec4& operator+=(const Vec4<T>& v) {
x+=v.x; y+=v.y; z+=v.z; w+=v.w;
return *this;
}
inline Vec4& operator-=(const Vec4<T>& v) {
x-=v.x; y-=v.y; z-=v.z; w-=v.w;
return *this;
}
inline Vec4& operator*=(const Vec4<T>& v) {
x*=v.x; y*=v.y; z*=v.z; w*=v.w;
return *this;
}
inline Vec4& operator/=(const Vec4<T>& v) {
x/=v.x; y/=v.y; z/=v.z; w/=v.w;
return *this;
}
template<class P>
inline Vec4& operator+=(P s) {
x+=s; y+=s; z+=s; w+=s;
return *this;
}
template<class P>
inline Vec4& operator-=(P s) {
x-=s; y-=s; z-=s; w-=s;
return *this;
}
template<class P>
inline Vec4& operator*=(P s) {
x*=s; y*=s; z*=s; w*=s;
return *this;
}
template<class P>
inline Vec4& operator/=(P s) {
x/=s; y/=s; z/=s; w/=s;
return *this;
}
};
template<class T>
struct Vec3 {
T x, y, z;
template<class P>
Vec3(P x, P y, P z) : x(x), y(y), z(z) {}
template<class P>
Vec3(P all) : x(all), y(all), z(all) {}
Vec3() : x(0), y(0), z(0) {}
inline Vec3& cross(const Vec3<T>& v) {
return new Vec3(
(y * v.z - z * v.y),
(x * v.z - z * v.x),
(x * v.y - y * v.x)
);
}
inline Vec3& dot(const Vec3<T>& v) {
return (x * v.x + y * v.y + z * v.z);
}
inline const Vec3& operator+() {
return *this;
}
inline Vec3& operator-() {
return Vec3<T>(-x, -y, -z);
}
inline Vec3& operator+(const Vec3<T>& v) {
return new Vec3(x + v.x, y + v.y, z + v.z);
}
inline Vec3& operator-(const Vec3<T>& v) {
return new Vec3(x - v.x, y - v.y, z - v.z);
}
inline Vec3& operator*(const Vec3<T>& v) {
return new Vec3(x * v.x, y * v.y, z * v.z);
}
inline Vec3& operator/(const Vec3<T>& v) {
return new Vec3(x / v.x, y / v.y, z / v.z);
}
inline Vec3& operator+=(const Vec3<T>& v) {
x+=v.x; y+=v.y; z+=v.z;
return *this;
}
inline Vec3& operator-=(const Vec3<T>& v) {
x-=v.x; y-=v.y; z-=v.z;
return *this;
}
inline Vec3& operator*=(const Vec3<T>& v) {
x*=v.x; y*=v.y; z*=v.z;
return *this;
}
inline Vec3& operator/=(const Vec3<T>& v) {
x/=v.x; y/=v.y; z/=v.z;
return *this;
}
template<class P>
inline Vec3& operator+=(P s) {
x+=s; y+=s; z+=s;
return *this;
}
template<class P>
inline Vec3& operator-=(P s) {
x-=s; y-=s; z-=s;
return *this;
}
template<class P>
inline Vec3& operator*=(P s) {
x*=s; y*=s; z*=s;
return *this;
}
template<class P>
inline Vec3& operator/=(P s) {
x/=s; y/=s; z/=s;
return *this;
}
};
template<class T>
struct Vec2 {
T x, y;
template<class P>
Vec2(P x, P y) : x(x), y(y) {}
template<class P>
Vec2(P all) : x(all), y(all) {}
Vec2() : x(0), y(0) {}
inline const Vec2& operator+() {
return *this;
}
inline Vec2& dot(const Vec3<T>& v) {
return (x * v.x + y * v.y);
}
inline Vec2& operator-() {
return Vec3<T>(-x, -y);
}
inline Vec2& operator+(const Vec2<T>& v) {
return new Vec2(x + v.x, y + v.y);
}
inline Vec2& operator-(const Vec2<T>& v) {
return new Vec2(x - v.x, y - v.y);
}
inline Vec2& operator*(const Vec2<T>& v) {
return new Vec2(x * v.x, y * v.y);
}
inline Vec2& operator/(const Vec2<T>& v) {
return new Vec2(x / v.x, y / v.y);
}
inline Vec2& operator+=(const Vec2<T>& v) {
x+=v.x; y+=v.y;
return *this;
}
inline Vec2& operator-=(const Vec2<T>& v) {
x-=v.x; y-=v.y;
return *this;
}
inline Vec2& operator*=(const Vec2<T>& v) {
x*=v.x; y*=v.y;
return *this;
}
inline Vec2& operator/=(const Vec2<T>& v) {
x/=v.x; y/=v.y;
return *this;
}
template<class P>
inline Vec2& operator+=(P s) {
x+=s; y+=s;
return *this;
}
template<class P>
inline Vec2& operator-=(P s) {
x-=s; y-=s;
return *this;
}
template<class P>
inline Vec2& operator*=(P s) {
x*=s; y*=s;
return *this;
}
template<class P>
inline Vec2& operator/=(P s) {
x/=s; y/=s;
return *this;
}
};
#endif

File diff suppressed because it is too large Load Diff

BIN
C++/Simple pendulum/output.o Executable file

Binary file not shown.

View File

@@ -0,0 +1,164 @@
#include "rect.h"
Rect::Rect() {
Clear();
}
Rect::Rect(int x, int y, int w, int h) {
SetRect(x, y, w, h);
}
void Rect::Clear() {
SetRect(0, 0, 0, 0);
}
std::string Rect::ToString() {
std::string res = "(";
res += std::to_string(x);
res += ", ";
res += std::to_string(y);
res += ", ";
res += std::to_string(w);
res += ", ";
res += std::to_string(h);
res += ")";
return res;
}
bool Rect::Intersects(Rect* rect) {
int leftA = x;
int rightA = x + w;
int topA = y;
int bottomA = y + h;
int leftB = rect->x;
int rightB = rect->x + rect->w;
int topB = rect->y;
int bottomB = rect->y + rect->h;
if (bottomA <= topB) return false;
if (topA >= bottomB) return false;
if (rightA <= leftB) return false;
if (leftA >= rightB) return false;
return true;
}
// bool Rect::Intersects(int x, int y, int w, int h) {
// return Intersects(&CreateRect(x, y, w, h));
// }
bool Rect::Contains(Rect* rect) {
return (rect->x >= x && rect->Right() <= (x + w) && rect->y >= y && rect->Bottom() <= (y + h));
}
bool Rect::Contains(Vec2<int>* point) {
return (point->x >= x && point->x <= (x + w) && point->y >= y && point->y <= (y + h));
}
bool Rect::Contains(Vec2<int> point) {
return (point.x >= x && point.x <= (x + w) && point.y >= y && point.y <= (y + h));
}
bool Rect::Contains(int x, int y, int w, int h) {
Rect tempRect(x, y, w, h);
return Contains(&tempRect);
}
Vec2<int>* Rect::Position() {
Vec2<int>* res = new Vec2<int>(x, y);
return res;
}
Vec2<int>* Rect::Center() {
Vec2<int>* res = new Vec2<int>(x + (w / 2), y + (h / 2));
return res;
}
int Rect::CenterX() {
return (x + (w / 2));
}
int Rect::CenterY() {
return (y + (h / 2));
}
int Rect::Left() {
return x;
}
int Rect::Right() {
return (x + w);
}
int Rect::Top() {
return y;
}
int Rect::Bottom() {
return y + h;
}
int Rect::Perimiter() {
return (w + w + h + h);
}
int Rect::Area() {
return (w + h);
}
int Rect::GetX() {
return x;
}
int Rect::GetY() {
return y;
}
int Rect::GetW() {
return w;
}
int Rect::GetH() {
return h;
}
void Rect::SetRect(int x, int y, int w, int h) {
this->x = x;
this->y = y;
this->w = w;
this->h = h;
}
void Rect::SetSize(Vec2<int>* size) {
this->x = size->x;
this->y = size->y;
}
void Rect::SetPos(Vec2<int>* pos) {
this->w = pos->x;
this->h = pos->y;
}
void Rect::SetPos(int x, int y) {
this->w = x;
this->h = y;
}
void Rect::Translate(Vec2<int>* offset) {
this->x += offset->x;
this->y += offset->y;
}
void Rect::TranslateX(int x) {
this->x += x;
}
void Rect::TranslateY(int y) {
this->y += y;
}
Rect::~Rect() {
}

View File

@@ -0,0 +1,73 @@
#ifndef RECT_H_
#define RECT_H_
#include <string>
#include "maths.h"
class Rect {
public:
Rect();
Rect(int x, int y, int w, int h);
void Clear();
static Rect CreateRect(int x, int y, int w, int h) {
Rect tempRect(x, y, w, h);
return tempRect;
}
Rect operator+(Rect* rect) {
return Rect(this->x + rect->x, this->y + this->x, w, h);
}
Rect operator-(Rect* rect) {
return Rect(this->x - rect->x, this->y - this->x, w, h);
}
bool operator==(const Rect* rect) {
return (x == rect->x && y == rect->y && w == rect->w && h == rect->h);
}
bool operator!=(const Rect* rect) {
return !(x == rect->x && y == rect->y && w == rect->w && h == rect->h);
}
std::string ToString();
bool Intersects(Rect* rect);
// bool Intersects(int x, int y, int w, int h);
bool Contains(Rect* rect);
bool Contains(Vec2<int>* point);
bool Contains(Vec2<int> point);
bool Contains(int x, int y, int w, int h);
Vec2<int>* Position();
Vec2<int>* Center();
int CenterX();
int CenterY();
int Left();
int Right();
int Top();
int Bottom();
int Perimiter();
int Area();
int GetX();
int GetY();
int GetW();
int GetH();
void SetRect(int x, int y, int w, int h);
void SetSize(Vec2<int>* size);
void SetPos(Vec2<int>* pos);
void SetPos(int x, int y);
void Translate(Vec2<int>* offset);
void TranslateX(int x);
void TranslateY(int y);
int x, y, w, h;
virtual ~Rect();
private:
};
#endif