//-------------------------------------------------------------- void ofApp::DrawKoch(ofVec2f start,ofVec2f end, int depth){ if(depth < 10){ ofVec2f mid1 = start + (end-start) / 3.0f; ofVec2f mid3 = start + (end-start) / 3.0f * 2; ofVec2f mid2_temp = (start+end) / 2.0f; float dist = ofDist(start.x, start.y, end.x, end.y); //片方マイナスにしないとおかしくなる ofVec2f mid2 = ofVec2f( mid2_temp.x - (end.y - start.y) / 3.0f *(sqrt(3.0)/2), mid2_temp.y + (end.x - start.x) / 3.0f *(sqrt(3.0)/2)); depth++; DrawKoch(start, mid1, depth); DrawKoch(mid1, mid2, depth); DrawKoch(mid2, mid3, depth); DrawKoch(mid3, end, depth); }else{ ofDrawLine(start, end); } }
//-------------------------------------------------------------- void ofApp::draw(){ DrawKoch(ofVec2f(ofGetWidth()-200,540),ofVec2f(200,540),_depth); DrawKoch(ofVec2f(ofGetWidth()/2,0),ofVec2f(ofGetWidth()-200,540),_depth); DrawKoch(ofVec2f(200,540),ofVec2f(ofGetWidth()/2,0),_depth); ofDrawBitmapString("FrameNum"+ofToString(ofGetElapsedTimef()), 30,45); ofDrawBitmapString("Depth"+ofToString(_depth), 30,30); }
void DrawKoch(float* vx, float* vy, double angle, int times) { float Vsnow[3][2] = {{0, 0}, {0, 0}, {0, 0}}; if( times == 0 ){ glBegin(GL_LINES); glColor3f(0, 255, 0); glVertex2f(vx[0], 480 - vx[1]); glVertex2f(vy[0], 480 - vy[1]); glEnd(); }else{ times--; /* Compute the 1st and 3rd Point along the input line */ if ((vx[0] <= vy[0]) && (vx[1] <= vy[1])){ Vsnow[0][0] = vx[0] + (vy[0] - vx[0])/3; Vsnow[2][0] = vx[0] + 2*(vy[0] - vx[0])/3; Vsnow[0][1] = vx[1] + (vy[1] - vx[1])/3; Vsnow[2][1] = vx[1] + 2*(vy[1] - vx[1])/3; } if ((vx[0] <= vy[0]) && (vx[1] >= vy[1])){ Vsnow[0][0] = vx[0] + (vy[0] - vx[0])/3; Vsnow[2][0] = vx[0] + 2*(vy[0] - vx[0])/3; Vsnow[0][1] = vy[1] + 2*(vx[1] - vy[1])/3; Vsnow[2][1] = vy[1] + (vx[1] - vy[1])/3; } if ((vx[0] >= vy[0]) && (vx[1] >= vy[1])){ Vsnow[0][0] = vy[0] + 2*(vx[0] - vy[0])/3; Vsnow[2][0] = vy[0] + (vx[0] - vy[0])/3; Vsnow[0][1] = vy[1] + 2*(vx[1] - vy[1])/3; Vsnow[2][1] = vy[1] + (vx[1] - vy[1])/3; } if ((vx[0] >= vy[0]) && (vx[1] <= vy[1])){ Vsnow[0][0] = vy[0] + 2*(vx[0] - vy[0])/3; Vsnow[2][0] = vy[0] + (vx[0] - vy[0])/3; Vsnow[0][1] = vx[1] + (vy[1] - vx[1])/3; Vsnow[2][1] = vx[1] + 2*(vy[1] - vx[1])/3; } /* Compute the coordinate of the Middle Point*/ Vsnow[1][0] = Vsnow[2][0]/2 - Vsnow[2][1]*(sqrt(3)/2) + Vsnow[0][0]/2 \ + Vsnow[0][1]*(sqrt(3)/2); Vsnow[1][1] = Vsnow[2][0]*(sqrt(3)/2) + Vsnow[2][1]/2 + Vsnow[0][1]/2 \ - Vsnow[0][0]*(sqrt(3)/2); DrawKoch(vx, Vsnow[0], Pi/3, times); DrawKoch(Vsnow[0], Vsnow[1], Pi/3, times); DrawKoch(Vsnow[1], Vsnow[2], Pi/3, times); DrawKoch(Vsnow[2], vy, Pi/3, times); } }
// // Draw the scene // void DrawScene() { renderBitmapString(20, 20, GLUT_BITMAP_HELVETICA_18, "Press <- or -> ro rotate the object."); renderBitmapString(20, 40, GLUT_BITMAP_HELVETICA_18, "Repeatedly press will increase the rotating speed."); renderBitmapString(20, 60, GLUT_BITMAP_HELVETICA_18, "Press Esc to exit.ENJOY ^_^"); if (PointsCounter == 3 && TurnLeft == true){ // DrawTriangle(); /* glBegin(GL_LINE_LOOP); glColor3f(0, 255, 255); glVertex2f(ClickedPoints[0][0], 480 - ClickedPoints[0][1]); glVertex2f(ClickedPoints[1][0], 480 - ClickedPoints[1][1]); glVertex2f(ClickedPoints[2][0], 480 - ClickedPoints[2][1]); glEnd(); */ glLoadIdentity(); glTranslatef(TriCenter[0], TriCenter[1], 0); glRotatef(-RotationAngle, 0.0, 0.0, 0.000001); glTranslatef(-TriCenter[0], -TriCenter[1], 0); sleepControl(); DrawKoch(ClickedPoints[0], ClickedPoints[1], Pi/3, 4); DrawKoch(ClickedPoints[1], ClickedPoints[2], Pi/3, 4); DrawKoch(ClickedPoints[2], ClickedPoints[0], Pi/3, 4); } if (PointsCounter == 3 && TurnRight == true){ glLoadIdentity(); glTranslatef(TriCenter[0], TriCenter[1], 0); glRotatef(RotationAngle, 0.0, 0.0, 1.0); glTranslatef(-TriCenter[0], -TriCenter[1], 0); sleepControl(); DrawKoch(ClickedPoints[0], ClickedPoints[1], Pi/3, 4); DrawKoch(ClickedPoints[1], ClickedPoints[2], Pi/3, 4); DrawKoch(ClickedPoints[2], ClickedPoints[0], Pi/3, 4); } if (PointsCounter == 3 && TurnRight == false && TurnLeft == false){ // renderBitmapString(20, 20, GLUT_BITMAP_HELVETICA_18, "Press <- or -> ro rotate the object."); // renderBitmapString(20, 40, GLUT_BITMAP_HELVETICA_18, "Repeatedly press will increase the rotating speed."); // renderBitmapString(20, 50, GLUT_BITMAP_HELVETICA_18, "Press Esc to exit.ENJOY ^_^"); DrawKoch(ClickedPoints[0], ClickedPoints[1], Pi/3, 4); DrawKoch(ClickedPoints[1], ClickedPoints[2], Pi/3, 4); DrawKoch(ClickedPoints[2], ClickedPoints[0], Pi/3, 4); } }