Esempio n. 1
0
static void drawCube(int row, int col, char ch, bool invert) {
   drawAndFillRoundedRect(cubeX(col), cubeY(row),
                          gState.cubeSize, gState.cubeSize,
                          gState.cubeSize/5.0,
                          invert ? LETTER_COLOR : DIE_COLOR);
   gwp->setColor(invert ? DIE_COLOR : LETTER_COLOR);
   drawCenteredChar(cubeX(col) + gState.cubeSize/2.0,
                    cubeY(row) + gState.cubeSize/2.0, ch);
}
Esempio n. 2
0
unsigned int Hexagon::orientationTo(Hexagon* hexagon)
{
    int dx = hexagon->cubeX() - cubeX();
    int dy = hexagon->cubeY() - cubeY();

    switch(dx)
    {
        case 0: // 0 || 3
        {
            return dy == 1 ? 0 : 3;
        }
        case 1: // 1 || 2
        {
            return dy == 0 ? 1 : 2;
        }
        case -1: // 4 || 5
        {
            return dy == 0 ? 4 : 5;
        }
    }
    return 0;
}
Esempio n. 3
0
float AMBezier::solveForY(float x, float x1, float y1, float x2, float y2)
{
	D3DXVECTOR2 point0, point1, point2, point3;

	//equation is linear with slope=1 if x & y values match
	if (x1 == y1 && x2 == y2)
		return x;

	//cannot estimate solution if invalid coordinates provided
	//just return x value as if the equation was linear
	if (x1 > 1 || y1 > 1 || x2 > 1 || y2 > 1 || x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0)
		return x;

	//set up coordinates
	point1.x = x1;
	point1.y = y1;
	point2.x = x2;
	point2.y = y2;

	//create point 0 & 3 based on MMD spec
	point0 = D3DXVECTOR2(0, 0);
	point3 = D3DXVECTOR2(1, 1);

	//calculate the coefficients to use when solving the x cubic poly
	//cx = 3(x1-x0)
	float cx = 3.0f*(point1.x-point0.x);
	//bx = 3(x2-x1) - cx
	float bx = 3.0f*(point2.x-point1.x) - cx;
	//ax = x3 - x0 - cx - bx
	float ax = point3.x-point0.x-cx-bx;
	//dx = -x
	float dx = -x;

	//estimate the time constant to use to solve for y using newton's method
	float t = x; //guess the time constant at the provided x value
	int i=0;
	while (i <= MAX_ITERATION)
	{
		float n;
		float ddx = cubeDerivX(t, ax, bx, cx);
		if (ddx == 0)
			n = 0;
		else
			n = t - (cubeX(t, ax, bx, cx, dx)/ddx);

		//end if the solved value is within the tolerance
		if (fabsf(n-t) < TOLERANCE)
			break;

		t = n;
		i++;
	}

	//make sure the time constant is within the range 0.0-1.0
	if (t > 1)
		t = 1;
	else if (t < 0)
		t = -t;

	//calculate the coefficients to use when solving the y cubic poly
	//cy = 3(y1-y0)
	float cy = 3.0f*(point1.y-point0.y);
	//by = 3(y2-y2)-cy
	float by = 3.0f*(point2.y-point1.y)-cy;
	//ay = y3 - y0 - cy - by
	float ay = point3.y - point0.y - cy - by;

	//finally solve the curve for y at the estimated t
	float y = ay*(t*t*t) + by*(t*t) + cy*t + point0.y;

	//make sure the y is within the range 0.0-1.0
	if (y > 1)
		y = 1;
	else if (y < 0)
		y = 0;
	return y;
}
Esempio n. 4
0
HexagonGrid::HexagonGrid()
{
    // Creating 200x200 hexagonal map
    unsigned int index = 0;
    for (unsigned int q = 0; q != 200; ++q)
    {
        for (unsigned int p = 0; p != 200; ++p, ++index)
        {
            auto hexagon = new Hexagon(index);
            int x = 48*100 + 16*(q+1) - 24*p;
            int y = (q+1)*12 + 6*p + 12;
            if (p&1)
            {
                x -= 8;
                y -= 6;
            }

            hexagon->setCubeX(q - (p + (p&1))/2);
            hexagon->setCubeZ(p);
            hexagon->setCubeY(-hexagon->cubeX() - hexagon->cubeZ());

            hexagon->setX(x);
            hexagon->setY(y);
            _hexagons.push_back(hexagon);
        }
    }

    // Creating links between hexagons
    for (index = 0; index != 200*200; ++index)
    {
        auto hexagon = _hexagons.at(index);

        unsigned int q = index/200; // hexagonal y
        unsigned int p = index%200; // hexagonal x

        unsigned index1 = (q + 1)*200 + p;
        unsigned index4 = (q-1)*200 + p;
        unsigned int index2, index3, index5, index6;
        if (index&1)
        {
            index2 = q*200 + p-1;
            index3 = (q-1)*200 + p-1;
            index5 = (q-1)*200 + p+1;
            index6 = q*200 + p+1;
        }
        else
        {
            index2 = (q+1)*200 + p-1;
            index3 = q*200 + p-1;
            index5 = q*200 + p+1;
            index6 = (q+1)*200 + p+1;
        }

        if (index1 < _hexagons.size()) hexagon->neighbors()->push_back(_hexagons.at(index1));
        if (index2 < _hexagons.size()) hexagon->neighbors()->push_back(_hexagons.at(index2));
        if (index3 < _hexagons.size()) hexagon->neighbors()->push_back(_hexagons.at(index3));
        if (index4 < _hexagons.size()) hexagon->neighbors()->push_back(_hexagons.at(index4));
        if (index5 < _hexagons.size()) hexagon->neighbors()->push_back(_hexagons.at(index5));
        if (index6 < _hexagons.size()) hexagon->neighbors()->push_back(_hexagons.at(index6));
    }
}