Skip to content

Commit

Permalink
improve lerp code and add formatting procedures.
Browse files Browse the repository at this point in the history
  • Loading branch information
SametSisartenep committed Apr 28, 2020
1 parent 7208029 commit 78a31ca
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
28 changes: 28 additions & 0 deletions fmt.c
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
2 changes: 2 additions & 0 deletions mkfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ OFILES=\
quaternion.$O\
rframe.$O\
triangle.$O\
utils.$O\
fmt.$O\

HFILES=geometry.h

Expand Down
14 changes: 7 additions & 7 deletions point.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -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)
);
}

Expand Down
9 changes: 9 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 78a31ca

Please sign in to comment.