예제 #1
0
int main()
{
	const char* string = (char *)malloc(50);
	const char* sub = (char *)malloc(19);
	int a = 8; float b = 9.4444; int c = 2;
	int d;
	printf("Before swapping: a = %d c = %d\n", a , c);
	a = a + c;
	c = a - c;
	a = a - c;
	printf("After swapping: a = %d c = %d\n", a , c);
	string = "This is the company namded apple inc";
	sub = "is";
	printf("Substring: %d\n", isSubstring(string, sub));
	printf("Position of the MSB is: %d\n", msbPos(3));
	printf("Is bit set: %d\n", isBitset(8,4));
	printf("%x to little endian %x\n", a, BigToLittle(a));
	printf("%d is a power of 2: %d\n", a ,isPowerof2(a));
	printf("%s after reversed: %s\n", string, reverse(string));
	printf("%s is palindrome: %d\n", string, isPalindrome(string));
	printf("%d is a prime number: %d\n",a, isPrimeNumber(a));
	primeNumber(20);
	printf("\nFibonacci Series: ");
	fibonacci(15);
	printf("\n");
	printf("Factorial of %d: %d\n", a, factorial(a));
	printf("%s after reversing: %s\n", string, reverseString(string));
	printf("Nearest int of %f is: %d\n", b, nearestInt(b));
	printf("%d", (-1%8));
	return 0;
}
static void displayCharts(double npoints)
{

	for (int y = 0; y < 511; y++)
		for (int x = 0; x < 511; x++) {
			float f = int_buff[y][x] * 0.2f;
			vfb[y][x] = Color(f, f, f);
	}
	for (int y = 0; y < 511; y++)
		for (int x = 0; x < 512; x++) {
			float f = float_buff[y][x] * 0.2f;
			vfb[y][x+512] = Color(f, f, f);
		}
	for (int y = 0; y < 512; y++)
		for (int x = 0; x < 511; x++) {
			float f = circle_buff[y][x] * 0.2f;
			vfb[y+512][x] = Color(f, f, f);
		}
	const int BORDERS = 16;
	const int NLINES = 8;
	const int SPACING = (512 - 2 * BORDERS) / NLINES;
	double mult = (512 - 2 * BORDERS) * 512 * 0.4 / (npoints);
	for (int x = 0; x < 512; x++) {
		Color base = Color(0, 0, 0);
		if ((int) fabs(x - 256) % ((int) (256.0/3.0)) == 0)
			base = Color(0.2f, 0.2f, 0.2f);
		int ey = nearestInt(512 - BORDERS - norm_buff[x] * mult);
		int sy = 512 - BORDERS;
		for (int y = 0; y < 512; y++) {
			Color f = base;
			if (sy - y >= 0 && (sy - y) % SPACING == 0) f += Color(0.1f, 0.1f, 0.1f);
			if (y == ey) f = Color(0.4f, 0.4f, 1.0f);
			else if (y == sy) f = Color(0.9f, 0.9f, 0.9f);
			else if (y > sy) f.makeZero();
			else if (y > ey) f += Color(0.2f, 0.2f, 0.5f);
			vfb[y+512][x+512] = f;
		}
	}
	for (int i = 0; i < 1024; i++)
		vfb[i][511] = vfb[511][i] = Color(1, 1, 1);
}
예제 #3
0
void Heightfield::fillProperties(ParsedBlock& pb)
{
    Bitmap* bmp = NULL;
    if (!pb.getBitmapFileProp("file", &bmp, filename)) pb.requiredProp("file");
    W = bmp->getWidth();
    H = bmp->getHeight();
    blur = 0;
    pb.getDoubleProp("blur", &blur);
    heights = new float[W * H];
    float minY = LARGE_FLOAT, maxY = -LARGE_FLOAT;
    // do we have blur? if no, just fetch the source image and store it:
    if (blur <= 0) {
        for (int y = 0; y < H; y++)
            for (int x = 0; x < W; x++) {
                float h = bmp->getPixel(x, y).intensity();
                heights[y * W + x] = h;
                minY = min(minY, h);
                maxY = max(maxY, h);
            }
    } else {
        // We have blur...
        // 1) convert image to greyscale (if not already):
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                float f = bmp->getPixel(x, y).intensity();
                bmp->setPixel(x, y, Color(f, f, f));
            }
        }
        // 2) calculate the gaussian coefficients, see http://en.wikipedia.org/wiki/Gaussian_blur
        static float gauss[128][128];
        int R = min(128, nearestInt(float(3 * blur)));
        for (int y = 0; y < R; y++)
            for (int x = 0; x < R; x++)
                gauss[y][x] = float(exp(-(sqr(x) + sqr(y))/(2 * sqr(blur))) / (2 * PI * sqr(blur)));
        // 3) apply gaussian blur with the specified number of blur units:
        // (this is potentially slow for large blur radii)
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                float sum = 0;
                for (int dy = -R + 1; dy < R; dy++)
                    for (int dx = -R + 1; dx < R; dx++)
                        sum += gauss[abs(dy)][abs(dx)] * bmp->getPixel(x + dx, y + dy).r;
                heights[y * W + x] = sum;
                minY = min(minY, sum);
                maxY = max(maxY, sum);
            }
        }
    }
    // set the bounding box. minY and maxY are the bbox extents along Y and are calculated
    // from the heights[] array.
    bbox.vmin = Vector(0, minY, 0);
    bbox.vmax = Vector(W, maxY, H);

    // calculate the maxH array. maxH(x, y) = max(H(x, y), H(x + 1, y), H(x, y + 1), H(x + 1, y+1))
    maxH = new float[W*H];
    for (int y = 0; y < H; y++)
        for (int x = 0; x < W; x++) {
            float& maxH = this->maxH[y * W + x];
            maxH = heights[y * W + x];
            if (x < W - 1) maxH = max(maxH, heights[y * W + x + 1]);
            if (y < H - 1) {
                maxH = max(maxH, heights[(y + 1) * W + x]);
                if (x < W - 1)
                    maxH = max(maxH, heights[(y + 1) * W + x + 1]);
            }
        }
    // precalculate the normals at each integer point. We create normals by doing two forward differences
    // on the height along x and y at each point and using the cross product of these differences to
    // obtain the normal vector
    normals = new Vector[W * H];
    for (int y = 0; y < H; y++)
        for (int x = 0; x < W; x++) {
            float h0 = heights[y * W + x];
            float hdx = heights[y * W + min(W - 1, x + 1)];
            float hdy = heights[min(H - 1, y + 1) * W + x];
            Vector vdx = Vector(1, hdx - h0, 0); // forward difference along X
            Vector vdy = Vector(0, hdy - h0, 1); // forward difference along Z
            Vector norm = vdy ^ vdx;
            norm.normalize();
            normals[y * W + x] = norm;
        }
    useOptimization = false;
    pb.getBoolProp("useOptimization", &useOptimization);
    if (!disableAcceleratedStructures && useOptimization) {
        Uint32 clk = SDL_GetTicks();
        buildStruct();
        clk = SDL_GetTicks() - clk;
        printf("Heightfield acceleration struct built in %.3lfs\n", clk / 1000.0);
    }
}