void drawSierpinskiTriangleHelper(GWindow& gw, double x, double y, int size, int order){ if(order > 0) gw.drawLine(x, y, x + size, y); gw.drawLine(x, y, x + size/2, y - size*sqrt(3)/2); gw.drawLine(x + size, y, x + size/2, y - size*sqrt(3)/2); drawSierpinskiTriangleHelper(gw, x - size/4, y - size*sqrt(3)/4, size/2, order-1); drawSierpinskiTriangleHelper(gw, x + 3*size/4, y - size*sqrt(3)/4, size/2, order-1); drawSierpinskiTriangleHelper(gw, x + size/4, y + size*sqrt(3)/4, size/2, order-1); }
/* * Display ChessBoard in graphics window */ void draw_ChessBoard(Grid<bool> &ChessBoard, GWindow &gw) { int row = ChessBoard.numRows(); int col = ChessBoard.numCols(); double width = gw.getWidth(); double height = gw.getHeight(); double cell_width = width/col; double cell_height = height/row; for(int i = 0; i < row; i++) { gw.drawLine(0, i*cell_height, width, i*cell_height); gw.drawLine(i*cell_width, 0, i*cell_width, height); } }
pair<double, double> drawCoordinateSystem(GWindow &graphicsWindow, int scale) { pair<double, double> result; result.first = graphicsWindow.getWidth() / 2; result.second = graphicsWindow.getHeight() / 2; graphicsWindow.drawLine(result.first, 0, result.first, result.second * 2); graphicsWindow.drawLine(0, result.second, result.first * 2, result.second); for (int x = -result.first / scale + 1; x <= result.first / scale - 1; x++) graphicsWindow.drawLine(result.first + x * scale, result.second - 5, result.first + x * scale, result.second + 5); for (int y = -result.second / scale + 1; y <= result.second / scale - 1; y++) graphicsWindow.drawLine(result.first - 5, result.second + y * scale, result.first + 5, result.second + y * scale); GLabel* xMark = new GLabel("x", result.first * 2 - 20, result.second + 15); graphicsWindow.add(xMark); GLabel* yMark = new GLabel("y", result.first + 10, 20); graphicsWindow.add(yMark); return result; }
void drawSierpinskiTriangle(GWindow& gw, double x, double y, double size, int order) { // TODO: write this function //gw.drawLine(20,20,30,80); if (order <= 0) return; if (order == 1) { gw.drawLine(x,y,x+size,y); gw.drawLine(x+size,y,x+0.5*size,y+sqrt(3)/2.0*size); gw.drawLine(x,y,x+0.5*size,y+sqrt(3)/2.0*size); } else { drawSierpinskiTriangle(gw, x, y, 0.5*size, order - 1); drawSierpinskiTriangle(gw, x + 0.5*size, y, 0.5*size, order - 1); drawSierpinskiTriangle(gw, x + 0.25*size, y + sqrt(3)/4.0*size, 0.5*size, order - 1); } }
/* * Function: drawSierpinskiTriangle(); * -------------------- * Draw a Sierpinski Triangle of a given order * * Preconditions: valid x,y values, order >=0 * * @param: Start position, size of sides, order of sierpinski triangle * @return: Nothing, but does draw something */ void drawSierpinskiTriangle(GWindow& gw, double x, double y, int size, int order) { // If the order is >0, draw the first triangle (this case is a little different than rest) // If not, do nothing, as we should // Check to make sure out parameters are valid checkSierpinskiParams(gw, x, y, size, order); if(order>0) { //determine the new midpoints double x2=x+size*cos(PI/3); double y2=y+size*sin(PI/3); double x3=x+size; double y3=y; // Draw outer triangle gw.drawLine(x,y,x2,y2); gw.drawLine(x,y,x3,y3); gw.drawLine(x2,y2,x3,y3); } // Start the recursive process drawSierpinskiHelp(gw,x,y,size,order); }
/* * Function: drawSierpinskiHelp(); * -------------------- * Helper function that does the actual recursion * * Preconditions: valid x,y values, order >=0 * * @param: Start position, size of sides, order of sierpinski triangle * @return: Nothing, but does draw something */ void drawSierpinskiHelp(GWindow& gw, double x, double y, int size, int order) { // As long as the order is large enough we will start recursing if(order>1){ // Determine the new midpoints double x2=x+size*cos(PI/3); double y2=y+size*sin(PI/3); double x3=x+size; double y3=y; // Draw the triangle connecting the midpoints gw.drawLine((x2+x)/2,(y2+y)/2,(x3+x2)/2,(y2+y3)/2); gw.drawLine((x3+x2)/2,(y2+y3)/2,(x+x3)/2,(y3+y)/2); gw.drawLine((x3+x)/2,(y+y3)/2,(x2+x)/2,(y2+y)/2); /* Repeat this process with 3 recursive calls */ // Make starting point the top left corner drawSierpinskiTriangle(gw,x,y,size/2,order-1); // Make starting point middle of left edge drawSierpinskiTriangle(gw,(x2+x)/2,(y+y2)/2,size/2,order-1); // make starting point middle of top edge drawSierpinskiTriangle(gw,(x3+x)/2,(y+y3)/2,size/2,order-1); } }
//builds two lines from each of two gotten vertex and calls itself recursively until the depth of recursion becomes zero void drawPythagoreanTree(GWindow &gw, GPoint endPoint1, GPoint endPoint2, double input_length, double theta, int recursion_depth){ double theta_one = theta - THETA; double theta_two = theta + THETA; double length = input_length * cos(THETA_RADIAN); GPoint branch1_1 = gw.drawPolarLine(endPoint1, length*2, theta_one); GPoint branch1_2 = gw.drawPolarLine(endPoint2, length, theta_one); gw.setColor("YELLOW"); gw.drawLine(branch1_1, branch1_2); GPoint branch2_1 = gw.drawPolarLine(endPoint1, length, theta_two); GPoint branch2_2 = gw.drawPolarLine(endPoint2, length*2, theta_two); gw.setColor("GREEN"); gw.drawLine(branch2_1, branch2_2); recursion_depth--; if(recursion_depth == 0){ return; } drawPythagoreanTree(gw, branch1_1, branch1_2, length, theta_one, recursion_depth); drawPythagoreanTree(gw, branch2_1, branch2_2, length, theta_two, recursion_depth); }
void drawSierpinkiTriangle(GPoint & top,int edge,int order,GWindow & gw) { int triangleHeight = (int)sqrt(pow((double)edge,2) - pow((double)edge/2,2)); GPoint ptA = top; GPoint ptB((top.getX() - (triangleHeight/2)),(top.getY()+triangleHeight)); GPoint ptC((top.getX() + (triangleHeight/2)),(top.getY()+triangleHeight)); gw.drawLine(ptA,ptB); gw.drawLine(ptB,ptC); gw.drawLine(ptC,ptA); if(order != 0) { order -= 1; edge = edge / 2; if(edge != 0) { triangleHeight = triangleHeight / 2; GPoint ptAb = top; GPoint ptBb((top.getX()-(triangleHeight/2)),(top.getY()+triangleHeight)); GPoint ptCb((top.getX() + (triangleHeight / 2)), (top.getY() + triangleHeight)); drawSierpinkiTriangle(ptAb, edge, order, gw); drawSierpinkiTriangle(ptBb, edge, order, gw); drawSierpinkiTriangle(ptCb, edge, order, gw); } } }