| 1 | # MATHC |
| 2 | |
| 3 | [](https://travis-ci.org/ferreiradaselva/mathc) |
| 4 | |
| 5 | MATHC (version 2) is a simple math library for 2D and 3D programming. It contains implementations for: |
| 6 | |
| 7 | - Vectors (2D, 3D and 4D) |
| 8 | - Quaternions |
| 9 | - Matrices (2×2, 3×3, and 4×4) |
| 10 | - Easing functions |
| 11 | |
| 12 | ## Configuring |
| 13 | |
| 14 | MATHC can be configured with the following preprocessors (described in the following sections of this document): |
| 15 | |
| 16 | - `MATHC_NO_STDBOOL` |
| 17 | - `mfloat_t` |
| 18 | - `MATHC_DOUBLE_PRECISION` |
| 19 | - `MATHC_NO_STRUCTURES` |
| 20 | - `MATHC_NO_POINTER_STRUCT_FUNCTIONS` |
| 21 | - `MATHC_NO_STRUCT_FUNCTIONS` |
| 22 | - `MATHC_NO_EASING_FUNCTIONS` |
| 23 | |
| 24 | You can define these macros during compilation time with flags: |
| 25 | |
| 26 | ``` |
| 27 | gcc -DMATHC_NO_STDBOOL=ON -Dmfloat_t=double -DMATHC_DOUBLE_PRECISION=ON |
| 28 | ``` |
| 29 | |
| 30 | Or include `mathc.c` in a source file. This second approach is more useful and flexible, because you can define `mfloat_t` as a different type other than the built-in types `float` or `double`, such as `GLfloat`: |
| 31 | |
| 32 | ```c |
| 33 | /* In a *.c file */ |
| 34 | #include <GL.h> |
| 35 | #define mfloat_t GLfloat |
| 36 | #define MATHC_DOUBLE_PRECISION |
| 37 | #include <mathc.c> |
| 38 | ``` |
| 39 | |
| 40 | ## Integer Type |
| 41 | |
| 42 | By default, `mint_t` is a `int32_t` if the header `stdint.h` is available. If the header `stdint.h` is not avaliable, disabled by defining `MATHC_NO_STDINT`, `mint_t` is a `int`. This can be changed by predefining `mint_t` as a desired type. |
| 43 | |
| 44 | ## Float-Point Type |
| 45 | |
| 46 | By default, `mfloat_t` is a `float`. This can be changed by predefining `mfloat_t` as a desired type. |
| 47 | |
| 48 | ## Math Precision |
| 49 | |
| 50 | By default, MATHC will use single-precision internally. This can be changed by predefining `MATHC_DOUBLE_PRECISION`. |
| 51 | |
| 52 | ## Types |
| 53 | |
| 54 | By default, types are can be declared as `mfloat_t` arrays or `mint_t` arrays, or structures: |
| 55 | |
| 56 | ```c |
| 57 | /* As float arrays */ |
| 58 | mfloat_t texture_coordinates[VEC2_SIZE]; |
| 59 | mfloat_t position[VEC3_SIZE]; |
| 60 | mfloat_t rgba[VEC4_SIZE]; |
| 61 | mfloat_t rotation[QUAT_SIZE]; |
| 62 | mfloat_t rotation_mat[MAT3_SIZE]; |
| 63 | mfloat_t model_mat[MAT4_SIZE]; |
| 64 | |
| 65 | /* As float structures */ |
| 66 | struct vec2 texture_coordinates; |
| 67 | struct vec3 position; |
| 68 | struct vec4 rgba; |
| 69 | struct quat rotation; |
| 70 | struct mat3 rotation_mat; |
| 71 | struct mat4 model_mat; |
| 72 | |
| 73 | /* As integer arrays */ |
| 74 | mint_t texture_coordinates[VEC2_SIZE]; |
| 75 | mint_t position[VEC3_SIZE]; |
| 76 | mint_t rgba[VEC4_SIZE]; |
| 77 | |
| 78 | /* As integer structures */ |
| 79 | struct vec2i texture_coordinates; |
| 80 | struct vec3i position; |
| 81 | struct vec4i rgba; |
| 82 | ``` |
| 83 | |
| 84 | By defining `MATHC_NO_STRUCTURES`, structure types will not defined. |
| 85 | |
| 86 | ## Functions |
| 87 | |
| 88 | By default, MATHC will declare functions that take `mfloat_t` array or `mint_t` array, structure values, and structure pointers as arguments: |
| 89 | |
| 90 | ```c |
| 91 | /* As array */ |
| 92 | mfloat_t position[VEC3_SIZE]; |
| 93 | mfloat_t offset[VEC3_SIZE]; |
| 94 | |
| 95 | vec2_add(position, |
| 96 | vec2(position, 0.0f, 0.0f), |
| 97 | vec2(offset, 1.0f, 1.0f)); |
| 98 | |
| 99 | /* As structures */ |
| 100 | struct vec2 position = svec2(0.0f, 0.0f); |
| 101 | struct vec2 offset = svec2(1.0f, 1.0f); |
| 102 | |
| 103 | /* As structure value */ |
| 104 | position = svec2_add(position, offset); |
| 105 | /* As structure pointer */ |
| 106 | psvec2_add(&position, &position, &offset); |
| 107 | ``` |
| 108 | |
| 109 | Functions that take structure as value have a prefix `s`. Functions that take structure pointers have a prefix `ps`. |
| 110 | |
| 111 | By defining `MATHC_NO_STRUCT_FUNCTIONS`, functions that take structure as value will not be defined. |
| 112 | |
| 113 | By defining `MATHC_NO_POINTER_STRUCT_FUNCTIONS`, functions that take structure pointers will not be defined. |
| 114 | |
| 115 | ## Easing Functions |
| 116 | |
| 117 | The easing functions are an implementation of the functions presented in [easings.net](http://easings.net/), useful particularly for animations. |
| 118 | |
| 119 | Easing functions take a value inside the range `0.0-1.0` and usually will return a value inside that same range. However, in some of the easing functions, the returned value extrapolate that range (Check the [easings.net](http://easings.net/) to see those functions). |
| 120 | |
| 121 | By defining `MATHC_NO_EASING_FUNCTIONS`, the easing functions will not be defined. |
| 122 | |
| 123 | ## LICENSE |
| 124 | |
| 125 | The source code of this project is licensed under the terms of the ZLIB license: |
| 126 | |
| 127 | Copyright (C) 2016 Felipe Ferreira da Silva |
| 128 | |
| 129 | This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. |
| 130 | |
| 131 | Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: |
| 132 | |
| 133 | 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. |
| 134 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
| 135 | 3. This notice may not be removed or altered from any source distribution. |