Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Last active August 29, 2015 14:18
Show Gist options
  • Save t-kashima/a700b6185251373072e4 to your computer and use it in GitHub Desktop.
Save t-kashima/a700b6185251373072e4 to your computer and use it in GitHub Desktop.
Draw square for C++
#include "ofApp.h"
#include <math.h>
#define WIDTH 200
#define HEIGHT 200
void ofApp::setup()
{
ofSetFrameRate(60);
ofSetWindowShape(WIDTH, HEIGHT);
ofSetBackgroundColor(0, 0, 0);
_currentFrame = 0;
_square = new Square();
}
void ofApp::update()
{
}
void ofApp::draw()
{
ofSetColor(0, 0, 0);
ofRect(0, 0, ofGetWindowWidth(), ofGetWindowHeight());
_currentFrame += 1;
_square->draw(ofGetWindowWidth() / 2, ofGetWindowHeight() / 2, 50, 50);
if (_currentFrame > ofGetFrameRate()) {
_currentFrame = 0;
}
}
#pragma once
#include "ofMain.h"
#include "Square.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
private:
Square *_square;
int _currentFrame;
};
#include "Square.h"
Square::Square()
: _linearType(LinearTypeTop)
, _lineLength(0)
, _alpha(0)
{
}
Square::~Square()
{
}
void Square::draw(float centerX, float centerY, float width, float height)
{
this->drawSquare(centerX, centerY, width, height);
_lineLength += 1.0;
switch (_linearType) {
case LinearTypeTop:
case LinearTypeBottom:
{
if (_lineLength > width) {
_lineLength = 0;
_alpha = 0;
this->nextLinearType();
} else {
_alpha = _lineLength / width;
}
}
break;
case LinearTypeRight:
case LinearTypeLeft:
{
if (_lineLength > height) {
_lineLength = 0;
_alpha = 0;
this->nextLinearType();
} else {
_alpha = _lineLength / height;
}
}
break;
}
}
void Square::drawSquare(float centerX, float centerY, float width, float height)
{
ofPath line = ofPath();
line.setStrokeColor(ofColor(255, 255, 255, 255 * _alpha));
line.setFilled(false);
line.setStrokeWidth(3.0 * _alpha);
switch (_linearType) {
case LinearTypeTop:
{
line.moveTo((centerX - width / 2), (centerY - height / 2), 0);
line.lineTo((centerX - width / 2) + _lineLength, (centerY - height / 2), 0);
}
break;
case LinearTypeRight:
{
line.moveTo((centerX + width / 2), (centerY - height / 2), 0);
line.lineTo((centerX + width / 2), (centerY - height / 2) + _lineLength, 0);
}
break;
case LinearTypeBottom:
{
line.moveTo((centerX + width / 2), (centerY + height / 2), 0);
line.lineTo((centerX + width / 2) - _lineLength, (centerY + height / 2), 0);
}
break;
case LinearTypeLeft:
{
line.moveTo((centerX - width / 2), (centerY + height / 2), 0);
line.lineTo((centerX - width / 2), (centerY + height / 2) - _lineLength, 0);
}
break;
default:
{
}
break;
}
line.draw();
line.close();
}
void Square::nextLinearType()
{
switch (_linearType) {
case LinearTypeTop:
{
_linearType = LinearTypeRight;
}
break;
case LinearTypeRight:
{
_linearType = LinearTypeBottom;
}
break;
case LinearTypeBottom:
{
_linearType = LinearTypeLeft;
}
break;
case LinearTypeLeft:
{
_linearType = LinearTypeTop;
}
break;
default:
{
_linearType = LinearTypeTop;
}
break;
}
}
#pragma once
#include "ofMain.h"
class Square
{
public:
Square();
~Square();
void draw(float centerX, float centerY, float width, float height);
private:
enum LinearType {
LinearTypeTop,
LinearTypeRight,
LinearTypeBottom,
LinearTypeLeft
};
private:
LinearType _linearType;
float _lineLength;
float _alpha;
private:
/**
* @brief 四角を描画する
*/
void drawSquare(float centerX, float centerY, float width, float height);
/**
* @brief 次の線形のタイプへ
*/
void nextLinearType();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment