46.
‣ オブジェクト指向プログラムのポイント:その3
‣ 必要のない情報は隠す (カプセル化)
‣ プログラムの実装全てを知る必要はない
‣ 必要なインターフェイス(接点)だけ見せて、あとは隠す
To invent programs, you need to be able to capture abstractions and ex
design. It’s the job of a programming language to help you do this. The
process of invention and design by letting you encode abstractions tha
It should let you make your ideas concrete in the code you write. Surf
the architecture of your program.
All programming languages provide devices that help express abstrac
are ways of grouping implementation details, hiding them, and giving
a common interface—much as a mechanical object separates its interfa
illustrated in “Interface and Implementation” .
Figure 2-1 Interface and Implementation
9
10
11
8
7
6
implementationinterface
インターフェイス 実装
OOP:ポイントその3
114.
インタラクションを追加
‣ マウスをクリックすると、クリックした位置からパーティクル
を放出するように改造
‣ 参考:testAppのマウスに関するインタラクション
‣ void mouseMoved(int x, int y );
‣ マウスを移動
‣ void mouseDragged(int x, int y, int button);
‣ マウスをドラッグ
‣ void mousePressed(int x, int y, int button);
‣ マウスのボタンを押した瞬間
‣ void mouseReleased();
‣ マウスの押していたボタンを離した瞬間
115.
testAppクラスの実装
‣ testApp.cppに、下記の処理を追加する
void testApp::mousePressed(int x, int y, int button){
! p.setInitialCondition(x,y,ofRandom(-10,10), ofRandom(-10,10));
}
128.
複数のパーティクルを同時に動かす(Array版)
‣ ヘッダーファイル:testApp.h
#pragma once
#include "ofMain.h"
#include "Particle.h"
#define NUM 100
class testApp : public ofSimpleApp{
!
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();
//クラスParticleの配列 (NUM個)
Particle p[NUM];
};
129.
複数のパーティクルを同時に動かす(Array版)
‣ 実装ファイル:testApp.cpp
#include "testApp.h"
void testApp::setup(){!
! ofSetVerticalSync(true);
! ofSetFrameRate(60);
! ofBackground(0, 0, 0);
}
void testApp::update(){
for (int i = 0; i < NUM; i++) {
p[i].resetForce();
p[i].addForce(0, 0.1);
p[i].addDampingForce();
p[i].update();
}
}
void testApp::draw(){
! ofSetColor(255, 255, 255);
for (int i = 0; i < NUM; i++) {
p[i].draw();
}
}
130.
複数のパーティクルを同時に動かす(Array版)
‣ 実装ファイル:testApp.cpp
...《中略》...
void testApp::mousePressed(int x, int y, int button){
for (int i = 0; i < NUM; i++) {
p[i].setInitialCondition(x, y, ofRandom(-10,10), ofRandom(-10,10));
}
}