Home Rumble Youtube Twitter/X Kofi Contact / Crypto

 

Line is a line segment, in 2 or 3 dimensions. It may or may not be considered to continue past the start and end points depending on the function. Ray is a line in 2 or 3 dimensions that starts at a specific point and continues infinitely in a certain direction. Some functions may return values that are in the negative of the ray direction, eg, behind the start.

// Line *segments* typedef struct { union { vec2 start, a; }; union { vec2 end, b; }; } Line2; typedef struct { union { vec3 start, a; }; union { vec3 end, b; }; } Line3; // Rays, but also infinite lines (mathematical lines) because there is no practical // difference besides whether you conside the ray to be one-sided or not. typedef struct { vec3 o; // origin vec3 d; // normalized direction } Ray3; typedef struct { vec2 o; // origin vec2 d; // normalized direction } Ray2;

Not all distance and intersection functions have systematic names yet. In general, functions are named in the pattern [operation][lesser]N[greater]N(l, g, [args], out*) where operation is something like dist or intersect, lesser is the argument with fewer dimensions, greater is the argument with more dimensions, and N is the number of dimensions if otherwise unclear (lines and triangles can exist in 2 or 3 dimensions, for example, but spheres and circles imply their dimensionality). Functions with "T" in the name return a normalized value along with whatever other results.

Primitives

  • Line: An infinite line, unless otherwise specified, with no implied direction.
  • Ray: A line with a start location that extends inifinitely in some specified direction.
  • Rect: An axis-aligned rectangle defined by two opposing corners.
  • Quad: An arbitrary quadrilateral in 2 or 3 dimensions defined by a loop of consecutive corners. May or may not need to be planar or rectangular (right angles) depending on the function.
  • Circle: A circle defined by a center and a radius in 2 dimensions.
  • Plane: An infinite plane in 3 dimensions defined by a normal vector and a distance from the origin.
  • PlaneP: An infinite plane in 3 dimensions defined by a normal vector and a point on the plane.
  • Sphere: A sphere in 3 dimensions defined by a center and a radius.
  • Capsule: A pill-shaped 3D object defined by the center points of the two connected end spheres and a radius.

Operation Stubs

  • dist: Shortest distance between the two objects.
  • intersect: Determines if the two objects intersect.
  • findIntersect: Finds the point (or line) of intersection between two objects. Unless otherwise specified, it's the closest point of intersection if there are multiple.
  • proj: Projects the first object onto the second, whatever that means for the objects involved.

Functions

float distPoint2Triangle2(vec2 a, vec2 tri[3])
Two dimensional closest distance between a point a triangle.
int intersectRay2Circle(Ray2 a, vec2 center, float radius, vec2 out[2])
Returns the number of intersecting points, [0-2], in both directions of the ray.
int intersectVec2Circle(vec2 lorigin, vec2 ldir, vec2 center, float radius, vec2 out[2])
Returns the number of intersecting points, [0-2], in both directions of the ray.
int intersectPoint2Rect2(vec2 a, Quad2 q)
Must be a rectangle, not an arbitrary quadrilateral. Returns C3DLAS_INTERSECT or C3DLAS_DISJOINT.
int intersectLine2Line2(Line2 a, Line2 b)
Returns C3DLAS_INTERSECT, C3DLAS_DISJOINT, or C3DLAS_PARALLEL.
int intersectRect2Rect2(Quad2 a, Quad2 b)
Returns C3DLAS_INTERSECT, C3DLAS_DISJOINT.
int findIntersectLine2Line2(Line2 a, Line2 b, vec2* out)
Returns C3DLAS_INTERSECT, C3DLAS_DISJOINT, or C3DLAS_PARALLEL.
int findIntersectLine2Line2T(Line2 a, Line2 b, vec2* out, float* Ta, float* Tb)
Returns C3DLAS_INTERSECT, C3DLAS_DISJOINT, or C3DLAS_PARALLEL.
int findIntersectLine2Ray2(Line2 a, Ray2 b, vec2* out)
Returns C3DLAS_INTERSECT, C3DLAS_DISJOINT, or C3DLAS_PARALLEL.
float distLineLine3(Line3* a, Line3* b)
float projPointLine2(vec2 p, Line2 ls)
float distLine2Line2(Line2 a, Line2 b)
float distLine2Rect2(Line2 a, Quad2 q)
Quad must be a rectangle, and the vertices must be ordered in a loop
float distPoint2Rect2(vec2 a, Quad2 q)
Quad must be a rectangle, and the vertices must be ordered in a loop
float distLine2Triangle2(Line2 a, vec2 tri[3])
float distTPointRay3(vec3 p, Ray3 r, float* T)
float dist2TPointRay3(vec3 p, Ray3 r, float* T)
int intersectBoxLine3(AABB3 b, Line3 l, vec3* ipoint, float* idist) int intersectBoxLine3p(const AABB3* b, const Line3* l, vec3* ipoint, float* idist)
int intersectPlaneRay3p(Plane* pl, Ray3* ray, vec3* ipoint, float* idist)
Negative values of idist are "behind" ray->o. Returns C3DLAS_INTERSECT, C3DLAS_PARALLEL or C3DLAS_DISJOINT.
float distPoint2Line2(vec2 p, Line2 ls)
float distTPoint2Line2(vec2 p, Line2 ls)
float distPoint3Line3(vec3 p, Line3 ls)
float distTPoint3Line3(vec3 p, Line3 ls)
int findIntersectLinePlane(Line3 l, Plane* pl, Vector3* out)
Returns C3DLAS_INTERSECT, C3DLAS_COPLANAR or C3DLAS_DISJOINT.
int findIntersectFastLinePlane(Line3 l, Plane* pl, Vector3* out)
Assumes that the line and plan intersect; does not check for coplanarity. Returns C3DLAS_INTERSECT, C3DLAS_COPLANAR or C3DLAS_DISJOINT.

Not Yet Documented

Line2 shortestLineFromLineToLine2(Line2* a, Line2* b); // same algorithm as the above, but returns the points instead of their distance Line3 shortestLineFromLineToLine(Line3* a, Line3* b); // same algorithm as the above, but returns the points instead of their distance // http://geomalgorithms.com/a07-_distance.html // _PARALLEL with no output on parallel lines // _INTERSECT with one point of output on intersection // _DISJOINT with two outputs otherwise int shortestLineFromRayToRay3p(Ray3* r1, Ray3* r2, vec3* pOut); // reflects the distance from v to pivot across pivot. // out, pivot, and v will form a straight line with pivot exactly in the middle. void vReflectAcross3p(vec3* v, vec3* pivot, vec3* out); vec3 vReflectAcross3(vec3 v, vec3 pivot); // https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm // returns _INTERSECT or _DISJOINT int rayTriangleIntersect( vec3* a, vec3* b, vec3* c, // triangle vec3* ray_origin, vec3* ray_dir, // ray float* u, float* v, float* t // barycentric out coords, t of intersection point along ray ); // C3DLAS_COPLANAR, _PARALLEL, _INTERSECT, or _DISJOINT // aboveCnt and belowCnt are always set. int linePlaneClip3p( vec3* la, vec3* lb, Plane* pl, vec3* aboveOut, vec3* belowOut, int* aboveCnt, int* belowCnt ); int boxClipRay(AABB3* b, Ray3 r, Line3* out); // returns _INTERSECT or _DISJOINT void makeRay3p(vec3* origin, vec3* direction, Ray3* out); int boxRayIntersectFast3p(const AABB3* b, const Ray3* r); int boxRayIntersect3p(const AABB3* b, const Ray3* r, vec3* ipoint, float* idist); // calls the output fn once for every cell that the line crosses, in no particular order. // return a non-zero value from the output fn to stop iteration // returns 0 if all cells were scanned, 1 if the user stopped iteration early. int rasterizeLine2d(vec2 pa, vec2 pb, float cellSize, int (*found)(void* userData, int64_t x, int64_t y), void* userData); // conservative overestimation; the ends are square, not conformed to the radius int rasterizeFatLine2d(vec2 pa, vec2 pb, float width, float cellSize, int (*found)(void* userData, int64_t x, int64_t y), void* userData);