Skip to content

Commit

Permalink
struct filling corrections.
Browse files Browse the repository at this point in the history
  • Loading branch information
SametSisartenep committed Apr 14, 2020
1 parent 0c954e9 commit 7208029
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
36 changes: 18 additions & 18 deletions point.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@ 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
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
Expand All @@ -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 */
Expand All @@ -92,38 +92,38 @@ 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
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
Expand All @@ -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
Expand All @@ -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);
}
24 changes: 12 additions & 12 deletions quaternion.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}

0 comments on commit 7208029

Please sign in to comment.