Skip to content

Instantly share code, notes, and snippets.

@tado
Last active February 3, 2018 12:47
Show Gist options
  • Save tado/a90054d39d7031848396d2ef7f29be78 to your computer and use it in GitHub Desktop.
Save tado/a90054d39d7031848396d2ef7f29be78 to your computer and use it in GitHub Desktop.
3次元空間の2つの点から平面の角度を計算
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(0);
ofSetDepthTest(true);
ofSetRectMode(OF_RECTMODE_CENTER);
posA.set(-80, -90, -200); // 開始点を設定
posB.set(100, 120, 130); // 終了点を設定
//開始点と終了点の中間の点を算出
posC = posA;
posC.interpolate(posB, 0.5);
}
//--------------------------------------------------------------
void ofApp::update(){
//2つの点から回転クオータニオンを算出
rot.makeRotate(ofVec3f(1,0,0), posB - posA);
//クオータニオンから、x, y, z軸の回転角度を算出
rot.getRotate(angle, axis);
}
//--------------------------------------------------------------
void ofApp::draw(){
cam.begin();
//全体の枠を描画
ofNoFill();
ofDrawBox(0, 0, 0, 500, 500, 500);
ofFill();
//開始点、終了点、中間点を描画
ofSetColor(0, 0, 255);
ofDrawSphere(posA, 5);
ofDrawSphere(posB, 5);
ofSetColor(255, 0, 0);
ofDrawSphere(posC, 5);
//面を描画
ofPushMatrix();
//2つの地点の中間点に移動
ofTranslate(posC);
//2つの点の角度で回転
ofRotate(angle, axis.x, axis.y, axis.z);
//中間点を中心に面(矩形)を描画
ofSetColor(255);
ofDrawRectangle(0, 0, 500, 50);
ofPopMatrix();
cam.end();
}
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
ofVec3f posA; //開始点
ofVec3f posB; //終了点
ofVec3f posC; //AとBの中間点
ofQuaternion rot; //回転クオータニオン
ofVec3f axis; //3次元の軸
float angle; //角度
ofEasyCam cam; //カメラ
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment