0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-01 20:09:04 -05:00
This commit is contained in:
Aitor 2023-03-10 12:27:56 +01:00
parent eb1bc480ca
commit 6f8e8da466
7 changed files with 526 additions and 0 deletions

View file

@ -0,0 +1,20 @@
# Notas
- [ ] Mover todo esto a algún otro sitio mejor que no sea `resources/public`.
- [ ] Implementar tanto `clang-format` como `clang-tidy` para formatear el código.
- [ ] Implementar algún sistema de testing (Catch2, Google Test, CppUnit, etc).
- [ ] Implementar CMake para construir el proyecto.
Para compilar el código en C++ se puede usar la siguiente línea:
```sh
g++ -std=c++20 src/main.cpp -o main
```
## Documentos
- [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-introduction)
## Recursos
- [Compiling C to WebAssembly without Emscripten](https://surma.dev/things/c-to-webassembly/)

View file

@ -0,0 +1,54 @@
#pragma once
#include "Vector2.h"
template <typename T>
struct Box2
{
Vector2<T> position;
Vector2<T> size;
Box2(T x, T y, T width, T height) {
position.set(x, y);
size.set(width, height);
}
Box2(const Box2<T>& box) : position(box.position), size(box.size) {}
Box2(const Vector2<T>& position, const Vector2<T>& size) : position(position), size(size) {}
auto left() const
{
return position.x;
}
auto right() const
{
return position.x + size.x;
}
auto top() const
{
return position.y;
}
auto bottom() const
{
return position.y + size.y;
}
bool contains(const Vector2<T>& point) const {
return point.x > position.x
&& point.x < position.x + size.x
&& point.y > position.y
&& point.y < position.y + size.y;
}
bool intersects(const Box2<T> &other) const {
if (left() > other.right() || right() < other.left())
return false;
if (top() > other.bottom() || bottom() < other.top())
return false;
return true;
}
};

View file

@ -0,0 +1,96 @@
#pragma once
template <typename T>
struct Color {
T r, g, b, a;
Color<T>& set(T new_r, T new_g, T new_b, T new_a)
{
r = new_r;
g = new_g;
b = new_b;
a = new_a;
return *this;
}
Color<T>& set(T new_r, T new_g, T new_b)
{
r = new_r;
g = new_g;
b = new_b;
return *this;
}
Color<T> operator+(const Color<T> other)
{
return {
r + other.r,
g + other.g,
b + other.b
};
}
Color<T> operator-(const Color<T> other)
{
return {
r - other.r,
g - other.g,
b - other.b
};
}
Color<T> operator*(const Color<T> other)
{
return {
r * other.r,
g * other.g,
b * other.b
};
}
Color<T> operator/(const Color<T> other)
{
return {
r / other.r,
g / other.g,
b / other.b
};
}
Color<T> operator+(const T scalar)
{
return {
r + scalar,
g + scalar,
b + scalar
};
}
Color<T> operator-(const T scalar)
{
return {
r - scalar,
g - scalar,
b - scalar
};
}
Color<T> operator*(const T scalar)
{
return {
r * scalar,
g * scalar,
b * scalar
};
}
Color<T> operator/(const T scalar)
{
return {
r / scalar,
g / scalar,
b / scalar
};
}
}

View file

@ -0,0 +1,20 @@
#pragma once
namespace Interpolation {
template <typename T>
T linear(const T x, const T a, const T b) {
return (1 - x) * a + x * b;
}
template <typename T>
T quadratic(const T x, const T a, const T b, const T c) {
return linear(x, linear(x, a, b), linear(x, b, c));
}
template <typename T>
T cubic(const T x, const T a, const T b, const T c, const T d) {
return linear(x, quadratic(x, a, b, c), quadratic(x, b, c, d));
}
}

View file

@ -0,0 +1,105 @@
#pragma once
#include <cmath>
#include <iostream>
template <typename T>
struct Matrix23 {
// a c tx
// b d ty
T a, b, c, d, tx, ty;
Matrix23() : a(1), b(0), c(0), d(1), tx(0), ty(0) {}
Matrix23(T a, T b, T c, T d, T tx, T ty) : a(a), b(b), c(c), d(d), tx(tx), ty(ty) {}
Matrix23(const Matrix23<T>& other) : a(other.a), b(other.b), c(other.c), d(other.d), tx(other.tx), ty(other.ty) {}
auto determinant() const {
return a * d - b * c;
}
Matrix23<T>& identity()
{
a = 1;
b = 0;
c = 0;
d = 1;
tx = 0;
ty = 0;
return *this;
}
Matrix23<T>& translate(T x, T y)
{
tx += x;
ty += y;
return *this;
}
Matrix23<T>& scale(T x, T y)
{
a *= x;
b *= y;
c *= x;
d *= y;
return *this;
}
Matrix23<T>& rotate(auto angle)
{
auto cos = std::cos(angle);
auto sin = std::sin(angle);
auto new_a = a * cos + c * sin;
auto new_b = b * cos + d * sin;
auto new_c = c * cos - a * sin;
auto new_d = d * cos - b * sin;
a = new_a;
b = new_b;
c = new_c;
d = new_d;
return *this;
}
Matrix23<T> invert()
{
auto det = determinant();
if (det == 0)
{
return *this;
}
auto inv_det = 1.0 / det;
return {
d * inv_det,
-b * inv_det,
-c * inv_det,
a * inv_det,
(c * ty - d * tx) * inv_det,
(b * tx - a * ty) * inv_det
};
}
Matrix23<T> operator*(const Matrix23<T>& other)
{
// M N
// a c x a c x
// b d y x b d y = T
// 0 0 1 0 0 1
return {
a * other.a + b * other.c,
a * other.b + b * other.d,
c * other.a + d * other.c,
c * other.b + d * other.d,
tx * other.a + ty * other.c + other.tx,
tx * other.b + ty * other.d + other.ty
};
}
};
template <typename T>
std::ostream &operator<<(std::ostream &os, const Matrix23<T> &matrix)
{
os << "Matrix23(" << matrix.a << ", " << matrix.b << ", " << matrix.c << ", " << matrix.d << ", " << matrix.tx << ", " << matrix.ty << ")";
return os;
}

View file

@ -0,0 +1,197 @@
#pragma once
#include <cmath>
#include <iostream>
#include "Interpolation.h"
template <typename T>
struct Vector2 {
T x, y;
Vector2() : x(0), y(0) {}
Vector2(T x, T y) : x(x), y(y) {}
Vector2(const Vector2<T>& other) : x(other.x), y(other.y) {}
auto dot(const Vector2<T> &other) const
{
return x * other.x + y * other.y;
}
auto cross(const Vector2<T> &other) const
{
return x * other.y - y * other.x;
}
auto direction() const
{
return std::atan2(y, x);
}
auto length() const
{
return std::hypot(x, y);
}
auto lengthSquared() const
{
return x * x + y * y;
}
Vector2<T>& normalize()
{
auto l = length();
return set(x / l, x / l);
}
Vector2<T>& perpLeft()
{
return set(y, -x);
}
Vector2<T>& perpRight()
{
return set(-y, x);
}
Vector2<T>& rotate(auto rotation)
{
auto c = std::cos(rotation);
auto s = std::sin(rotation);
return set(
c * x - s * y,
s * x + c * y
);
}
Vector2<T>& scale(auto s)
{
return set(
x * s,
y * s
);
}
Vector2<T>& set(T newX, T newY)
{
x = newX;
y = newY;
return *this;
}
Vector2<T>& copy(const Vector2<T>& other)
{
return set(other.x, other.y);
}
Vector2<T>& linear(auto p, Vector2<T>& a, Vector2<T>& b)
{
return set(
Interpolation::linear(p, a.x, b.x),
Interpolation::linear(p, a.y, b.y)
);
}
Vector2<T>& quadratic(auto p, Vector2<T>& a, Vector2<T>& b, Vector2<T>& c)
{
return set(
Interpolation::quadratic(p, a.x, b.x, c.x),
Interpolation::quadratic(p, a.y, b.y, c.y)
);
}
Vector2<T>& cubic(auto p, Vector2<T>& a, Vector2<T>& b, Vector2<T>& c, Vector2<T>& d)
{
return set(
Interpolation::cubic(p, a.x, b.x, c.x, d.x),
Interpolation::cubic(p, a.y, b.y, c.y, d.y)
);
}
Vector2<T> operator*(const Matrix23<T> m)
{
return {
x * m.a + y * m.c + m.tx,
x * m.b + y * m.d + m.ty
};
}
Vector2<T> operator+(const Vector2<T> other)
{
return {
x + other.x,
y + other.y
};
}
Vector2<T> operator-(const Vector2<T> other)
{
return {
x - other.x,
y - other.y
};
}
Vector2<T> operator*(const Vector2<T> other)
{
return {
x * other.x,
y * other.y
};
}
Vector2<T> operator/(const Vector2<T> other)
{
return {
x / other.x,
y / other.y
};
}
Vector2<T> operator+(T scalar)
{
return {
x + scalar,
y + scalar
};
}
Vector2<T> operator-(T scalar)
{
return {
x - scalar,
y - scalar
};
}
Vector2<T> operator*(T scalar)
{
return {
x * scalar,
y * scalar
};
}
Vector2<T> operator/(T scalar)
{
return {
x / scalar,
y / scalar
};
}
Vector2<T> operator-() const
{
return {
-x,
-y
};
}
};
template <typename T>
std::ostream& operator<<(std::ostream &os, const Vector2<T>& vector)
{
os << "Vector2(" << vector.x << ", " << vector.y << ")";
return os;
}

View file

@ -0,0 +1,34 @@
#include <cmath>
#include <iostream>
#include "Interpolation.h"
#include "Matrix23.h"
#include "Vector2.h"
#include "Box2.h"
void resize()
{
}
int main(int argc, char** argv)
{
Vector2<float> a(1, 0);
Vector2<float> b(0, 1);
Matrix23<float> m;
std::cout << m << std::endl;
std::cout << m.rotate(M_PI / 2) << std::endl;
Vector2<float> a2 = a * m;
Vector2<float> b2 = b * m;
std::cout << a << ", " << a2 << std::endl;
std::cout << b << ", " << b2 << std::endl;
std::cout << "Hello, World!" << std::endl;
return 0;
}