[ Video ]
[ About ]
二つの円周が重なったら交点を計算して、くり抜いた形状で描写
Double circle.
[ 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 |
#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 mouseEntered(int x, int y) {}; void mouseExited(int x, int y) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; bool compute_angle(glm::vec2 location_1, float radius_1, glm::vec2 location_2, float radius_2, double& theta, double& a); }; |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetColor(39); ofNoFill(); ofSetLineWidth(5); ofSetCircleResolution(72); } //-------------------------------------------------------------- void ofApp::update() { ofSeedRandom(39); } //-------------------------------------------------------------- void ofApp::draw() { auto location_1 = glm::vec2(ofGetWidth() * 0.5, ofGetHeight() * 0.5); auto radius_1 = 280; auto location_2 = glm::vec2(ofMap(ofNoise(ofRandom(1000), ofGetFrameNum() * 0.008), 0, 1, 0, ofGetWidth()), ofMap(ofNoise(ofRandom(1000), ofGetFrameNum() * 0.008), 0, 1, 0, ofGetHeight())); auto radius_2 = 120; auto theta = 0.0; auto a = 0.0; auto result = this->compute_angle(location_1, radius_1, location_2, radius_2, theta, a); if (result) { vector<glm::vec2> vertices; for (auto rad = theta + a; rad <= theta + 2 * PI - a; rad += PI / 180) { vertices.push_back(location_1 + glm::vec2(radius_1 * cos(rad), radius_1 * sin(rad))); } this->compute_angle(location_2, radius_2, location_1, radius_1, theta, a); for (auto rad = theta + a; rad >= theta - a; rad -= PI / 180) { vertices.push_back(location_2 + glm::vec2(radius_2 * cos(rad), radius_2 * sin(rad))); } ofFill(); ofSetColor(239, 39, 39); ofBeginShape(); ofVertices(vertices); ofEndShape(); ofNoFill(); ofSetColor(39); ofBeginShape(); ofVertices(vertices); ofEndShape(); } else { ofFill(); ofSetColor(239, 39, 39); ofDrawCircle(location_1, radius_1); ofNoFill(); ofSetColor(39); ofDrawCircle(location_1, radius_1); ofFill(); ofSetColor(239); ofDrawCircle(location_2, radius_2); ofNoFill(); ofSetColor(39); ofDrawCircle(location_2, radius_2); } } //-------------------------------------------------------------- bool ofApp::compute_angle(glm::vec2 location_1, float radius_1, glm::vec2 location_2, float radius_2, double& theta, double& a) { auto distance = glm::distance(location_1, location_2); if (distance > radius_1 + radius_2 || distance < radius_1 - radius_2) { return false; } theta = atan2(location_2.y - location_1.y, location_2.x - location_1.x); auto cos_a = (pow(distance, 2) + pow(radius_1, 2) - pow(radius_2, 2)) / (2 * distance * radius_1); a = acos(cos_a); return true; } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |