Last active
August 29, 2015 14:12
-
-
Save rc1/3715e673ef175b5e0c93 to your computer and use it in GitHub Desktop.
OpenFrameworks Pie Chart Example for Jon
This file contains 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
#pragma once | |
#include "ofMain.h" | |
ofMesh generatePieMesh( float value, float radius = 10.f, float segments = 32 ) { | |
// Create our mesh. | |
ofMesh mesh; | |
mesh.setMode( OF_PRIMITIVE_TRIANGLE_FAN ); | |
// Add the center point | |
mesh.addVertex( ofVec3f::zero() ); | |
// Say it is the first point | |
mesh.addIndex( 0 ); | |
// Add the outer points | |
for ( float i = 0; i < segments; ++i ) { | |
// Use our matrix to plot the points. | |
// Because I can't be bothered doing the trig :) | |
ofMatrix4x4 matrix; | |
// This is done backwards | |
// 1. Move to the outside | |
matrix.translate( 0.0f, radius, 0.0f ); | |
// 2. Rotate around the circle | |
matrix.rotate( 360 * (i / segments), 0.0, 0.0, 1.0 ); | |
// 3. Invert the scale so it works clockwise | |
matrix.scale( 1.0, -1.0, 1.0 ); | |
// Create a point at zero and multiple it by our matrix | |
ofVec3f p = ofVec3f::zero() * matrix; | |
// Add the point | |
mesh.addVertex( p ); | |
// Set the point | |
mesh.addIndex( i + 1 ); | |
} | |
return mesh; | |
} |
This file contains 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
#include "ofApp.h" | |
#include "generatePieMesh.h" | |
//-------------------------------------------------------------- | |
void ofApp::setup(){ | |
// For Example 2 | |
// Pre-generate a pi | |
pie = generatePieMesh( 0.4, 100.0, 64 ); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw(){ | |
// Example 1 | |
// ========= | |
ofPushMatrix(); | |
ofPushStyle(); | |
ofTranslate( 100.0f, 100.0f ); | |
ofSetColor( 255, 0, 0 ); | |
// Generate and draw the pie every frame (inefficent) | |
// Generate returns a mesh which we call draw on | |
generatePieMesh( 0.8, 100 ).draw(); | |
ofPopStyle(); | |
ofPopMatrix(); | |
// Example 2 | |
// ========= | |
ofPushMatrix(); | |
ofPushStyle(); | |
ofTranslate( 300.0f, 100.f ); | |
ofSetColor( 0, 255, 0 ); | |
// Draw a pre-generated pie | |
pie.draw(); | |
ofPopStyle(); | |
ofPopMatrix(); | |
} |
This file contains 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
#pragma once | |
#include "ofMain.h" | |
class ofApp : public ofBaseApp{ | |
public: | |
void setup(); | |
void draw(); | |
// For Example 2 | |
ofMesh pie; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment