From 720802920c652ad1337264c31799fe35c5583551 Mon Sep 17 00:00:00 2001 From: rodri Date: Tue, 14 Apr 2020 19:53:51 +0000 Subject: struct filling corrections. --- point.c | 36 ++++++++++++++++++------------------ quaternion.c | 24 ++++++++++++------------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/point.c b/point.c index 4c0731d..b580369 100644 --- a/point.c +++ b/point.c @@ -19,25 +19,25 @@ Vec2(double x, double y) Point2 addpt2(Point2 a, Point2 b) { - return (Point2){a.x+b.x, a.y+b.y, a.w+b.w}; + return Pt2(a.x+b.x, a.y+b.y, a.w+b.w); } Point2 subpt2(Point2 a, Point2 b) { - return (Point2){a.x-b.x, a.y-b.y, a.w-b.w}; + return Pt2(a.x-b.x, a.y-b.y, a.w-b.w); } Point2 mulpt2(Point2 p, double s) { - return (Point2){p.x*s, p.y*s, p.w*s}; + return Pt2(p.x*s, p.y*s, p.w*s); } Point2 divpt2(Point2 p, double s) { - return (Point2){p.x/s, p.y/s, p.w/s}; + return Pt2(p.x/s, p.y/s, p.w/s); } Point2 @@ -45,11 +45,11 @@ lerp2(Point2 a, Point2 b, double t) { if(t < 0) t = 0; if(t > 1) t = 1; - return (Point2){ + return Pt2( (1 - t)*a.x + t*b.x, (1 - t)*a.y + t*b.y, (1 - t)*a.w + t*b.w - }; + ); } double @@ -71,8 +71,8 @@ normvec2(Point2 v) len = vec2len(v); if(len == 0) - return (Point2){0, 0, 0}; - return (Point2){v.x/len, v.y/len, 0}; + return Pt2(0,0,0); + return Pt2(v.x/len, v.y/len, 0); } /* 3D */ @@ -92,25 +92,25 @@ Vec3(double x, double y, double z) Point3 addpt3(Point3 a, Point3 b) { - return (Point3){a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w}; + return Pt3(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w); } Point3 subpt3(Point3 a, Point3 b) { - return (Point3){a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w}; + return Pt3(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w); } Point3 mulpt3(Point3 p, double s) { - return (Point3){p.x*s, p.y*s, p.z*s, p.w*s}; + return Pt3(p.x*s, p.y*s, p.z*s, p.w*s); } Point3 divpt3(Point3 p, double s) { - return (Point3){p.x/s, p.y/s, p.z/s, p.w/s}; + return Pt3(p.x/s, p.y/s, p.z/s, p.w/s); } Point3 @@ -118,12 +118,12 @@ lerp3(Point3 a, Point3 b, double t) { if(t < 0) t = 0; if(t > 1) t = 1; - return (Point3){ + 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 - }; + ); } double @@ -135,12 +135,12 @@ dotvec3(Point3 a, Point3 b) Point3 crossvec3(Point3 a, Point3 b) { - return (Point3){ + return Pt3( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x, 0 - }; + ); } double @@ -156,6 +156,6 @@ normvec3(Point3 v) len = vec3len(v); if(len == 0) - return (Point3){0, 0, 0, 0}; - return (Point3){v.x/len, v.y/len, v.z/len, 0}; + return Pt3(0,0,0,0); + return Pt3(v.x/len, v.y/len, v.z/len, 0); } diff --git a/quaternion.c b/quaternion.c index 3297788..8ef32b8 100644 --- a/quaternion.c +++ b/quaternion.c @@ -17,13 +17,13 @@ Quatvec(double s, Point3 v) Quaternion addq(Quaternion a, Quaternion b) { - return (Quaternion){a.r+b.r, a.i+b.i, a.j+b.j, a.k+b.k}; + return Quat(a.r+b.r, a.i+b.i, a.j+b.j, a.k+b.k); } Quaternion subq(Quaternion a, Quaternion b) { - return (Quaternion){a.r-b.r, a.i-b.i, a.j-b.j, a.k-b.k}; + return Quat(a.r-b.r, a.i-b.i, a.j-b.j, a.k-b.k); } Quaternion @@ -34,19 +34,19 @@ mulq(Quaternion q, Quaternion r) qv = Vec3(q.i, q.j, q.k); rv = Vec3(r.i, r.j, r.k); tmp = addpt3(addpt3(mulpt3(rv, q.r), mulpt3(qv, r.r)), crossvec3(qv, rv)); - return (Quaternion){q.r*r.r - dotvec3(qv, rv), tmp.x, tmp.y, tmp.z}; + return Quatvec(q.r*r.r - dotvec3(qv, rv), tmp); } Quaternion smulq(Quaternion q, double s) { - return (Quaternion){q.r*s, q.i*s, q.j*s, q.k*s}; + return Quat(q.r*s, q.i*s, q.j*s, q.k*s); } Quaternion sdivq(Quaternion q, double s) { - return (Quaternion){q.r/s, q.i/s, q.j/s, q.k/s}; + return Quat(q.r/s, q.i/s, q.j/s, q.k/s); } double @@ -62,14 +62,14 @@ invq(Quaternion q) len² = dotq(q, q); if(len² == 0) - return (Quaternion){0, 0, 0, 0}; - return (Quaternion){q.r/len², -q.i/len², -q.j/len², -q.k/len²}; + return Quat(0,0,0,0); + return Quat(q.r/len², -q.i/len², -q.j/len², -q.k/len²); } double qlen(Quaternion q) { - return sqrt(q.r*q.r + q.i*q.i + q.j*q.j + q.k*q.k); + return sqrt(dotq(q, q)); } Quaternion @@ -79,12 +79,12 @@ normq(Quaternion q) } Point3 -qrotate(Point3 p, Point3 axis, double angle) +qrotate(Point3 p, Point3 axis, double θ) { Quaternion qaxis, qr; - angle /= 2; - qaxis = Quatvec(cos(angle), mulpt3(axis, sin(angle))); + θ /= 2; + qaxis = Quatvec(cos(θ), mulpt3(axis, sin(θ))); qr = mulq(mulq(qaxis, Quatvec(0, p)), invq(qaxis)); - return Vec3(qr.i, qr.j, qr.k); + return Pt3(qr.i, qr.j, qr.k, p.w); } -- cgit v1.2.3