[work 105] Star & Bezier curve
![[work 105] Star & Bezier curve](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/10/outFrameImg0000-4.png?fit=1024%2C576&ssl=1)
Movie
Source code
about
- 星をベジェ曲線で描く
- アンカーポイントを中心にコントロールポイントを回転させる
file
- 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
- 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"
//================================
int main( ){
// 4K:4096x2160
// 2K:2048x1080
// FullHD:1920x1080
// HD:1440x1080
// HD720p:1280x720
// DVD:720x480
// setup the GL context
ofSetupOpenGL(1280, 720, OF_WINDOW);
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new ofApp());
}
#pragma once
#include "ofMain.h"
#include "Shape.hpp"
class ofApp : public ofBaseApp{
public:
ofApp();
~ofApp();
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);
private:
std::vector<std::shared_ptr<Shape>> shapes;
};
#include "ofApp.h"
ofApp::ofApp(){
}
ofApp::~ofApp(){
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(0);
ofSetBackgroundAuto(true);
ofSetVerticalSync(true);
std::shared_ptr<Shape> s;
for (int i = 0; i < 3; i++) {
s = make_shared<Shape>();
s->setup();
shapes.push_back(s);
}
}
//--------------------------------------------------------------
void ofApp::update(){
for (std::shared_ptr<Shape> s : shapes) {
s->update();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofRectangle window;
window.setFromCenter(ofGetWidth() / 2, ofGetHeight() / 2, 1200, 500);
float width = window.getWidth() / (shapes.size() + 1);
int index = 1;
for (std::shared_ptr<Shape> s : shapes) {
ofPushMatrix();
ofTranslate(window.getTopLeft().x + width * index, window.getCenter().y);
s->display();
ofPopMatrix();
index++;
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == 's') {
ofImage img;
img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
img.save("screenshot.png");
}
}
#ifndef Shape_hpp
#define Shape_hpp
#include <stdio.h>
#include "ofMain.h"
class Shape {
public:
Shape();
~Shape();
void setup();
void update();
void display();
private:
void setStar();
float size;
std::vector<ofVec2f> points;
std::vector<int> angles;
std::vector<ofVec2f> controls;
};
#endif /* Shape_hpp */
#include "Shape.hpp"
Shape::Shape()
{
}
Shape::~Shape()
{
}
void Shape::setup()
{
setStar();
for (int i = 0; i < points.size(); i++) {
int next = (i + 1) % points.size();
ofVec2f dir = points.at(next) - points.at(i);
ofVec2f ci = dir * ofRandom(0.5, 1);
std::array<int, 4> v{-2, -1, 1, 2};
angles.push_back(v.at((int)ofRandom(v.size())));
controls.push_back(ci);
ofVec2f cn = dir * (-1) * ofRandom(0.5, 1);
angles.push_back(v.at((int)ofRandom(v.size())));
controls.push_back(cn);
}
}
void Shape::update()
{
if (ofGetFrameNum() > 0) {
for (int i = 0; i < controls.size(); i++) {
controls.at(i).rotate(angles.at(i));
}
}
}
void Shape::display()
{
ofPushMatrix();
ofNoFill();
ofSetLineWidth(4);
ofSetColor(255);
ofBeginShape();
for (int i = 0; i < points.size(); i++) {
int next = (i + 1) % points.size();
int ctrlIdx = i * 2;
ofVec2f ci = points.at(i) + controls.at(ctrlIdx);
ofVec2f cn = controls.at(ctrlIdx + 1) + points.at(next);
if (i == 0) {
ofVertex(points.at(i));
ofBezierVertex(ci, cn, points.at(next));
} else {
ofBezierVertex(ci, cn, points.at(next));
}
}
ofEndShape();
for (ofVec2f p : points) {
ofFill();
ofSetColor(255, 255, 0);
ofDrawCircle(p, 8);
}
ofPopMatrix();
}
void Shape::setStar()
{
size = 100;
ofVec2f org = ofVec2f(0, -1);
org = org * size;
for (int i = 0; i < 5; i++) {
ofVec2f d = org;
points.push_back(d.rotate(145 * i));
}
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofVec2f |
| openframeworks | ofVertex |
| openframeworks | ofBezierVertex |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode