Skip to content

Instantly share code, notes, and snippets.

@hideyukisaito
Created February 7, 2016 10:26
Show Gist options
  • Save hideyukisaito/5f7f7a90b2bff8fab8d9 to your computer and use it in GitHub Desktop.
Save hideyukisaito/5f7f7a90b2bff8fab8d9 to your computer and use it in GitHub Desktop.
#include "ofMain.h"
class ofApp : public ofBaseApp
{
public:
ofMesh mesh, ellipse;
double scale = 200.0;
//--------------------------------------------------------------
void setup()
{
ofBackground(0);
mesh.setMode(OF_PRIMITIVE_LINE_LOOP);
mesh.addVertices({
ofVec2f(ofRandom(1.0), ofRandom(-1.0)) * scale,
ofVec2f(ofRandom(-1.0), ofRandom(-1.0)) * scale,
ofVec2f(0, ofRandom(1.0)) * scale,
});
ellipse.setMode(OF_PRIMITIVE_LINE_LOOP);
}
//--------------------------------------------------------------
void draw()
{
ofSetColor(255);
ofPushMatrix();
{
// ベースになる三角形
ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5);
mesh.draw(OF_MESH_WIREFRAME);
}
ofPopMatrix();
// 「三角形の重心座標 = 0 を使い(x3, y3)を消去」( http://d.hatena.ne.jp/Hyperion64/20120316/p1 )
const double x1 = mesh.getVertices().at(0).x, y1 = mesh.getVertices().at(0).y;
const double x2 = mesh.getVertices().at(1).x, y2 = mesh.getVertices().at(1).y;
const double a = (x2 * y1 - x1 * y2) / std::sqrt(std::pow(y1, 2.0) + y1 * y2 + std::pow(y2, 2.0));
const double b = (x2 * y1 - x1 * y2) / std::sqrt(std::pow(x1, 2.0) + x1 * x2 + std::pow(x2, 2.0));
const double c = -((x1 * (2 * y1 + y2) + x2 * (y1 + 2 * y2)) / std::pow((x2 * y1 - x1 * y2), 2.0));
const double check = (std::pow(x2, 2.0) / std::pow(a, 2.0)) + (std::pow(y2, 2.0) / std::pow(b, 2.0)) + c * x2 * y2;
ofLog() << check; // = 1
// 楕円
double resolution = 128.0;
ellipse.clear();
for (int i = 0; i < (int)resolution; ++i)
{
double theta = TWO_PI / resolution * i;
// 楕円の媒介変数表示 x = acosθ, y = b / a・asinθ を使って円周上の座標を決定
// c を使った回転の適用方法が分からない、、、
ofVec2f v = ofVec2f(a * std::cos(theta), b * std::sin(theta));
ellipse.addVertex(v);
}
ofPushMatrix();
{
ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5);
ellipse.draw(OF_MESH_WIREFRAME);
}
ofPopMatrix();
}
void mousePressed(int x, int y, int button)
{
mesh.clear();
mesh.addVertices({
ofVec2f(ofRandom(1.0), ofRandom(-1.0)) * scale,
ofVec2f(ofRandom(-1.0), ofRandom(-1.0)) * scale,
ofVec2f(0, ofRandom(1.0)) * scale,
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment