Created
October 3, 2020 11:49
-
-
Save harieamjari/aca7025ac615120a6bf65ecf789d14f8 to your computer and use it in GitHub Desktop.
Matrix multiplication of a vertex of a cube.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#define _USE_MATH_DEFINES | |
struct vector { | |
double x, z, y; | |
}; | |
struct vector *rot_x(const struct vector *_matrix, const double d_angle){ | |
//printf("sizeof _matrix: %ld\n", sizeof(*_matrix)); | |
struct vector *_object = malloc(sizeof(struct vector)*sizeof(*_matrix)); | |
if (_object == NULL) { | |
fprintf(stderr, "couldn't allocate enough space\n"); | |
exit(1); | |
} | |
for (int i = 0; i < (int)sizeof(*_matrix)/3; i++){ | |
_object[i].x = _matrix[i].x; | |
_object[i].y = (_matrix[i].y*cos(d_angle*M_PI/180.0))+(-_matrix[i].z*sin(d_angle*M_PI/180.0)); | |
_object[i].z = (_matrix[i].y*sin(d_angle*M_PI/180.0))+(_matrix[i].z*cos(d_angle*M_PI/180.0)); | |
} | |
return _object; | |
} | |
struct vector *rot_z(const struct vector *_matrix, const double d_angle){ | |
struct vector *_object = malloc(sizeof(struct vector)*sizeof(*_matrix)); | |
if (_object == NULL) { | |
fprintf(stderr, "couldn't allocate enough space\n"); | |
exit(1); | |
} | |
for (int i = 0; i < (int)(sizeof(*_matrix))/3; i++){ | |
_object[i].x = (_matrix[i].x*cos(d_angle*M_PI/180.0))+(-_matrix[i].z*sin(d_angle*M_PI/180.0)); | |
_object[i].y = (_matrix[i].x*sin(d_angle*M_PI/180.0))+(_matrix[i].z*cos(d_angle*M_PI/180.0)); | |
_object[i].z = _matrix[i].z; | |
} | |
return _object; | |
} | |
int print_transformation(const struct vector *_matrix){ | |
for (int i = 0; i < sizeof(*_matrix)/3; i++) | |
printf("%10.4f ", _matrix[i].x); | |
putchar('\n'); | |
for (int i = 0; i < sizeof(*_matrix)/3; i++) | |
printf("%10.4f ", _matrix[i].y); | |
putchar('\n'); | |
for (int i = 0; i < sizeof(*_matrix)/3; i++) | |
printf("%10.4f ", _matrix[i].z); | |
putchar('\n'); | |
return 0; | |
} | |
int main(int argc, char *argv[]){ | |
if (argc != 2) return 1; | |
const struct vector cube[8] = { | |
{8.0, 0.0, 5.0}, // vertex A | |
{8.0, 2.0, 5.0}, // vertex B | |
{-8.0, 2.0, 5.0}, // vertex C | |
{-8.0, 0.0, 5.0}, // vertex D | |
{8.0, 0.0, 8.0}, // vertex E | |
{8.0, 2.0, 8.0}, // vertex F | |
{-8.0, 2.0, 8.0}, // vertex G | |
{-8.0, 0.0, 8.0} // vertex H | |
}; | |
struct vector *rot_x_cube; | |
int d_eye = atoi(argv[1]); | |
/* | |
puts("Before transformation:"); | |
print_transformation(cube); | |
putchar('\n'); | |
puts("After transformation: "); | |
print_transformation(rot_y_cube); | |
*/ | |
/* | |
for (int i = 0; i < sizeof(cube)/3) | |
free(rot_y_cube); | |
for (int i = 0; i < sizeof(cube)/3; i++) | |
printf("%d ", closes_x[i]); | |
putchar('\n'); | |
for (int i = 0; i < sizeof(cube)/3; i++) | |
printf("%d ", closes_y[i]); | |
*/ | |
for (int i = 0; i < 120; i++){ | |
char execute[500]; | |
rot_x_cube = rot_z(cube, (double) (i*90/120)); | |
FILE *fp = fopen("file.dat", "w"); | |
if (fp == NULL){ | |
fprintf(stderr, "couldn't open file.dat\n"); | |
exit(1); | |
} | |
for (int c = 0; c < (int) (sizeof(cube)/(sizeof(double)*3)); c++){ | |
fprintf(fp, "0 0 %10f %10f\n", rot_x_cube[c].x/rot_x_cube[c].y, rot_x_cube[c].z/rot_x_cube[c].y); | |
} | |
fclose(fp); | |
sprintf(execute, "gnuplot -e \"set terminal png; plot [-5:5] [0:10]\'file.dat\' using 1:2:3:4 with vectors\" > \"/mnt/c/Users/User/Desktop/ffd/plot-%d.png\"\n", i); | |
printf(execute); | |
system(execute); | |
free(rot_x_cube); | |
} | |
/* | |
printf("length of cube %ld %d\n", sizeof(cube), (sizeof(cube)/3)); | |
for (int i = 0; i < (int )(sizeof(cube)/(sizeof(double)*3)); i++){ | |
printf("0 0 %10f %10f\n", rot_y_cube[i].x, rot_y_cube[i].y); | |
} | |
*/ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment