Fix rotations and add second token value

Rotations only took cosine of the angle, but this is not enough to
determine all possible angles. create_rotation now takes sin of the
angle too. Modified tokens to have a second value to accomodate this
change
This commit is contained in:
Madeline Busig 2025-05-06 23:55:50 -07:00
parent 7975b84a68
commit 361938f5f1
3 changed files with 20 additions and 15 deletions

View File

@ -9,7 +9,7 @@
#include <mtl/exception.hpp> #include <mtl/exception.hpp>
// TODO: Implement in MTL and create version that doesn't need cos of the angle // TODO: Implement in MTL and create version that doesn't need cos of the angle
mtl::mat<2, 2> create_rotation(mtl::fixed angle_cos); mtl::mat<2, 2> create_rotation(mtl::fixed angle_cos, mtl::fixed angle_sin);
namespace fractal { namespace fractal {
@ -36,6 +36,7 @@ enum class token_type_e {
struct token_t { struct token_t {
token_type_e m_type = token_type_e::empty; token_type_e m_type = token_type_e::empty;
mtl::fixed m_value; mtl::fixed m_value;
mtl::fixed m_value2;
uint32_t m_mark; uint32_t m_mark;
}; };
@ -122,7 +123,7 @@ public:
* *
* @exception @c mtl::length_error if the maximum number of tokens is reached * @exception @c mtl::length_error if the maximum number of tokens is reached
*/ */
token_id_t add_token(token_type_e type, mtl::fixed value = 0, uint32_t mark = 0); token_id_t add_token(token_type_e type, mtl::fixed value = 0, mtl::fixed value2 = 0, uint32_t mark = 0);
/** /**
* @brief Checks if the maximum number of group characteristics has been reached * @brief Checks if the maximum number of group characteristics has been reached

View File

@ -11,9 +11,7 @@ using mtl::fixed;
using mtl::vec2; using mtl::vec2;
using mtl::mat; using mtl::mat;
mtl::mat<2, 2> create_rotation(mtl::fixed angle_cos) { mtl::mat<2, 2> create_rotation(mtl::fixed angle_cos, mtl::fixed angle_sin) {
fixed angle_sin = fixed(1) - angle_cos * angle_cos;
return mat<2, 2>({ return mat<2, 2>({
{ angle_cos, -angle_sin }, { angle_cos, -angle_sin },
{ angle_sin, angle_cos } { angle_sin, angle_cos }
@ -24,12 +22,12 @@ namespace fractal {
// ---------- RULESET ---------- // ---------- RULESET ----------
token_id_t ruleset_t::add_token(token_type_e type, mtl::fixed value, uint32_t mark) { token_id_t ruleset_t::add_token(token_type_e type, mtl::fixed value, mtl::fixed value2, uint32_t mark) {
if (m_tokens.full()) { if (m_tokens.full()) {
throw mtl::length_error(); throw mtl::length_error();
} }
token_t tok { .m_type = type, .m_value = value, .m_mark = mark}; token_t tok { .m_type = type, .m_value = value, .m_value2 = value2, .m_mark = mark};
m_tokens.push_back(tok); m_tokens.push_back(tok);
return m_tokens.size() - 1; return m_tokens.size() - 1;

View File

@ -14,9 +14,15 @@ int main(void) {
REG_DISPCNT = DCNT_MODE3 | DCNT_BG2; REG_DISPCNT = DCNT_MODE3 | DCNT_BG2;
log::debug << "Hello world!" << mtl::endl; log::debug << "Hello world!" << mtl::endl;
constexpr float cos_PI_2 = std::cos(M_PI_2);
constexpr float cos_PI_4 = std::cos(M_PI_4);
constexpr float sin_PI_2 = std::sin(M_PI_2);
constexpr float sin_PI_4 = std::sin(M_PI_4);
float cos_PI_2 = std::cos(M_PI_4);
fixed cos_PI_2_fx = cos_PI_2; fixed cos_PI_2_fx = cos_PI_2;
fixed cos_PI_4_fx = cos_PI_4;
fixed sin_PI_2_fx = sin_PI_2;
fixed sin_PI_4_fx = sin_PI_4;
ruleset_t rules; ruleset_t rules;
@ -24,16 +30,16 @@ int main(void) {
fixed branch_angle = M_PI_4; fixed branch_angle = M_PI_4;
token_id_t walk_petal_length = rules.add_token(token_type_e::walk, 8); token_id_t walk_petal_length = rules.add_token(token_type_e::walk, 8);
token_id_t walk_petal_side = rules.add_token(token_type_e::walk, 4); token_id_t walk_petal_side = rules.add_token(token_type_e::walk, 2);
token_id_t rotate_axiom = rules.add_token(token_type_e::rotate, cos_PI_2_fx); token_id_t rotate_axiom = rules.add_token(token_type_e::rotate, cos_PI_2_fx, sin_PI_2_fx);
token_id_t rotate_petal = rules.add_token(token_type_e::rotate, cos_PI_2_fx); token_id_t rotate_petal = rules.add_token(token_type_e::rotate, cos_PI_2_fx, -sin_PI_2_fx);
token_id_t rotate_branchr = rules.add_token(token_type_e::rotate, branch_angle); token_id_t rotate_branchr = rules.add_token(token_type_e::rotate, cos_PI_4_fx, -sin_PI_4_fx);
token_id_t rotate_branchl = rules.add_token(token_type_e::rotate, branch_angle * -2); token_id_t rotate_branchl = rules.add_token(token_type_e::rotate, cos_PI_2_fx, sin_PI_2_fx);
token_id_t branch = rules.add_token(token_type_e::empty); token_id_t branch = rules.add_token(token_type_e::empty);
token_id_t petal = rules.add_token(token_type_e::empty); token_id_t petal = rules.add_token(token_type_e::empty);
token_id_t mark = rules.add_token(token_type_e::empty, 0, 1); token_id_t mark = rules.add_token(token_type_e::empty, 0, 0, 1);
log::info << "Added tokens" << endl; log::info << "Added tokens" << endl;
@ -45,7 +51,7 @@ int main(void) {
etl::vector branch_chr{ petal, rotate_branchr, branch, rotate_branchl, branch }; etl::vector branch_chr{ petal, rotate_branchr, branch, rotate_branchl, branch };
group_id_t branch_grp = rules.add_group_characteristic(branch_chr); group_id_t branch_grp = rules.add_group_characteristic(branch_chr);
etl::vector petal_chr{ mark, walk_petal_length, mark, rotate_petal, walk_petal_length, mark }; etl::vector petal_chr{ mark, walk_petal_length, mark, rotate_petal, walk_petal_side, mark };
group_id_t petal_grp = rules.add_group_characteristic(petal_chr); group_id_t petal_grp = rules.add_group_characteristic(petal_chr);
log::info << "Added group characteristics" << endl; log::info << "Added group characteristics" << endl;