Implementing 3D Vector Operations in C Programming

Classified in Computers

Written on in English with a size of 4.19 KB

C Program for 3D Vector Algebra Implementation

This C program demonstrates the implementation of fundamental 3D vector operations, including addition, subtraction, scalar (dot) product, vector (cross) product, modulus calculation, and element swapping. It also includes functions for displaying vectors to the console and writing them to a file.

Header Files and Function Prototypes

#include <stdio.h>
#include <math.h>

void suma_resta(double [], double [], double [], char);
void vectorial(double [], double [], double []);
void swap(double []);
void pant_vector(double []);
void fich_vector(double []);

double escalar(double [], double []);
double modulo(double []);

Main Function Execution

The main function initializes two vectors, a and b, and demonstrates the usage of the implemented vector functions.

int main() {
  double a[3] = {1, 1, 1}, b[3] = {2, 2, 2}, x[3] = {1, 2, 3};
  double c[3];

  puts("Vector a[3].");
  pant_vector(a);
  puts("\nVector b[3].");
  pant_vector(b);

  puts("\nc = a + b (Vector Addition).");
  suma_resta(a, b, c, '+');
  pant_vector(c);
  fich_vector(c); // Output to file

  printf("\n|a| = %lf (Vector Modulus).\n", modulo(a));
  printf("a * b = %lf (Scalar Product).\n", escalar(a, b));

  vectorial(a, b, c);
  puts("\na x b (Vector Cross Product).");
  pant_vector(c);

  puts("\nVector x[3] before swap.");
  pant_vector(x);
  swap(x);
  puts("\nswap(x) result.");
  pant_vector(x);

  system("pause");
  return 0;
}

Vector Operation Function Definitions

1. Vector Addition and Subtraction (suma_resta)

This function performs element-wise addition or subtraction based on the operator character (+ or -).

void suma_resta(double a[3], double b[3], double c[3], char op) {
  int i;

  if (op == '+') {
    for (i = 0; i < 3; i++) {
      c[i] = a[i] + b[i];
    }
  } else if (op == '-') {
    for (i = 0; i < 3; i++) {
      c[i] = a[i] - b[i];
    }
  } else {
    puts("Incorrect operator provided.");
  }
}

2. Scalar (Dot) Product (escalar)

Calculates the dot product of two 3D vectors.

double escalar(double a[3], double b[3]) {
  int i;
  double sum = 0;

  for (i = 0; i < 3; i++) {
    sum += a[i] * b[i];
  }
  return sum;
}

3. Vector Modulus (modulo)

Calculates the magnitude (modulus) of a vector using the square root of the sum of squares of its components.

double modulo(double a[3]) {
  int i;
  double sum = 0;

  for (i = 0; i < 3; i++) {
    sum += a[i] * a[i];
  }
  return sqrt(sum); /* Equivalent to: return sqrt(escalar(a, a)); */
}

4. Vector (Cross) Product (vectorial)

Calculates the cross product of two 3D vectors, storing the result in vector c.

void vectorial(double a[3], double b[3], double c[3]) {
  c[0] = a[1] * b[2] - a[2] * b[1];
  c[1] = a[2] * b[0] - a[0] * b[2];
  c[2] = a[0] * b[1] - a[1] * b[0];
}

5. Vector Element Swapping (swap)

Performs a specific cyclic permutation (swap) of the vector elements: a[0] moves to a[1], a[1] moves to a[2], and a[2] moves to a[0].

void swap(double a[3]) {
  double aux[3];
  int i;

  aux[0] = a[2];
  aux[1] = a[0];
  aux[2] = a[1];

  for (i = 0; i < 3; i++) {
    a[i] = aux[i];
  }
}

Utility Functions: Display and File Output

1. Display Vector to Console (pant_vector)

void pant_vector(double v[3]) {
  int i;

  for (i = 0; i < 3; i++) {
    printf("v[%d] = %lf.\n", i, v[i]);
  }
}

2. Write Vector to File (fich_vector)

Writes the vector components to a file named Vector.txt.

void fich_vector(double v[3]) {
  FILE *fich;
  int i;

  if ((fich = fopen("Vector.txt", "w")) != NULL) {
    for (i = 0; i < 3; i++) {
      fprintf(fich, "v[%d] = %lf.\n", i, v[i]);
    }
    fclose(fich);
  } else {
    puts("Error opening the file.");
  }
}

Related entries: