void tbhUpdate(Flywheel *flywheel, float timeChange) { flywheel->action += flywheel->error * timeChange * flywheel->tbhGain; if (signOf(flywheel->error) != signOf(flywheel->lastError)) { if (flywheel->firstCross) { flywheel->action = flywheel->tbhApprox; flywheel->firstCross = false; } else { flywheel->action = 0.5f * (flywheel->action + flywheel->lastAction); } flywheel->lastAction = flywheel->action; } flywheel->lastError = flywheel->error; }
static Graph::Face *splitFace(Graph &graph, Graph::Face &face, FRandomStream &random) { auto edgePair = getRandomOpposingEdges(face, random); auto dir = getDirection(*edgePair.first); // Assumes both edges are opposing faces on a rectangle. auto otherDir = getDirection(*edgePair.second); check((dir.x == -1 * otherDir.x) && (dir.y == -1 * otherDir.y)); // If the rectangle is not big enough do not split it. if ((std::abs(dir.x) < 2*MARGIN+1) && (std::abs(dir.y) < 2*MARGIN+1)) return nullptr; // Get a random point along the edges. auto randX = (dir.x != 0 ? signOf(dir.x) * random.RandRange(MARGIN, std::abs(dir.x) - MARGIN) : 0); auto randY = (dir.y != 0 ? signOf(dir.y) * random.RandRange(MARGIN, std::abs(dir.y) - MARGIN) : 0); auto position0 = getSourcePosition(*edgePair.first) + Graph::Position{randX, randY}; auto position1 = getTargetPosition(*edgePair.second) + Graph::Position{randX, randY}; // Split the edges and connect. Graph::Node &node0 = graph.SplitEdge(position0, *edgePair.first); Graph::Node &node1 = graph.SplitEdge(position1, *edgePair.second); return &graph.ConnectNodes(node0, node1); }
void updatePositions() { oldPaddle = paddle; oldBall = ball; if (KEY_DOWN_NOW(BUTTON_RIGHT)) { paddle.x = min(paddle.x + PADDLE_SPEED, GAME_WIDTH - PADDLE_WIDTH); if (ball.onPaddle) ball.x = paddle.x + PADDLE_WIDTH / 2; } if (KEY_DOWN_NOW(BUTTON_LEFT)) { paddle.x = max(paddle.x - PADDLE_SPEED, 0); if (ball.onPaddle) ball.x = paddle.x + PADDLE_WIDTH / 2; } //release ball from paddle if (ball.onPaddle && KEY_DOWN_NOW(BUTTON_A)) { ball.onPaddle = FALSE; ball.xspeed = 1; ball.yspeed = -3; ball.y -= 1; //give it one pixel of space so it doesn't "collide" with paddle right away } //--------------------------CHECK FOR COLLISIONS-------------------------------// //check collision with right boundary if (collisionRect(ball.x, ball.y, ball.x + BALL_SIZE, ball.y + BALL_SIZE, GAME_WIDTH, 0, GAME_WIDTH + 10, GAME_HEIGHT)) { ball.xspeed = -ball.xspeed; ball.x -= 2; } //check collision with top boundary if (collisionRect(ball.x, ball.y, ball.x + BALL_SIZE, ball.y + BALL_SIZE, 0, -10, GAME_WIDTH, 0)) { ball.yspeed = -ball.yspeed; ball.y += 2; } //check collision with left boundary if (collisionRect(ball.x, ball.y, ball.x + BALL_SIZE, ball.y + BALL_SIZE, -10, 0, 0, GAME_HEIGHT)) { ball.xspeed = -ball.xspeed; ball.x += 2; } //check collision with bricks for (int i = 0; i < NUM_OF_BRICKS; i++) { if (brickArray[i].health && collisionRect(ball.x, ball.y, ball.x + BALL_SIZE, ball.y + BALL_SIZE, brickArray[i].x, brickArray[i].y, brickArray[i].x + BRICK_WIDTH, brickArray[i].y + BRICK_HEIGHT)) { int ballxmid = ball.x + BALL_SIZE/2; int ballymid = ball.y + BALL_SIZE/2; int brickxmid = brickArray[i].x + BRICK_WIDTH/2; int brickymid = brickArray[i].y + BRICK_HEIGHT/2; //slope of the vector pointing from the ball to the brick int deltax = brickxmid - ballxmid; int deltay = brickymid - ballymid; //below or above brick if (abs(deltax) < 2 * abs(deltay) + 2) // abs(dy/dx) > 1/2 (visual->) \_____/ { ball.yspeed = -ball.yspeed; ball.y += signOf(ball.yspeed) * 2; //"push it out of brick just a bit" } //side of brick else { ball.xspeed = -ball.xspeed; ball.x += signOf(ball.xspeed) * 2; //"push it out of brick just a bit" } brickArray[i].health -= 1; if (!brickArray[i].health) { //draw part of the background image that was previously hidden drawImageExt3(brickArray[i].x, brickArray[i].y, brickArray[i].x, brickArray[i].y, BRICK_WIDTH, BRICK_HEIGHT, gtech); bricksLeft --; sprintf(bricksString, "%d", bricksLeft); //store lives as a string waitForVblank(); drawRect(215, 85, 12, 8, RGB(6, 0, 6)); drawString(215, 85, bricksString, WHITE); } } } //check collision with paddle if (!ball.onPaddle && collisionRect(ball.x, ball.y, ball.x + BALL_SIZE, ball.y + BALL_SIZE, paddle.x, PADDLE_Y, paddle.x + PADDLE_WIDTH, PADDLE_Y + PADDLE_HEIGHT)) { setNewBallSpeed(); } //check for death if (ball.y > GAME_HEIGHT) { lives--; sprintf(livesString, "%d", lives); //store lives as a string //draw new lives number waitForVblank(); drawRect(215, 35, 6, 8, RGB(6, 0, 6)); drawString(215, 35, livesString, WHITE); setBallOnPaddle(); } ball.x += ball.xspeed; ball.y += ball.yspeed; }
BigInteger::BigInteger(short x) : sign(signOf(x)), mag(magOf<short, unsigned short>(x)) {}
BigInteger::BigInteger(int x) : sign(signOf(x)), mag(magOf<int , unsigned int >(x)) {}
BigInteger::BigInteger(long x) : sign(signOf(x)), mag(magOf<long , unsigned long >(x)) {}
int isqrt(int n){ int sign = signOf(n); n = iabs(n); int cur = n, next = (n+1)/2; while(iabs(next-cur) > 1){ cur = next; next = (cur + n/cur)/2; } return sign*next; }