Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Last active January 26, 2021 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harieamjari/6da96fdf250d67ce49aa1cba2dc2f0a1 to your computer and use it in GitHub Desktop.
Save harieamjari/6da96fdf250d67ce49aa1cba2dc2f0a1 to your computer and use it in GitHub Desktop.
Project a 3D vector to a 2D plane.
/*
DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE
Version 1, October 2013
Copyright (c) 2020 Al-buharie Amjari <healer.harie@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified copies
of this license document, and changing it is allowed as long as the name
is changed.
DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE TERMS
AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION:
0. You just DO WHAT THE FUCK YOU WANT TO.
1. Do not hold the author(s), creator(s), developer(s) or distributor(s)
liable for anything that happens or goes wrong with your use of the work.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define _USE_MATH_DEFINES
struct vector {
double x, y, z;
};
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_y(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].y;
_object[i].z = (-_matrix[i].x*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].y*sin(d_angle*M_PI/180.0));
_object[i].y = (_matrix[i].x*sin(d_angle*M_PI/180.0))+(_matrix[i].y*cos(d_angle*M_PI/180.0));
_object[i].z = _matrix[i].z;
}
return _object;
}
struct vector *translate(const struct vector *_matrix, const double dx, const double dy, const double dz){
struct vector *_object = malloc(sizeof(struct vector)*sizeof(*_matrix));
if (_object == NULL) {
fprintf(stderr, "malloc failed\n");
exit(1);
}
for (int i = 0; i < (int)(sizeof(*_matrix)/3); i++){
_object[i].x = _matrix[i].x+dx;
_object[i].y = _matrix[i].y+dy;
_object[i].z = _matrix[i].z+dz;
}
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, -4.0, -5.0}, // vertex A
{8.0, 4.0, -5.0}, // vertex B
{-8.0, 4.0, -5.0}, // vertex C
{-8.0,-4.0, -5.0}, // vertex D
{8.0, -4.0, 5.0}, // vertex E
{8.0, 4.0, 5.0}, // vertex F
{-8.0, 4.0, 5.0}, // vertex G
{-8.0,-4.0, 5.0} // vertex H
};
struct vector *rot_x_cube = NULL, *rot_y_cube = NULL, *rot_z_cube = NULL, *translatedCube = NULL;
double 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 < 600; i++){
char execute[500];
rot_y_cube = rot_y(cube, (double) (i*300/120));
rot_z_cube = rot_z(rot_y_cube, (double) (i*300/120));
rot_x_cube = rot_x(rot_z_cube, (double) (i*300/120));
translatedCube = translate(rot_x_cube, 0, 0, 20);
free(rot_x_cube);
rot_x_cube = translatedCube;
FILE *fp = fopen("file.dat", "w");
if (fp == NULL){
fprintf(stderr, "couldn't open file.dat\n");
free(rot_x_cube);
exit(1);
}
fprintf(fp, "%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n"
"%10f %10f %10f %10f\n",
d_eye*rot_x_cube[1].x/rot_x_cube[1].z, d_eye*rot_x_cube[1].y/rot_x_cube[1].z,
d_eye*rot_x_cube[0].x/rot_x_cube[0].z, d_eye*rot_x_cube[0].y/rot_x_cube[0].z,
d_eye*rot_x_cube[2].x/rot_x_cube[2].z, d_eye*rot_x_cube[2].y/rot_x_cube[2].z,
d_eye*rot_x_cube[1].x/rot_x_cube[1].z, d_eye*rot_x_cube[1].y/rot_x_cube[1].z,
d_eye*rot_x_cube[3].x/rot_x_cube[3].z, d_eye*rot_x_cube[3].y/rot_x_cube[3].z,
d_eye*rot_x_cube[2].x/rot_x_cube[2].z, d_eye*rot_x_cube[2].y/rot_x_cube[2].z,
d_eye*rot_x_cube[0].x/rot_x_cube[0].z, d_eye*rot_x_cube[0].y/rot_x_cube[0].z,
d_eye*rot_x_cube[3].x/rot_x_cube[3].z, d_eye*rot_x_cube[3].y/rot_x_cube[3].z,
d_eye*rot_x_cube[5].x/rot_x_cube[5].z, d_eye*rot_x_cube[5].y/rot_x_cube[5].z,
d_eye*rot_x_cube[4].x/rot_x_cube[4].z, d_eye*rot_x_cube[4].y/rot_x_cube[4].z,
d_eye*rot_x_cube[6].x/rot_x_cube[6].z, d_eye*rot_x_cube[6].y/rot_x_cube[6].z,
d_eye*rot_x_cube[5].x/rot_x_cube[5].z, d_eye*rot_x_cube[5].y/rot_x_cube[5].z,
d_eye*rot_x_cube[7].x/rot_x_cube[7].z, d_eye*rot_x_cube[7].y/rot_x_cube[7].z,
d_eye*rot_x_cube[6].x/rot_x_cube[6].z, d_eye*rot_x_cube[6].y/rot_x_cube[6].z,
d_eye*rot_x_cube[4].x/rot_x_cube[4].z, d_eye*rot_x_cube[4].y/rot_x_cube[4].z,
d_eye*rot_x_cube[7].x/rot_x_cube[7].z, d_eye*rot_x_cube[7].y/rot_x_cube[7].z,
// begin line z axis
d_eye*rot_x_cube[0].x/rot_x_cube[0].z, d_eye*rot_x_cube[0].y/rot_x_cube[0].z,
d_eye*rot_x_cube[4].x/rot_x_cube[4].z, d_eye*rot_x_cube[4].y/rot_x_cube[4].z,
d_eye*rot_x_cube[1].x/rot_x_cube[1].z, d_eye*rot_x_cube[1].y/rot_x_cube[1].z,
d_eye*rot_x_cube[5].x/rot_x_cube[5].z, d_eye*rot_x_cube[5].y/rot_x_cube[5].z,
d_eye*rot_x_cube[2].x/rot_x_cube[2].z, d_eye*rot_x_cube[2].y/rot_x_cube[2].z,
d_eye*rot_x_cube[6].x/rot_x_cube[6].z, d_eye*rot_x_cube[6].y/rot_x_cube[6].z,
d_eye*rot_x_cube[3].x/rot_x_cube[3].z, d_eye*rot_x_cube[3].y/rot_x_cube[3].z,
d_eye*rot_x_cube[7].x/rot_x_cube[7].z, d_eye*rot_x_cube[7].y/rot_x_cube[7].z);
fclose(fp);
sprintf(execute, "gnuplot -e \"set terminal png; plot [-25:25] [-25:25]\'file.dat\' using 1:2:(\\$3-\\$1):(\\$4-\\$2) with vectors nohead\" > \"/mnt/c/Users/User/Desktop/ffd/plot-%d.png\"\n", i);
printf(execute);
system(execute);
//free(rot_x_cube);
free(rot_y_cube);
free(rot_z_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;
}
@harieamjari
Copy link
Author

ffmpeg -i plot-%d.png -r 24 -pix_fmt yuv420p ff.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment