diff options
author | rodri <rgl@antares-labs.eu> | 2020-07-06 19:45:08 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2020-07-06 19:45:08 +0000 |
commit | 278c3d3be924246488226ae89e67616955dce661 (patch) | |
tree | 30709eb6ff90d16da3d88b010f52ca945f5646c9 | |
parent | 88e3ce5e4f8a942a5cf8f74aa295f5cab68d781b (diff) | |
download | libgeometry-278c3d3be924246488226ae89e67616955dce661.tar.gz libgeometry-278c3d3be924246488226ae89e67616955dce661.tar.bz2 libgeometry-278c3d3be924246488226ae89e67616955dce661.zip |
bring in the edge function and ptinpoly algorithm.
-rw-r--r-- | geometry.h | 6 | ||||
-rw-r--r-- | point.c | 36 |
2 files changed, 42 insertions, 0 deletions
@@ -7,6 +7,7 @@ typedef double Matrix3[4][4]; typedef struct Quaternion Quaternion; typedef struct RFrame RFrame; typedef struct RFrame3 RFrame3; +typedef struct Triangle2 Triangle2; typedef struct Triangle3 Triangle3; struct Point2 { @@ -31,6 +32,11 @@ struct RFrame3 { Point3 bx, by, bz; }; +struct Triangle2 +{ + Point2 p0, p1, p2; +}; + struct Triangle3 { Point3 p0, p1, p2; }; @@ -75,6 +75,42 @@ normvec2(Point2 v) return Pt2(v.x/len, v.y/len, 0); } +/* + * the edge function, from: + * + * Juan Pineda, A Parallel Algorithm for Polygon Rasterization, + * Computer Graphics, Vol. 22, No. 8, August 1988 + */ +int +edgeptcmp(Point2 e0, Point2 e1, Point2 p) +{ + Point3 e0p, e01, r; + + p = subpt2(p, e0); + e1 = subpt2(e1, e0); + e0p = Vec3(p.x,p.y,0); + e01 = Vec3(e1.x,e1.y,0); + r = crossvec3(e0p, e01); + + /* clamp to avoid overflow */ + return fclamp(r.z, -1, 1); /* e0.x*e1.y - e0.y*e1.x */ +} + +/* + * algorithm by W. Randolph Franklin + */ +int +ptinpoly(Point2 p, Point2 *pts, ulong npts) +{ + int i, j, c; + + for(i = c = 0, j = npts-1; i < npts; j = i++) + if(p.y < pts[i].y != p.y < pts[j].y && + p.x < (pts[j].x - pts[i].x) * (p.y - pts[i].y)/(pts[j].y - pts[i].y) + pts[i].x) + c ^= 1; + return c; +} + /* 3D */ Point3 |