From ee03c2cac6344a679d345aae196dbb3f2a115f3e Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 5 Jul 2017 08:30:21 +0000 Subject: [PATCH] Add rectangle genes --- src/Genes.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++--- src/Genes.hpp | 29 ++++++++++++++++- src/Screens.cpp | 17 +++++++--- 3 files changed, 122 insertions(+), 10 deletions(-) diff --git a/src/Genes.cpp b/src/Genes.cpp index 0711a7b..9aee796 100644 --- a/src/Genes.cpp +++ b/src/Genes.cpp @@ -30,6 +30,9 @@ Gene* Gene::create(GeneType type) case Gene::Triangle: gene = new GeneTriangle(); break; + case Gene::Rectangle: + gene = new GeneRectangle(); + break; } gene->randomize(); @@ -44,6 +47,8 @@ Gene* Gene::copy(Gene* gene) return new GeneCircle(*static_cast(gene)); case Gene::Triangle: return new GeneTriangle(*static_cast(gene)); + case Gene::Rectangle: + return new GeneRectangle(*static_cast(gene)); } return nullptr; @@ -232,16 +237,89 @@ void GeneTriangle::mutate() switch (choicedist(*Gene::re)) { case 0: - Gene::mutatePosition(this->pos1, this->stddev_position); + Gene::mutatePosition(this->pos1, GeneTriangle::stddev_position); break; case 1: - Gene::mutatePosition(this->pos2, this->stddev_position); + Gene::mutatePosition(this->pos2, GeneTriangle::stddev_position); break; case 2: - Gene::mutatePosition(this->pos3, this->stddev_position); + Gene::mutatePosition(this->pos3, GeneTriangle::stddev_position); break; case 3: - Gene::mutateColor(this->color, this->stddev_color); + Gene::mutateColor(this->color, GeneTriangle::stddev_color); break; } } + + + +// GeneRectangle +float GeneRectangle::stddev_position = .1; +float GeneRectangle::stddev_color = 20; +sf::RectangleShape GeneRectangle::rectangle; + + +GeneRectangle::GeneRectangle() +{ + this->type = Gene::Rectangle; +} + + +GeneRectangle::~GeneRectangle() +{ +} + + +void GeneRectangle::draw(sf::RenderTarget& target, sf::RenderStates states) const +{ + GeneRectangle::rectangle.setPosition(this->topleft); + GeneRectangle::rectangle.setSize(this->botright - this->topleft); + + GeneRectangle::rectangle.setFillColor(this->color); + + target.draw(GeneRectangle::rectangle, states); +} + + +void GeneRectangle::randomize() +{ + Gene::randomizePosition(this->topleft); + Gene::randomizePosition(this->botright); + + Gene::randomizeColor(this->color); +} + + +void GeneRectangle::mutate() +{ + std::uniform_int_distribution<> choicedist(0, 2); + + switch (choicedist(*Gene::re)) { + case 0: + Gene::mutatePosition(this->topleft, GeneRectangle::stddev_position); + break; + case 1: + Gene::mutatePosition(this->botright, GeneRectangle::stddev_position); + break; + case 2: + Gene::mutateColor(this->color, GeneRectangle::stddev_color); + break; + } +} + + +sf::FloatRect GeneRectangle::rect() +{ + sf::Vector2f tl; + sf::Vector2f br; + + tl.x = std::min(this->topleft.x, this->botright.x); + br.x = std::max(this->topleft.x, this->botright.x); + + tl.y = std::min(this->topleft.y, this->botright.y); + br.y = std::max(this->topleft.y, this->botright.y); + + br -= tl; // br is now size + + return sf::FloatRect(tl, br); +} diff --git a/src/Genes.hpp b/src/Genes.hpp index 5ca8900..c006c67 100644 --- a/src/Genes.hpp +++ b/src/Genes.hpp @@ -15,7 +15,8 @@ public: enum GeneType { Circle, - Triangle + Triangle, + Rectangle }; static Gene* create(GeneType type); @@ -86,3 +87,29 @@ private: sf::Vector2f pos3; sf::Color color; }; + + + +class GeneRectangle : public Gene +{ +public: + static float stddev_position; + static float stddev_color; + + GeneRectangle(); + virtual ~GeneRectangle(); + + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; + virtual void randomize(); + virtual void mutate(); + +private: + static sf::RectangleShape rectangle; + + sf::Vector2f topleft; + sf::Vector2f botright; + + sf::Color color; + + sf::FloatRect rect(); +}; diff --git a/src/Screens.cpp b/src/Screens.cpp index 69ff2b3..5ed4132 100644 --- a/src/Screens.cpp +++ b/src/Screens.cpp @@ -23,7 +23,7 @@ ScreenSetup::ScreenSetup(Control* controlptr) : Screen(controlptr), offset(20, 20), distance(30), - items(4) + items(5) { this->selection = 0; @@ -131,8 +131,9 @@ void ScreenSetup::executeItem(int position) switch (position) { case 0: Chromosome::toggleGeneType(Gene::Circle); break; case 1: Chromosome::toggleGeneType(Gene::Triangle); break; - case 2: break; // spacing placeholder - case 3: + case 2: Chromosome::toggleGeneType(Gene::Rectangle); break; + case 3: break; // spacing placeholder + case 4: if (Chromosome::isAnyGeneTypeAllowed()) { this->control->switchScreen(this->control->screenGenerations); } else { @@ -151,15 +152,21 @@ void ScreenSetup::drawMenu(sf::RenderTarget& target, sf::RenderStates states) co sf::Vector2f position = this->offset; this->drawMenuItem( - target, states, position, "Circle genes: ", + target, states, position, "Circle genes: ", Chromosome::isGeneTypeAllowed(Gene::Circle)?"x":" " ); position.y += this->distance; this->drawMenuItem( - target, states, position, "Triangle genes:", + target, states, position, "Triangle genes: ", Chromosome::isGeneTypeAllowed(Gene::Triangle)?"x":" " ); + position.y += this->distance; + + this->drawMenuItem( + target, states, position, "Rectangle genes:", + Chromosome::isGeneTypeAllowed(Gene::Rectangle)?"x":" " + ); position.y += 2*this->distance; this->drawMenuItem(