[work 104] Polygon & Bezier curve
![[work 104] Polygon & Bezier curve](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/10/outFrameImg0000-3.png?fit=1280%2C720&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(255);
ofSetBackgroundAuto(true);
ofSetVerticalSync(true);
std::shared_ptr<Shape> s;
s = make_shared<Shape>(S_TRIANGLE);
s->setup();
shapes.push_back(s);
s = make_shared<Shape>(S_SQUARE);
s->setup();
shapes.push_back(s);
s = make_shared<Shape>(S_PENTAGON);
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, 1000, 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"
enum ShapeType {
S_TRIANGLE = 0,
S_SQUARE,
S_PENTAGON,
S_RANDOM
};
class Shape {
public:
Shape();
Shape(ShapeType type);
~Shape();
void setup();
void update();
void display();
private:
void setPolygon(int n);
ShapeType shapeType;
float size;
std::vector<ofVec2f> points;
std::vector<int> angles;
std::vector<ofVec2f> controls;
};
#endif /* Shape_hpp */
#include "Shape.hpp"
Shape::Shape()
{
shapeType = S_RANDOM;
}
Shape::Shape(ShapeType type)
{
shapeType = type;
}
Shape::~Shape()
{
}
void Shape::setup()
{
if (shapeType == S_TRIANGLE) {
setPolygon(3);
} else if (shapeType == S_SQUARE) {
setPolygon(4);
} else if (shapeType == S_PENTAGON) {
setPolygon(5);
} else {
setPolygon((int)ofRandom(3, 5));
}
for (int i = 0; i < points.size(); i++) {
int next = (i + 1) % points.size();
ofVec2f dir = points.at(next) - points.at(i);
angles.push_back(1);
controls.push_back(dir);
angles.push_back(-1);
controls.push_back(dir * -1);
}
}
void Shape::update()
{
for (int i = 0; i < controls.size(); i++) {
controls.at(i).rotate(angles.at(i));
}
}
void Shape::display()
{
ofPushMatrix();
ofNoFill();
ofSetLineWidth(3);
ofSetColor(0);
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();
ofPopMatrix();
}
void Shape::setPolygon(int n)
{
size = 100;
float angle = 360 / n;
ofVec2f org = ofVec2f(0, -1);
org = org * size;
for (int i = 0; i < n; i++) {
ofVec2f d = org;
points.push_back(d.rotate(angle * 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