aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fmt.c28
-rw-r--r--geometry.h10
-rw-r--r--mkfile2
-rw-r--r--point.c14
-rw-r--r--utils.c9
5 files changed, 56 insertions, 7 deletions
diff --git a/fmt.c b/fmt.c
new file mode 100644
index 0000000..359b4ba
--- /dev/null
+++ b/fmt.c
@@ -0,0 +1,28 @@
+#include <u.h>
+#include <libc.h>
+#include <geometry.h>
+
+int
+vfmt(Fmt *f)
+{
+ Point2 p;
+
+ p = va_arg(f->args, Point2);
+ return fmtprint(f, "[%g %g %g]", p.x, p.y, p.w);
+}
+
+int
+Vfmt(Fmt *f)
+{
+ Point3 p;
+
+ p = va_arg(f->args, Point3);
+ return fmtprint(f, "[%g %g %g %g]", p.x, p.y, p.z, p.w);
+}
+
+void
+GEOMfmtinstall(void)
+{
+ fmtinstall('v', vfmt);
+ fmtinstall('V', Vfmt);
+}
diff --git a/geometry.h b/geometry.h
index 0e4f748..7c273d1 100644
--- a/geometry.h
+++ b/geometry.h
@@ -35,6 +35,9 @@ struct Triangle3 {
Point3 p0, p1, p2;
};
+/* utils */
+double flerp(double, double, double);
+
/* Point2 */
Point2 Pt2(double, double, double);
Point2 Vec2(double, double);
@@ -108,3 +111,10 @@ Point3 invrframexform3(Point3, RFrame3);
/* Triangle3 */
Point3 centroid(Triangle3);
+
+/* Fmt */
+#pragma varargck type "v" Point2
+#pragma varargck type "V" Point3
+int vfmt(Fmt*);
+int Vfmt(Fmt*);
+void GEOMfmtinstall(void);
diff --git a/mkfile b/mkfile
index 85f7c76..1b91e7b 100644
--- a/mkfile
+++ b/mkfile
@@ -7,6 +7,8 @@ OFILES=\
quaternion.$O\
rframe.$O\
triangle.$O\
+ utils.$O\
+ fmt.$O\
HFILES=geometry.h
diff --git a/point.c b/point.c
index b580369..507bb7f 100644
--- a/point.c
+++ b/point.c
@@ -46,9 +46,9 @@ lerp2(Point2 a, Point2 b, double t)
if(t < 0) t = 0;
if(t > 1) t = 1;
return Pt2(
- (1 - t)*a.x + t*b.x,
- (1 - t)*a.y + t*b.y,
- (1 - t)*a.w + t*b.w
+ flerp(a.x, b.x, t),
+ flerp(a.y, b.y, t),
+ flerp(a.w, b.w, t)
);
}
@@ -119,10 +119,10 @@ lerp3(Point3 a, Point3 b, double t)
if(t < 0) t = 0;
if(t > 1) t = 1;
return Pt3(
- (1 - t)*a.x + t*b.x,
- (1 - t)*a.y + t*b.y,
- (1 - t)*a.z + t*b.z,
- (1 - t)*a.w + t*b.w
+ flerp(a.x, b.x, t),
+ flerp(a.y, b.y, t),
+ flerp(a.z, b.z, t),
+ flerp(a.w, b.w, t)
);
}
diff --git a/utils.c b/utils.c
new file mode 100644
index 0000000..54141e3
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,9 @@
+#include <u.h>
+#include <libc.h>
+#include <geometry.h>
+
+double
+flerp(double a, double b, double t)
+{
+ return a + (b - a)*t;
+}