diff options
author | rodri <rgl@antares-labs.eu> | 2020-04-28 12:39:05 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2020-04-28 12:39:05 +0000 |
commit | 78a31caf9c482706040a38173c8e2d6913fa282e (patch) | |
tree | e25f2bee4adc2759150701234225404b45cdb043 | |
parent | 720802920c652ad1337264c31799fe35c5583551 (diff) | |
download | libgeometry-78a31caf9c482706040a38173c8e2d6913fa282e.tar.gz libgeometry-78a31caf9c482706040a38173c8e2d6913fa282e.tar.bz2 libgeometry-78a31caf9c482706040a38173c8e2d6913fa282e.zip |
improve lerp code and add formatting procedures.
-rw-r--r-- | fmt.c | 28 | ||||
-rw-r--r-- | geometry.h | 10 | ||||
-rw-r--r-- | mkfile | 2 | ||||
-rw-r--r-- | point.c | 14 | ||||
-rw-r--r-- | utils.c | 9 |
5 files changed, 56 insertions, 7 deletions
@@ -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); +} @@ -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); @@ -7,6 +7,8 @@ OFILES=\ quaternion.$O\ rframe.$O\ triangle.$O\ + utils.$O\ + fmt.$O\ HFILES=geometry.h @@ -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) ); } @@ -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; +} |