[ Video ]
[ About ]
「数学デッサン教室 描いて楽しむ数学のかたち」という本に記載のある数式を元にトーラス結び目(7, 2)をopenFrameworksで描写しました
Torus knot.
[ Source ]
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  | 
						#pragma once #include "ofMain.h" class ofApp : public ofBaseApp { public: 	void setup(); 	void update(); 	void draw(); 	void keyPressed(int key) {}; 	void keyReleased(int key) {}; 	void mouseMoved(int x, int y) {}; 	void mouseDragged(int x, int y, int button) {}; 	void mousePressed(int x, int y, int button) {}; 	void mouseReleased(int x, int y, int button) {}; 	void windowResized(int w, int h) {}; 	void dragEvent(ofDragInfo dragInfo) {}; 	void gotMessage(ofMessage msg) {}; 	void draw_torus(float R, float r, float p, float q); 	glm::vec3 make_point(float R, float r, float t, float p, float q); 	ofEasyCam cam; };  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63  | 
						#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { 	ofSetFrameRate(60); 	ofSetWindowTitle("openFrameworks"); 	ofBackground(239); 	ofSetLineWidth(5); 	ofEnableDepthTest(); } //-------------------------------------------------------------- void ofApp::update() { } //-------------------------------------------------------------- void ofApp::draw() { 	this->cam.begin(); 	this->draw_torus(200, 60, 7, 2); 	this->cam.end(); } //-------------------------------------------------------------- void ofApp::draw_torus(float R, float r, float p, float q) { 	ofColor color; 	for (int t_start = ofGetFrameNum() * 0.75; t_start < ofGetFrameNum() * 0.75 + 360; t_start += 30) { 		ofSetColor(39); 		for (int t = t_start; t <= t_start + 20; t++) { 			ofDrawLine(this->make_point(R, r, t, p, q), this->make_point(R, r, t + 1, p, q)); 		} 		color.setHsb(ofMap(t_start % 360, 0, 360, 0, 255), 255, 255); 		ofSetColor(color); 		ofDrawSphere(this->make_point(R, r, t_start + 25, p, q), 10); 	} } //-------------------------------------------------------------- glm::vec3 ofApp::make_point(float R, float r, float t, float p, float q) { 	// 数学デッサン教室 描いて楽しむ数学たち P.39 	float x = (R + r * cos(p * t * DEG_TO_RAD)) * cos(q * t * DEG_TO_RAD); 	float y = (R + r * cos(p * t * DEG_TO_RAD)) * sin(q * t * DEG_TO_RAD); 	float z = r * sin(p * t * DEG_TO_RAD); 	return glm::vec3(x, y, z); } //-------------------------------------------------------------- int main() { 	ofSetupOpenGL(720, 720, OF_WINDOW); 	ofRunApp(new ofApp()); }  |