Quick actions

cmd+k|ctrl+k

Navigation

Languages

vector

Snippet info

Language

Cpp

Visibility

public

Author

evine

Created

2016-05-21T21:32:32Z

Updated

2016-05-21T21:48:26Z

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <math.h>

#include "misc.h"

s8 *vec_array[] = {"x","y","z","u","v","w"};

void vector_normal_operator(s8 *operator_string, s8 *type, s32 size)
{
    forI(i, size)
    {
        printf("internal v%i%s operator %s(v%i%s a,v%i%s b)\n", i+1, type, operator_string, i+1, type, i+1, type);
        printf("{\n");
        printf("\treturn v%i%s{", i+1, type);
        for(s32 j = 0;; ++j)
        {
            printf("CAST(%s, a[%i] %s b[%i])", type, j, operator_string, j);
            if(j + 1 > i) break;
            printf(", ");
        }
        printf("};\n");
        printf("}\n");
    }
}

void vector_reference_operator(s8 *operator_string, s8 *type, s32 size)
{
    forI(i, size)
    {
        printf("internal void operator %s(v%i%s& a, v%i%s b)\n", operator_string, i+1, type, i+1, type);
        printf("{ a = a + b; }\n");
    }
}

void vector_create(s8 *type, s8 *type_print, s32 size)
{
    // structs
    forI(i, size)
    {
        printf("union v%i%s{\n",i + 1, type);
        printf("\tstruct{\n");
        for(s32 j = 0; j < i+1; ++j)
        {
            printf("\t\t%s %s;\n", type, vec_array[j]);
        }
        printf("\t};\n");
        printf("\t%s element_[%i];\n", type, i+1);
        printf("\t%s& operator [](s32 i)", type);
        printf("\t{\n");
        printf("\t\treturn this->element_[i];\n");
        printf("\t}\n");
        printf("};\n");
    }
    
    // Operator Overloading
    vector_normal_operator("+", type, size);
    vector_normal_operator("-", type, size);
    vector_normal_operator("*", type, size);
    vector_normal_operator("/", type, size);
    
    vector_reference_operator("+=", type, size);
    vector_reference_operator("-=", type, size);
    vector_reference_operator("*=", type, size);
    vector_reference_operator("/=", type, size);
    
    forI(i, size)
    {
        printf("internal v%i%s operator -(v%i%s a)\n", i+1, type, i+1, type);
        printf("{\n");
        printf("\treturn v%i%s{", i+1, type);
        for(s32 j = 0;; ++j)
        {
            printf("CAST(%s, -a[%i])", type, j);
            if(j + 1 > i) break;
            printf(", ");
        }
        printf("};\n");
        printf("}\n");
    }
    
    // va_print
    forI(i, size)
    {
        printf("internal void va_print(v%i%s a)\n", i+1, type);
        printf("{\n");
        printf("\tprintf(\"v%i%s{", i+1, type);
        for(s32 j = 0;; ++j)
        {
            printf("%s", type_print);
            if(j + 1 > i) break;
            printf(", ");
        }
        printf("}\"");
        for(s32 j = 0;; ++j)
        {
            printf(", a[%i]",j);
            if(j + 1 > i) break;
        }
        printf(");\n");
        printf("}\n");
    }
}

void vector_create_float_function(s8 *type, s32 size)
{
    // v_length
    forI(i, size)
    {
        printf("internal %s v_length(v%i%s a)\n", type, i+1, type);
        printf("{\n");
        printf("\treturn CAST(%s, sqrt(",type); // Verify: need cast to type??
        for(s32 j = 0;; ++j)
        {
            printf("(a*a)[%i]", j);
            if(j + 1 > i) break;
            printf(" + ");
        }
        printf("));\n");
        printf("}\n");
    }
    // v_angle
    printf("internal %s v_angle(v2%s a)\n", type, type);
    printf("{\n");
    printf("\t%s Result = CAST(%s, atan2(a[0],a[1]));\n",type, type);
    printf("\treturn Result >= 0 ? Result : Result + (%s)TAU;\n", type);
    printf("}\n");
    
    // v2type_new
    printf("internal v2%s v2%s_new(%s angle, %s length)\n", type, type, type, type);
    printf("{\n");
    printf("\treturn v2%s{CAST(%s, cos(angle)) * length, CAST(%s, sin(angle)) * length};\n", type, type, type);
    printf("}\n");
    
    // v2_rotate
    printf("internal v2%s v2_rotate(v2%s a, %s rotation)\n", type, type, type);
    printf("{\n");
    printf("\treturn v2%s{CAST(%s, a[0] * sin(rotation)), CAST(%s, a[1] * cos(rotation))};\n", type, type, type);
    printf("}\n");
    
    // v_normal
    forI(i, size)
    {
        printf("internal v%i%s v_normal(v%i%s a)\n", i+1, type, i+1, type);
        printf("{\n");
        printf("\t%s l = v_length(a);\n", type);
        printf("\treturn v%i%s{", i+1, type);
        for(s32 j = 0;; ++j)
        {
            printf("a[%i] / l",j);
            if(j + 1 > i) break;
            printf(", ");
        }
        printf("};\n");
        printf("}\n");
    }
}

s32 main()
{
    printf("/*Vector Library*/\n");
    
    vector_create("s8", "%i", 6);
    vector_create("s16", "%i", 6);
    vector_create("s32", "%i", 6);
    vector_create("s64", "%lli", 6);
    
    vector_create("f32", "%g", 6);
    vector_create_float_function("f32", 6);
    vector_create("f64", "%g", 6);
    vector_create_float_function("f64", 6);
    return 0;
}
INFO