不得不說,我真的是跳票了好久。。。。。從1月20日至1月24日都沒有更新。說好的日更,簡直成了周更!
其實找理由也是不難的,最近我摔了一跤,牙磕碎了一顆,腿腳不便,頭腦眩暈,實在不是寫博客的心情......
不過說借口又有什么用!我不還是每天上着B站嗎?之所以不更新博客,沒有別的原因,就是懶!
那么我再說幾句閑話,反正也沒人看。╮(╯▽╰)╭
恩,最近有點失落。Kinect被收回去了啊!唉唉唉,真是的,那個老師,真是靠不住.....期末也只給我63。
沒見過這種給他出了大力氣還受累不討好的啊!!不過。。。說這個其實還是算是一種掩飾,真正的原因,
還是在於又一次懷疑自己的能力了,又一次懷疑自己所做的事情的意義了。我在這里寫博客,反正也沒人看,
OpenFrameworks你們也都不用,我寫的基礎部分不好,概念也講不清,拔高也做不到,真是失敗,那還
做什么?
因為我想寫。就算我做不好,就算我講不好,只要我想講,我就有權利講,就應該講!於是,開始!
//commonline.h #pragma once #include "ofMain.h" class CommonLine { public: CommonLine(void); ~CommonLine(void); void setStartPoint(double percentX, double percentY); void setEndPoint(double percentX, double percentY); void releaseDrag(); void drawPath(double gridWidth, double gridHeight, double startX, double startY); ofPoint getStartPoint(); ofPoint getEndPoint(); bool isHasPath(); private: ofPoint startPoint; ofPoint endPoint; bool hasPath; };
//CommonLine.cpp #include "CommonLine.h" CommonLine::CommonLine(void) { this->hasPath = false; } CommonLine::~CommonLine(void) { } void CommonLine::setStartPoint(double percentX, double percentY) { if(this->hasPath == true) { return; } this->hasPath = true; this->startPoint.set(percentX,percentY); this->endPoint.set(percentX,percentY); }; void CommonLine::setEndPoint(double percentX, double percentY) { this->endPoint.set(percentX,percentY); if(percentX >1) { if(percentY<1 && percentY >0) { this->endPoint.y = (1-this->startPoint.x) *(percentY-this->startPoint.y) /(percentX-this->startPoint.x) + this->startPoint.y; } if(percentY <0) { this->endPoint.y = (1-this->startPoint.x) *(-percentY-this->startPoint.y) /(percentX-this->startPoint.x) + this->startPoint.y; } this->endPoint.x = 1; } if(percentX <0) { if(percentY>0 && percentY<1) { this->endPoint.y = this->startPoint.x *(percentY-this->startPoint.y) /(this->startPoint.x-percentX) +this->startPoint.y; } if(percentY<0) { this->endPoint.y = this->startPoint.x *(-percentY-this->startPoint.y) /(this->startPoint.x-percentX) +this->startPoint.y; } this->endPoint.x = 0; } if(percentY >1) { if(percentX>0 && percentX<1) { this->endPoint.x = (1-this->startPoint.y) *(percentX-this->startPoint.x) /(percentY-this->startPoint.y) + this->startPoint.x; } if(percentX<0) { this->endPoint.x = (1-this->startPoint.y) *(-percentX-this->startPoint.x) /(percentY-this->startPoint.y) + this->startPoint.x; } this->endPoint.y = 1; } if(percentY <0 && percentX>0 && percentX<1) { if(percentX>0 && percentX<1) { this->endPoint.x = this->startPoint.y *(percentX-this->startPoint.x) /(this->startPoint.y-percentY) +this->startPoint.x; } if(percentX<0) { this->endPoint.x = this->startPoint.y *(percentX-this->startPoint.x) /(this->startPoint.y-percentY) +this->startPoint.x; } this->endPoint.y = 0; } }; void CommonLine::releaseDrag() { this->hasPath = false; } void CommonLine::drawPath(double gridWidth, double gridHeight , double startX, double startY) { if(this->hasPath == true) { ofSetColor(255,0,0); ofLine(gridWidth*this->startPoint.x+startX,gridHeight*this->startPoint.y+startY, gridWidth*this->endPoint.x+startX,gridHeight*this->endPoint.y+startY); } }; ofPoint CommonLine::getStartPoint() { return this->startPoint; }; ofPoint CommonLine::getEndPoint() { return this->endPoint; }; bool CommonLine::isHasPath() { return this->hasPath; }
//gridbox.h #pragma once #include "ofMain.h" #include "LinePath.h" #include "CommonLine.h" class Gridbox { public: Gridbox(void); ~Gridbox(void); void setUpGridBox(int windowX, int windowY); void drawGridBoxWithDots(); void drawCurrentLineDots(); void updateGridDotSize(int windowX, int windowY); void addCurrentLineDots(); void addDotToGridBox(int x, int y); void clearAllDots(); void clickLeft(int mouseX, int mouseY); void clickRight(); void drag(int mouseX, int mouseY); void releaseDrag(); void drawPath(); bool isClickInBox(int mouseX, int mouseY); private: void calculateEdgeStart(int windowX, int windowY); int currentLineNum; int pastWindowX; int pastWindowY; bool dots[41][41]; double dotRadius; double dotPGridRatial; double minEageRatial; int gridStartposX; int gridStartposY; int gridWidth; int minLineNum; int maxLineNum; LinePath thisLinePath; CommonLine thisCommonLine; };
//gridbox.cpp #include "Gridbox.h" #include "ofMain.h" Gridbox::Gridbox(void) { } Gridbox::~Gridbox(void) { } void Gridbox::setUpGridBox(int windowX, int windowY) { this->minLineNum = 10; this->maxLineNum = 40; this->currentLineNum = 40; this->dotPGridRatial = 0.85; this->minEageRatial = 0.05; this->pastWindowX = windowX; this->pastWindowY = windowY; this->calculateEdgeStart(windowX,windowY); this->clearAllDots(); } void Gridbox::drawGridBoxWithDots() { int lineLength = this->currentLineNum * this->gridWidth; ofSetHexColor(0x000000); for(int i=0; i<this->currentLineNum+1; i++) { ofLine(this->gridStartposX,this->gridStartposY+this->gridWidth*i, this->gridStartposX+lineLength,this->gridStartposY+this->gridWidth*i); ofLine(this->gridStartposX+this->gridWidth*i,this->gridStartposY, this->gridStartposX+this->gridWidth*i,this->gridStartposY+lineLength); } ofFill(); ofSetColor(0,0,0); for(int j=0; j<this->currentLineNum+1; j++) { for(int k=0; k<this->currentLineNum+1; k++) { if(this->dots[j][k] == true) { ofEllipse(ofPoint(this->gridStartposX + j*this->gridWidth, this->gridStartposY + k*this->gridWidth),this->dotRadius,this->dotRadius); } } } this->drawCurrentLineDots(); } void Gridbox::updateGridDotSize(int windowX, int windowY) { if(this->pastWindowX != windowX || this->pastWindowY != windowY) { this->pastWindowX = windowX; this->pastWindowY = windowY; this->calculateEdgeStart(windowX,windowY); } } void Gridbox::addCurrentLineDots() { if(this->thisCommonLine.isHasPath() == false) { return; } ofPoint startP; ofPoint endP; ofPoint tempP = this->thisCommonLine.getStartPoint(); startP.x = tempP.x; startP.y = tempP.y; tempP = this->thisCommonLine.getEndPoint(); endP.x = tempP.x; endP.y = tempP.y; startP.x *= (int)(this->currentLineNum+1.5); startP.y *= (int)(this->currentLineNum+1.5); endP.x *= (int)(this->currentLineNum+1.5); endP.y *= (int)(this->currentLineNum+1.5); double k = ( endP.y - startP.y ) / ( endP.x - startP.x ); if( k > 1 || k < -1) { if(startP.y > endP.y ) { ofPoint tempP; tempP.x = startP.x; tempP.y = startP.y; startP.x = endP.x; startP.y = endP.y; endP.x = tempP.x; endP.y = tempP.y; } int a,b,dt1,dt2,d,x,y,ystp=1; a = startP.x - endP.x; b = endP.y - startP.y; if(k<0) { a=-a; ystp = -1; } d = (a<<1)+b; dt1 = a<<1; dt2 = (a+b)<<1; x = startP.x; y = startP.y; this->addDotToGridBox(x,y); while(y<endP.y-1) { if(d<0) { y++; x+=ystp; d+=dt2; } else { y++; d+=dt1; } this->addDotToGridBox(x,y); } } else { if(startP.x > endP.x ) { ofPoint tempP; tempP.x = startP.x; tempP.y = startP.y; startP.x = endP.x; startP.y = endP.y; endP.x = tempP.x; endP.y = tempP.y; } int a,b,dt1,dt2,d,x,y,ystp=1; a = startP.y - endP.y; b = endP.x - startP.x; if(k<0) { a=-a; ystp = -1; } d = (a<<1)+b; dt1 = a<<1; dt2 = (a+b)<<1; x = startP.x; y = startP.y; this->addDotToGridBox(x,y); while(x<endP.x-1) { if(d<0) { x++; y+=ystp; d+=dt2; } else { x++; d+=dt1; } this->addDotToGridBox(x,y); } } } void Gridbox::drawCurrentLineDots() { if(this->thisCommonLine.isHasPath() == false) { return; } ofPoint startP; ofPoint endP; ofPoint tempP = this->thisCommonLine.getStartPoint(); startP.x = tempP.x; startP.y = tempP.y; tempP = this->thisCommonLine.getEndPoint(); endP.x = tempP.x; endP.y = tempP.y; startP.x *= (int)(this->currentLineNum+1.5); startP.y *= (int)(this->currentLineNum+1.5); endP.x *= (int)(this->currentLineNum+1.5); endP.y *= (int)(this->currentLineNum+1.5); double k = ( endP.y - startP.y ) / ( endP.x - startP.x ); if( k > 1 || k < -1) { if(startP.y > endP.y ) { ofPoint tempP; tempP.x = startP.x; tempP.y = startP.y; startP.x = endP.x; startP.y = endP.y; endP.x = tempP.x; endP.y = tempP.y; } int a,b,dt1,dt2,d,x,y,ystp=1; a = startP.x - endP.x; b = endP.y - startP.y; if(k<0) { a=-a; ystp = -1; } d = (a<<1)+b; dt1 = a<<1; dt2 = (a+b)<<1; ofFill(); ofSetColor(0,0,0); x = startP.x; y = startP.y; ofEllipse(ofPoint(this->gridStartposX + x*this->gridWidth, this->gridStartposY + y*this->gridWidth),this->dotRadius,this->dotRadius); while(y<endP.y-1) { if(d<0) { y++; x+=ystp; d+=dt2; } else { y++; d+=dt1; } ofEllipse(ofPoint(this->gridStartposX + x*this->gridWidth, this->gridStartposY + y*this->gridWidth),this->dotRadius,this->dotRadius); } } else { if(startP.x > endP.x ) { ofPoint tempP; tempP.x = startP.x; tempP.y = startP.y; startP.x = endP.x; startP.y = endP.y; endP.x = tempP.x; endP.y = tempP.y; } int a,b,dt1,dt2,d,x,y,ystp=1; a = startP.y - endP.y; b = endP.x - startP.x; if(k<0) { a=-a; ystp = -1; } d = (a<<1)+b; dt1 = a<<1; dt2 = (a+b)<<1; x = startP.x; y = startP.y; ofEllipse(ofPoint(this->gridStartposX + x*this->gridWidth, this->gridStartposY + y*this->gridWidth),this->dotRadius,this->dotRadius); while(x<endP.x-1) { if(d<0) { x++; y+=ystp; d+=dt2; } else { x++; d+=dt1; } ofEllipse(ofPoint(this->gridStartposX + x*this->gridWidth, this->gridStartposY + y*this->gridWidth),this->dotRadius,this->dotRadius); } } } void Gridbox::addDotToGridBox(int x, int y) { this->dots[x][y]=true; } void Gridbox::clearAllDots() { for(int j=0; j<this->maxLineNum+1; j++) { for(int k=0; k<this->maxLineNum+1; k++) { this->dots[j][k] = false; } } } void Gridbox::calculateEdgeStart(int windowX, int windowY) { int miniLine; miniLine = windowY; if(windowX < windowY) { miniLine = windowX; } this->gridWidth = miniLine*(1-2*this->minEageRatial) / this->currentLineNum; this->dotRadius = this->gridWidth * this->dotPGridRatial; this->gridStartposX = ( windowX - this->currentLineNum * this->gridWidth ) / 2; this->gridStartposY = ( windowY - this->currentLineNum * this->gridWidth ) / 2; } void Gridbox::clickLeft(int mouseX, int mouseY) { if(this->isClickInBox(mouseX,mouseY) == true) { /* if(this->thisLinePath.isFull() == true) { this->thisLinePath.clearPath(); this->clearAllDots(); } double gridLength = this->gridWidth * this->currentLineNum; double perX = ( mouseX - this->gridStartposX ) / gridLength; double perY = ( mouseY - this->gridStartposY ) / gridLength; this->thisLinePath.addPathDot(perX,perY); */ double gridLength = this->gridWidth * this->currentLineNum; double perX = ( mouseX - this->gridStartposX ) / gridLength; double perY = ( mouseY - this->gridStartposY ) / gridLength; this->thisCommonLine.setStartPoint(perX,perY); } } void Gridbox::clickRight() { //this->thisLinePath.clearPath(); this->thisCommonLine.releaseDrag(); this->clearAllDots(); } void Gridbox::drag(int mouseX, int mouseY) { double gridLength = this->gridWidth * this->currentLineNum; double perX = ( mouseX - this->gridStartposX ) / gridLength; double perY = ( mouseY - this->gridStartposY ) / gridLength; this->thisCommonLine.setEndPoint(perX,perY); } void Gridbox::releaseDrag() { if(this->thisCommonLine.isHasPath()==true) { this->addCurrentLineDots(); this->thisCommonLine.releaseDrag(); } } bool Gridbox::isClickInBox(int mouseX, int mouseY) { ofRectangle box; double gridLength = this->gridWidth * this->currentLineNum; box.set(ofPoint(this->gridStartposX,this->gridStartposY),gridLength,gridLength); return box.inside(mouseX,mouseY); } void Gridbox::drawPath() { double gridLength = this->gridWidth * this->currentLineNum; //this->thisLinePath.drawPath(this->gridStartposX,this->gridStartposY,gridLength); this->thisCommonLine.drawPath(gridLength,gridLength,this->gridStartposX,this->gridStartposY); }
//linepath.h #pragma once #include "ofMain.h" class LinePath { public: LinePath(void); ~LinePath(void); void clearPath(); void addPathDot(double percentX, double percentY); void drawPath(double startPathposX, double startPathposY, int gridLength); bool isFull(); private: ofPoint storedPathDots[11]; int currentPathDotNum; };
//linepath.cpp #include "LinePath.h" LinePath::LinePath(void) { } LinePath::~LinePath(void) { } void LinePath::clearPath() { this->currentPathDotNum = 0; } bool LinePath::isFull() { if(this->currentPathDotNum >= 10) { return true; } else { return false; } } void LinePath::addPathDot(double percentX, double percenrY) { this->storedPathDots[this->currentPathDotNum].set(percentX,percenrY); this->currentPathDotNum++; } void LinePath::drawPath(double startPathposX, double startPathposY, int gridLength) { ofSetColor(255,0,0); for(int i=0; i<this->currentPathDotNum-1; i++) { ofLine(startPathposX+gridLength*this->storedPathDots[i].x, startPathposY+gridLength*this->storedPathDots[i].y, startPathposX+gridLength*this->storedPathDots[i+1].x, startPathposY+gridLength*this->storedPathDots[i+1].y); } }
//ofapp.h #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); };
//ofapp.cpp #include "ofApp.h" #include "Gridbox.h" Gridbox thisGridBox; //-------------------------------------------------------------- void ofApp::setup(){ int x = ofGetWindowWidth(); int y = ofGetWindowHeight(); thisGridBox.setUpGridBox(x,y); thisGridBox.addDotToGridBox(12,8); thisGridBox.addDotToGridBox(10,6); thisGridBox.addDotToGridBox(11,7); } //-------------------------------------------------------------- void ofApp::update(){ int x = ofGetWindowWidth(); int y = ofGetWindowHeight(); thisGridBox.updateGridDotSize(x,y); } //-------------------------------------------------------------- void ofApp::draw(){ ofBackground(255,255,255); thisGridBox.drawGridBoxWithDots(); thisGridBox.drawPath(); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ } //-------------------------------------------------------------- void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ if(button == OF_MOUSE_BUTTON_LEFT) { thisGridBox.drag(x,y); } } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ if(button == OF_MOUSE_BUTTON_LEFT) { thisGridBox.clickLeft(x,y); } if(button == OF_MOUSE_BUTTON_RIGHT) { thisGridBox.clickRight(); } } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button){ /* if(button == OF_MOUSE_BUTTON_LEFT) { thisGridBox.clickLeft(x,y); } if(button == OF_MOUSE_BUTTON_RIGHT) { thisGridBox.clickRight(); } */ thisGridBox.releaseDrag(); } //-------------------------------------------------------------- void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }
//main.cpp #include "ofMain.h" #include "ofApp.h" //======================================================================== int main( ){ ofSetupOpenGL(800,600,OF_WINDOW); // <-------- setup the GL context // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN // pass in width and height too: ofRunApp(new ofApp()); }
不知道同學們有沒有學過計算機圖形學?一般上來就會學習一個怎么畫直線。可能有人會不解,就一個直線還有什么好教的?
對,你會在紙面上畫直線,但是,這可不是說你就會在計算機的屏幕上畫一條直線出來。實際上,計算機圖形學在畫直線的
時候是以像素為基本點的,所以,一般,書上的圖示會畫得很大,然后把像素畫成圓的。
但是捏,老師在做樣例程序的時候就完全不管那么多了,直接畫像素。那么結果就是,只是畫了一條平凡無奇的直線,根本
沒法看懂這個線是怎么畫的。所以呢,我就以畫出書上插圖那樣的程序為目標,開始了編程。然后,產生的就是上面的那個程序。
我還是多少截個圖。
(紅線是鼠標拖出來的路徑,黑點是擬合出來的直線)
這個程序現在還是一個試玩版,所以很多內容還比較粗糙,但是呢,總有一天我會把這個程序完全做好的!(真的)
到時候,除了畫直線,畫曲線,畫圓畫方,還可以填色,還有橡皮擦,畫刷。
恩,最后我再扯扯淡。
寫博客的價值就是在於看的。如果有人說,我寫博客就是為了好玩,就是為了給自己看,就是為了整理資料,那么我只能說
他是在自欺欺人。這世上自由很多種方式來記錄文字、心情。當然,在技術類博客里,主要還是記錄技術。之所以挑選博客
這種方式,就正是看上了它能便捷的傳播給不特定多數人的特征。起碼我寫博客就是這個目的。
到現在為止,沒有除了我以外的任何人看過這個博客——當然我也沒有嘗試過推薦它到首頁。因為,我知道,現在的它還遠遠
沒有那個資格。但是,總有一天它會的。我要做的就是堅持到那一天。
明天,我還會回來。
對了,關於以后具體講解什么內容的事,我在下次日更時會說清楚的。
再見~O(∩_∩)O~