Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Last active August 29, 2015 14:17
Show Gist options
  • Save t-kashima/38cc54470a5465a5a09b to your computer and use it in GitHub Desktop.
Save t-kashima/38cc54470a5465a5a09b to your computer and use it in GitHub Desktop.
Openframeworks draw cirlces
#include "Circle.h"
Circle::Circle(float x, float y, float r)
{
_x = x;
_y = y;
_r = r;
_color.r = ofRandom(255);
_color.g = ofRandom(255);
_color.b = ofRandom(255);
_color.a = 255;
_isDestroy = false;
}
Circle::~Circle()
{
}
bool Circle::isDestroy()
{
return _isDestroy;
}
void Circle::draw()
{
if (0 >= _color.a) {
_isDestroy = true;
return;
}
ofSetColor(_color);
ofCircle(_x, _y, _r);
_color.a -= 1;
}
#pragma once
#include "ofMain.h"
class Circle
{
public:
Circle(float x, float y, float r);
~Circle();
void draw();
bool isDestroy();
private:
float _x;
float _y;
float _r;
ofColor _color;
bool _isDestroy;
};
#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
ofSetupOpenGL(200, 200, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
#include "ofApp.h"
#include <math.h>
#define WIDTH 200
#define HEIGHT 200
#define CIRCLE_RADIUS 10
void ofApp::setup()
{
ofSetFrameRate(60);
ofSetWindowShape(WIDTH, HEIGHT);
_x = 0;
_y = 0;
}
void ofApp::update()
{
}
void ofApp::draw()
{
for (std::vector<Circle *>::iterator it = _circles.begin(); it != _circles.end();) {
Circle *circle = (*it);
if (circle->isDestroy()) {
it = _circles.erase(it);
delete circle;
continue;
}
circle->draw();
it++;
}
}
void ofApp::mouseMoved(int x, int y)
{
float distance = sqrt(pow((_x - x), 2) + pow((_y - y), 2));
if ((CIRCLE_RADIUS * 2) > distance) {
return;
}
_x = x;
_y = y;
_circles.push_back(new Circle(_x, _y, CIRCLE_RADIUS));
}
#pragma once
#include "ofMain.h"
#include "./Circle.h"
class ofApp : public ofBaseApp
{
public:
std::vector<Circle *> _circles;
float _x;
float _y;
public:
void setup();
void update();
void draw();
void mouseMoved(int x, int y);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment