Programming

C

Read heightgrid from keyboard input and convert it to an .obj file (3D model file) (.c file)

// txt_to_obj.c - reads heightgrid from keyboard input and converts it to an .obj file (3D model file)
// heightgrid values must be between 0 and 9
// input format:
// 43284198
// 57435643
// 23423453
// 23121433.
// max size of terrain is now 64x64, you can change that in the variables below

#include <stdio.h>

int main()
{
	char c;
	int i = 0;
	int j = 0;
	int vertexno[64][64];	// max size of terrain (64x64)
	int total = 0;
	int last_total = 0;
	int tmpsecond = 0;
	int rowstart[64];	// this must be same as max size of terrain (64)

	rowstart[0] = 0;

	c = getchar();
	while (c != '.')	// end terrain input with a dot ('.')
	{
		if (c >= '0' && c <= '9')
		{
			// v (VERTICES) - '/10' means heightvalues gets divided by 10 as they get into the .obj file. change this value to get a steeper or flatter model
			if (total == 0) printf("v %i %i %f\n", i, j, ((float)(c-'0'))/10);			// only on first face on first row
			if (i >= 1 && j == 0) printf("v %i %i %f\n", i+1, j, ((float)(c-'0'))/10);		// special case for first face on rest of the rows
			else if (i == 0) printf("v %i %i %f\n", i, j+1, ((float)(c-'0'))/10);			// only on rest of the faces on first row
			printf("v %i %i %f\n", i+1, j+1, ((float)(c-'0'))/10);
			if (total == 0) printf("v %i %i %f\n", i+1, j, ((float)(c-'0'))/10);			// only on first face on first row

			// f (FACES)
			if (total == 0) printf("f %i %i %i %i\n", total+4, total+3, total+2, total+1);										// first face on first row
			else if (total == 4) printf("f %i %i %i %i\n", last_total+3, total+2, total+1, last_total+2);								// second face on first row
			else if (i == 1 && j == 0) printf("f 3 4 %i %i\n", last_total+3, last_total+4);										// first face on second row
			else if (i >= 2 && j == 0) printf("f %i %i %i %i\n", rowstart[i-1]+2, rowstart[i-1]+1, last_total+2, last_total+3);					// first face on rest of the rows
			else if (i == 1 && j == 1) { printf("f 3 %i %i 6\n", last_total+2, last_total+3); tmpsecond = 6; }							// second face on second row
			else if (i >= 2 && j == 1) { printf("f %i %i %i %i\n", rowstart[i-1]+2, last_total+2, last_total+3, rowstart[i-1]+3); tmpsecond = rowstart[i-1]+3; }	// second face on rest of the rows
			else if (i == 1 && j >= 2) { printf("f %i %i %i %i\n", tmpsecond, last_total+1, last_total+2, tmpsecond+2); tmpsecond += 2; }				// rest of the faces on second row
			else if (i >= 2 && j >= 2) { printf("f %i %i %i %i\n", tmpsecond, last_total+1, last_total+2, tmpsecond+1); tmpsecond++; }				// rest of the faces on rest of the rows
			else printf("f %i %i %i %i\n", last_total+2, total+2, total+1, last_total+1);										// rest of the faces on first row

			last_total = total;
			vertexno[i][j] = total;
			if (total == 0) total += 4;
			else if (i == 0 || (i >= 1 && j == 0)) total += 2;
			else total++;
			j++;
		}
		if (c == '\n')
		{
			printf("\n\n");
			i++;
			j = 0;
			rowstart[i] = total;
		}
		c = getchar();
	}

	return 0;
}

Javascript

Get an interpolated value in a triangle (code to put in a Javascript script):

// (v1, v2 and v3 are values for the three edges)
// (x_v1, y_v1, x_v2, y_v2, x_v3 and y_v3 are x and y coordinates for the three edges)
// (x_p and y_p are x and y coordinates for point p)
// (... and we wanna calculate the value for point p, from only knowing the value for the three edges v1, v2 and v3...)

// step 1: calculate "weights" from edges v1, v2 and v3
w1 = ((y_v2-y_v3)*(x_p-x_v3) + (x_v3-x_v2)*(y_p-y_v3)) / ((y_v2-y_v3)*(x_v1-x_v3) + (x_v3-x_v2)*(y_v1-y_v3));
w2 = ((y_v3-y_v1)*(x_p-x_v3) + (x_v1-x_v3)*(y_p-y_v3)) / ((y_v2-y_v3)*(x_v1-x_v3) + (x_v3-x_v2)*(y_v1-y_v3));
w2 = 1-w1-w2;

// step 2: calculate interpolated value for point p from "weights"
p = (w1*v1 + w2*v2 + w3*v3) / (w1+w2+w3);