Created
April 12, 2022 16:03
-
-
Save stungeye/e4662b9799cda3896b288ea3b03ea94e to your computer and use it in GitHub Desktop.
Shape and Derived Shapes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define a new class called Shape | |
// | |
// Properties of Shape: | |
// - color (ofColor) // ofColor::cadetBlue | |
// - position (glm::vec2) // {0, 0} | |
// | |
// Constructor that takes in a ofColor and a glm::vec2 | |
// and uses those parameters to initial the properties. | |
// | |
// Shape myShape{ {12, 34}, ofColor::cadetBlue }; | |
class Shape { | |
protected: | |
glm::vec2 position; | |
ofColor color; | |
public: | |
Shape(const glm::vec2& position, const ofColor& color) | |
: position{position}, color{color} { | |
} | |
virtual void render() = 0; // Pure Virtual Function. This will make Shape an Abstract Class. | |
}; | |
// Create two derived classes that inherit from Shape | |
// | |
// - Rectangle | |
// - width (int) | |
// - height (int) | |
// - Constructor that takes in a position, color, width, height | |
// - Heart | |
// - size (int) | |
// - Constructor that takes in position, color, size | |
// | |
// For each give them a render function... you can try implementing these. | |
class Rectangle : public Shape { | |
int width; | |
int height; | |
public: | |
Rectangle(const glm::vec2& position, const ofColor& color, int width, int height) | |
: Shape{position, color}, width{width}, height{height} { | |
} | |
void render() override { | |
ofSetColor(color); | |
ofDrawRectangle(position.x, position.y, width, height); | |
} | |
}; | |
class Heart : public Shape { | |
int size; | |
public: | |
Heart(const glm::vec2& position, const ofColor& color, int size) | |
: Shape{position, color}, size{size} { | |
} | |
void render() override { | |
// std::cout << "Drawing Heart of s{" << size << "} @ x: " << position.x << " y: " << position.y << "\n"; | |
ofSetColor(color); | |
ofBeginShape(); | |
float i = 0; | |
while (i < TWO_PI) { | |
float r = (2 - 2 * sin(i) + sin(i) * sqrt(abs(cos(i))) / (sin(i) + 1.4)) * -(size * 0.25); | |
float x = position.x + cos(i) * r; | |
float y = position.y + sin(i) * r; | |
ofVertex(x, y); | |
i += 0.005 * HALF_PI * 0.5; | |
} | |
ofEndShape(true); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment