- Jul 29, 2018
- 7
- 1
When creating a map, one crucial design-aspect is its total size and available space for players. Therefore, being able to calculate distances correctly is an essential skill, since different distances can affect fall-damage and accessibility. Since working in three dimensions can be challenging, I have created a C program that allows you to input distances as vectors. This program will then calculate the distance between these two vectors. While map-making would use three dimensions at most, this program can handle vectors of any size (including 10 or 100 dimensions, if one so chooses) with any real coefficients. To run this code on any Windows machine, simply download the executable and run through Command Prompt. Otherwise, I have included the source-code below so that Mac and Linux users can compile it themselves. When run, you will be asked how many dimensions you are working in; this will affect the number of inputs for the two vectors. Once you have specified your dimensions (two or three for map-making), you will be prompted to type-in each vector's values. Simply type-in each individual number, followed by "Enter", and the program will take-care of the rest. Here is the source code for my non-Windows mates:
#include <stdio.h>
#include <math.h>
int main() {
int size, i, j, k, L;
printf("\nHow many dimensions are you working in: ");
scanf("%i", &size);
double A[size], B[size], C[size], total;
total = 0;
printf("\nInsert the first vector's values:\n");
for (i=1;i<=size;i++) {
scanf("%lf", &A[i-1]);
}
printf("\nInsert the second vector's values:\n");
for (j=1;j<=size;j++) {
scanf("%lf", &B[j-1]);
}
for (k=1;k<=size;k++) {
C[k-1] = A[k-1]-B[k-1];
}
for (L=1;L<=size;L++) {
total += pow(C[L-1], 2);
}
total = sqrt(total);
printf("\nThis is the distance between the two vectors: %lf", total);
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
int size, i, j, k, L;
printf("\nHow many dimensions are you working in: ");
scanf("%i", &size);
double A[size], B[size], C[size], total;
total = 0;
printf("\nInsert the first vector's values:\n");
for (i=1;i<=size;i++) {
scanf("%lf", &A[i-1]);
}
printf("\nInsert the second vector's values:\n");
for (j=1;j<=size;j++) {
scanf("%lf", &B[j-1]);
}
for (k=1;k<=size;k++) {
C[k-1] = A[k-1]-B[k-1];
}
for (L=1;L<=size;L++) {
total += pow(C[L-1], 2);
}
total = sqrt(total);
printf("\nThis is the distance between the two vectors: %lf", total);
return 0;
}