summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-12-09 23:16:29 +0100
committerNao Pross <naopross@thearcway.org>2018-12-09 23:16:29 +0100
commit01b6c3bb6b7e18136b573b8e834693a93c180496 (patch)
tree65b5fca4d2d2b484866abea7e8a2441f51cb91e8
parentAdd vector example documentation (diff)
downloadcplusplus-01b6c3bb6b7e18136b573b8e834693a93c180496.tar.gz
cplusplus-01b6c3bb6b7e18136b573b8e834693a93c180496.zip
Update vector ex. makefile and add vec3_mag function
-rw-r--r--vector/makefile8
-rw-r--r--vector/vector.c9
2 files changed, 14 insertions, 3 deletions
diff --git a/vector/makefile b/vector/makefile
index 6ce8fd9..ad56656 100644
--- a/vector/makefile
+++ b/vector/makefile
@@ -1,17 +1,21 @@
CARGS := -Wall -Werror -I.
+LDARGS := -lm
CPPARGS := -Wall -Werror -I.
-all: cpp-vector.pdf c_build/vectors
+all: vector.pdf c_build/vector
%.pdf: %.md
pandoc \
+ --from=markdown+raw_tex \
--latex-engine=xelatex \
+ --number-sections \
--filter pandoc-include-code \
+ --highlight-style tango \
$< -o $@
c_build/%: %.c
mkdir -p c_build
- gcc $(CARGS) $< -o $@
+ gcc $(CARGS) $< -o $@ $(LDARGS)
cpp_build/%: %.cpp
mkdir -p cpp_build
diff --git a/vector/vector.c b/vector/vector.c
index c8a5906..59f48cc 100644
--- a/vector/vector.c
+++ b/vector/vector.c
@@ -1,5 +1,6 @@
#include <stddef.h>
#include <stdio.h>
+#include <math.h>
struct vec3 {
double x;
@@ -64,12 +65,16 @@ struct vec3 vec3_cross(struct vec3 * const v, struct vec3 * const u)
return result;
}
+double vec3_mag(struct vec3 * const v) {
+ return sqrt((v->x * v->x) + (v->y * v->y) + (v->z * v->z));
+}
+
void vec3_print(struct vec3 * const v)
{
if (v == NULL)
return;
- printf("<%.2f,%.2f,%.2f>\n", v->x, v->y, v->y);
+ printf("<%.2f,%.2f,%.2f>\n", v->x, v->y, v->z);
}
@@ -84,6 +89,8 @@ int main(int argc, char *argv[])
printf("b = ");
vec3_print(&b);
+ printf("||a|| = %f\n", vec3_mag(&a));
+
printf("sum a + b : ");
res = vec3_add(&a, &b);
vec3_print(&res);