Difference between revisions of "Geometric"

From Second Life Wiki
Jump to navigation Jump to search
m (language tags to <source>)
 
(67 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header}}
{{RightToc|clear:right;}}


Note; Currently working on a [[User:Nexii_Malthus/GeometricLib|new design of this page]] so this page might be rewritten at any moment.
'''Please vote for:
https://jira.secondlife.com/browse/WEB-235'''
''So that I can expand each function into deeper detail without the page starting to fail in readability.'' --[[User:Nexii Malthus|Nexii Malthus]] 23:05, 24 October 2008 (UTC)
:Break it up so each major section has its own page... thats why hypertext was invented. -[[User:Overbrain Unplugged|Overbrain Unplugged]] 13:36, 10 October 2010 (UTC)


==Original optimized version==
== Geometric Library ==
I tried to minimize the script function names to be easily readable. All the geometric function names start with a g.
 
Please note, Nexii Malthus is no maths professor and so one or two functions might not work as expected.
__Toc__


Here is the legend:
=== <div style="font-size: 120%;">[[#Geometric Library|Line Functions]]</div> ===


{| class="sortable" {{Prettytable}}
<!--############# LINE NEAREST POINT #############-->
|-{{Hl2}}
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
! '''Shorthand'''
!style="color: #000000; background-color: #aaaaff;" height="20px"|
! '''Name'''
==== Line and Point, Vector ====
! class="unsortable" | '''Description'''
|-
|-
||X
|
||Point
Calculates the vector from a point ''''to'''' the closest point on a line
||vector defining a point in space
 
<source lang="lsl2">
vector gLXdV(vector O,vector D,vector A){
    return (O-A)-((O-A)*D)*D;}
</source>
 
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
|-
||L
| vector O
||Line
| Origin of Line
||A line has an origin and a direction
|-
|-
||R
| vector D
||Ray
| Direction of Line
||A ray is like a line, except it is more distinct as it can define weather it points forward or back
|-
|-
||P
| vector A
||Plane
| Origin of Point
||A two dimensional doubly ruled surface of infinite size
|-
|-
||S
!style="background-color: #d0d0ee" | Output
||Sphere
!style="background-color: #d0d0ee" | Description
||A sphere is defined by origin and radius
|-
|-
||d
| return vector gLXdV
||Distance
| Returns origin of closest point on Line to Point
||Calculate distance
|}
|-
<div style="float:left;font-size: 80%;">
||n
'''3D'''</div>
||Nearest
<div style="float:right;font-size: 80%;">
||Calculate nearest
By Nexii Malthus</div>
|-
||p
||Project
||Calculates projection
|-
||x
||Intersection
||Calculates intersection
|-
||Z
||Float
||Represents that a float is returned
|-
||V
||Vector
||Represents that a vector is returned
|-
||O
||Origin
||Represents the Origin of the ray or line
|-
||D
||Direction
||Direction from the Origin
|}
|}


== Documentation ==
<!--############# LINE NEAREST POINT, DISTANCE #############-->
=== Line Functions ===
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Line Nearest Point
==== Line and Point, Distance ====
|-
|-
|
|
Calculates the vector from a point to the closest point on a line
Calculates distance of line to point, same as measuring magnitude of Line and Point Vector, but faster on it's own
<lsl>
<source lang="lsl2">
vector gLXdV(vector O,vector D,vector A){
float gLXdZ(vector O,vector D,vector A){
     return (O-A)-((O-A)*D)*D;}
     vector k = ( A - O ) % D;
</lsl>
    return llSqrt( k * k );}
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 90: Line 71:
|-
|-
| vector D
| vector D
| Direction of Line
| Direction of Line (unit vector)
|-
|-
| vector A
| vector A
Line 98: Line 79:
!style="background-color: #d0d0ee" | Description
!style="background-color: #d0d0ee" | Description
|-
|-
| return vector gLXdV
| return float gLXdZ
| Returns origin of closest point on Line to Point
| Returns numerical distance from Line to Point
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# LINE NEAREST POINT, NEAREST POINT #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Line Nearest Point, Distance
 
==== Line Nearest Point, Nearest Point ====
|-
|-
|
|
Calculates distance of this vector, but faster on it's own
Returns nearest point on line to given point
<lsl>
<source lang="lsl2">
float gLXdZ(vector O,vector D,vector A){
vector gLXnX(vector O,vector D,vector A){
     return llSqrt(CP((A-O),D)*CP((A-O),D));}
     return gLXdV(O,D,A) + A;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 130: Line 117:
!style="background-color: #d0d0ee" | Description
!style="background-color: #d0d0ee" | Description
|-
|-
| return float gLXdZ
| return vector gLXnX
| Returns numerical distance from Line to Point
| Returns nearest point on line given point
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| function vector gLXdV(vector O,vector D,vector A)
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# LINE AND LINE, VECTOR #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Line and Line, Vector
==== Line and Line, Vector ====
|-
|-
|
|
Shortest vector of two lines
Shortest vector of two lines
<lsl>
<source lang="lsl2">
vector gLLdV(vector O1,vector D1,vector O2,vector D2){
vector gLLdV(vector O1,vector D1,vector O2,vector D2){
     return Project3D( (O2-O1), CP(D1,D2) );}
    vector A = O2 - O1; vector B = D1 % D2;
</lsl>
     return B*( (A*B)/(B*B) );}
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 168: Line 165:
| Returns shortest vector between the two lines
| Returns shortest vector between the two lines
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# LINE AND LINE, DISTANCE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Line and Line, Distance
==== Line and Line, Distance ====
|-
|-
|
|
Returns the distance between two lines
Returns the distance between two lines
<lsl>
<source lang="lsl2">
float gLLdZ(vector O1,vector D1,vector O2,vector D2){
float gLLdZ(vector O1,vector D1,vector O2,vector D2){
     vector A = CP(D1,D2);float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>;
     vector A = D1%D2;float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>;
     return (O2-O1) * A;}
     return (O2-O1) * A;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 204: Line 206:
| Returns numerical distance between the two lines
| Returns numerical distance between the two lines
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# LINE AND LINE, NEAREST POINT #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Line and Line, Nearest point
==== Line and Line, Nearest point ====
|-
|-
|
|


Closest point of two lines
Closest point of two lines
<lsl>
<source lang="lsl2">
vector gLLnX(vector O1,vector D1,vector O2,vector D2){
vector gLLnX(vector O1,vector D1,vector O2,vector D2){
     vector nO1 = < O1*D1, O1*D2, 0>;
     vector nO1 = < O1*D1, O1*D2, 0>;
Line 225: Line 232:
      
      
     return O1 + D1*t;}
     return O1 + D1*t;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 249: Line 256:
| Returns closest point between the two lines
| Returns closest point between the two lines
|}
|}
<div style="float:left;font-size: 80%;">
'''2D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
<!--############# LINE AND LINE, INTERSECTION POINT #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
==== Line and Line, intersection point ====
|-
|
Computes intersection point of two lines, if there is any, else <-1,-1,-1> if none.
<source lang="lsl2">
vector gLLxX( vector A, vector B, vector C, vector D ){
    vector b = B-A; vector d = D-C;
    float dotperp = b.x*d.y - b.y*d.x;
    if (dotperp == 0) return <-1,-1,-1>;
    vector c = C-A;
    float t = (c.x*d.y - c.y*d.x) / dotperp;
    return <A.x + t*b.x, A.y + t*b.y, 0>;}
</source>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| vector A
| Start of Line 1
|-
| vector B
| End of Line 1
|-
| vector C
| Start of Line 2
|-
| vector D
| End of Line 2
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return vector gLLxX
| The intersection point of the two lines, else <-1,-1,-1> if none
|}
<div style="float:left;font-size: 80%;">
'''2D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}
<!--############# LINE AND LINE, TWO NEAREST POINTS #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Line and Line, two nearest points along both lines
 
==== Line and Line, two nearest points of lines ====
|-
|-
|
|
Two closest points of two lines on each line
Two closest points of two lines on each line
<lsl>
<source lang="lsl2">
vector X1;vector X2;
vector X1;vector X2;
gLLnnXX(vector O1,vector D1,vector O2,vector D2){
gLLnnXX(vector O1,vector D1,vector O2,vector D2){
Line 269: Line 328:
      
      
     X1 = O1 + D1*t;
     X1 = O1 + D1*t;
     X2 = X1 + CP(nD1,nD2);}
     X2 = X1 + nD1%nD2;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 296: Line 355:
| vector X2
| vector X2
| Closest point on line 2 to line 1
| Closest point on line 2 to line 1
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| global vector X1
|-
|style="background-color: #eed0d0" colspan="2"| global vector X2
|}
<div style="float:left;font-size: 80%;">
'''2D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
<!--############# LINE AND LINE, NEAREST LINE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
==== Line and Line, nearest line ====
|-
|
Input two lines, the function will return a list containing two vectors responding to the line nearest between them. As well as two floats corresponding to the scalar value on the two line of where the line has an end located at.
<source lang="lsl2">
list gLLnL( vector v0, vector v1, vector v2, vector v3 ) {
    float Eps = 0.000001; vector vx; vector vy; vector va; vector vb; vector vc;
    float x; float y; float d0; float d1; float d2; float d3; float d4;
    va = v0-v2; vb = v3-v2; if(llVecMag(vb)<Eps) return [];
    vc = v1-v0; if(llVecMag(vc)<Eps) return [];
    d0 = va*vb; d1 = vb*vc; d2 = va*vc; d3 = vb*vb; d4 = vc*vc;
    float den = d4*d3-d1*d1; if( llFabs(den) < Eps ) return [];
    float num = d0*d1-d2*d3; x = num/den; y = (d0+d1*x)/d3;
    vx = v0+vc*x; vy = v2+vb*y; return [vx,vy,x,y]; }
</source>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| vector v0
| Point on Line 1
|-
| vector v1
| Point on Line 1
|-
| vector v2
| Point on Line 2
|-
| vector v3
| Point on Line 2
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| [vx]
| Nearest point on Line 1 to Line 2
|-
| [vy]
| Nearest point on Line 2 to Line 1
|-
| [x]
| Scalar value representing location of vx on line 1 (range [v0,v1])
|-
| [y]
| Scalar value representing location of vy on line 2 (range [v2,v3])
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# LINE AND LINE SEGMENTS, NEAREST LINE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Line and Line, two nearest points with vector and distance
 
==== Line and Line Segments, nearest line segment ====
|-
|
Input two line segments, the function will return a list containing two vectors responding to the line segment nearest between them. As well as two floats corresponding to the scalar value on the two line segments of where the line segment has an end located at.
<source lang="lsl2">
list gLSLSnLS( vector v0, vector v1, vector v2, vector v3 ) {
    float Eps = 0.000001; vector vx; vector vy; vector va; vector vb; vector vc;
    float x; float y; float d0; float d1; float d2; float d3; float d4;
    va = v0-v2; vb = v3-v2; if(llVecMag(vb)<Eps) return [];
    vc = v1-v0; if(llVecMag(vc)<Eps) return [];
    if( llFabs(vc.x + vc.y + vc.z) < Eps ) return [];
    d0 = va*vb; d1 = vb*vc; d2 = va*vc; d3 = vb*vb; d4 = vc*vc;
    float den = d4*d3-d1*d1; if( llFabs(den) < Eps ) return [];
    float num = d0*d1-d2*d3; x = num/den; y = (d0+d1*x)/d3;
    if(x<0)x=0; else if(x>1)x=1; if(y<0)y=0; else if(y>1)y=1;
    vx = v0+vc*x; vy = v2+vb*y; return [vx,vy,x,y]; }
</source>
 
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| vector v0
| Start of Line Segment 1
|-
| vector v1
| End of Line Segment 1
|-
| vector v2
| Start of Line Segment 2
|-
| vector v3
| End of Line Segment 2
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| [vx]
| Nearest point on Line Segment 1 to Line Segment 2
|-
| [vy]
| Nearest point on Line Segment 2 to Line Segment 1
|-
| [x]
| Scalar value representing location of vx on Line Segment 1 (range [v0,v1])
|-
| [y]
| Scalar value representing location of vy on Line Segment 2 (range [v2,v3])
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# LINE AND LINE, TWO NEAREST POINTS, VECTOR AND DISTANCE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
 
==== Line and Line, two nearest points with vector and distance ====
|-
|-
|
|
Computes two closest points of two lines, vector and distance
Computes two closest points of two lines, vector and distance
<lsl>
<source lang="lsl2">
vector X1;vector X2;vector V1;float Z1;
vector X1;vector X2;vector V1;float Z1;
gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){
gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){
Line 319: Line 506:
     X1 = O1 + D1*t;
     X1 = O1 + D1*t;
     X2 = X1 + CP(nD1,nD2);
     X2 = X1 + CP(nD1,nD2);
     V1 = CP(nD1,nD2);
     V1 = nD1%nD2;
     Z1 = llVecMag(V1);}
     Z1 = llVecMag(V1);}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 353: Line 540:
| float Z1
| float Z1
| Numerical distance of line 1 to line 2
| Numerical distance of line 1 to line 2
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| global vector X1
|-
|style="background-color: #eed0d0" colspan="2"| global vector X2
|-
|style="background-color: #eed0d0" colspan="2"| global vector V1
|-
|style="background-color: #eed0d0" colspan="2"| global float Z1
|}
|}
<div style="float:left;font-size: 80%;">
'''2D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


=== Plane Functions ===
<!--############# Line and Point, Direction #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
==== Line and Point, Direction ====
|-
|
Works out where point (X) is relative to the line of the segment (L0, L1).
<source lang="lsl2">
float gLSPdir( vector L0, vector L1, vector X ){
    return (L1.x - L0.x)*(X.y - L0.y) - (X.x - L0.x)*(L1.y - L0.y);
}
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| vector L0, vector L1
| Start and End of line segment
|-
| vector X
| Origin of Point
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| float isLeft( vector L0, vector L1, vector X )
| Returns float, >0 is Left, 0 on Line, <0 is Right, according to line angle
|}
<div style="float:left;font-size: 80%;">
'''2D'''</div>
<div style="float:right;font-size: 80%;">
Copyright 2001, softSurfer (www.softsurfer.com) (Must accept License #2), LSL-Port By Nexii Malthus</div>
|}
=== <div style="font-size: 120%;">[[#Geometric Library|Plane Functions]]</div> ===
<!--############# PLANE AND POINT, DISTANCE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Point, Distance
==== Plane and Point, Distance ====
|-
|-
|
|
Finds distance of a point from a plane
Finds distance of a point from a plane
<lsl>
<source lang="lsl2">
float gPXdZ(vector Pn,float Pd,vector A){
float gPXdZ(vector Pn,float Pd,vector A){
     return A * Pn + Pd;}
     return A * Pn + Pd;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 374: Line 611:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane
| Normal of Plane (unit vector)
|-
|-
| float Pd
| float Pd
Line 388: Line 625:
| Returns Distance between plane and point
| Returns Distance between plane and point
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# PLANE AND POINT, VECTOR #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Point, Vector
==== Plane and Point, Vector ====
|-
|-
|
|
Finds vector that points from point to nearest on plane
Finds vector that points from point to nearest on plane
<lsl>
<source lang="lsl2">
vector gPXdV(vector Pn,float Pd,vector A){
vector gPXdV(vector Pn,float Pd,vector A){
     return -(Pn * A + Pd)*Pn;}
     return -(Pn * A + Pd)*Pn;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 406: Line 648:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane
| Normal of Plane (unit vector)
|-
|-
| float Pd
| float Pd
Line 420: Line 662:
| Returns vector from point to closest point on plane
| Returns vector from point to closest point on plane
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# PLANE AND POINT, NEAREST POINT #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Point, Nearest point
==== Plane and Point, Nearest point ====
|-
|-
|
|
Finds closest point on plane given point
Finds closest point on plane given point
<lsl>
<source lang="lsl2">
vector gPXnX(vector Pn,float Pd,vector A){
vector gPXnX(vector Pn,float Pd,vector A){
     return A - (Pn * A + Pd) * Pn;}
     return A - (Pn * A + Pd) * Pn;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 438: Line 685:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane
| Normal of Plane (unit vector)
|-
|-
| float Pd
| float Pd
Line 452: Line 699:
| Returns vector of a point from closest of point to plane
| Returns vector of a point from closest of point to plane
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# PLANE AND RAY, INTERSECTION DISTANCE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Ray, Intersection Distance
==== Plane and Ray, Intersection Distance ====
|-
|-
|
|
Finds distance to intersection of plane along ray
Finds distance to intersection of plane along ray
<lsl>
<source lang="lsl2">
float gPRxZ(vector Pn,float Pd,vector O,vector D){
float gPRxZ(vector Pn,float Pd,vector O,vector D){
    //
     return -((Pn*O+Pd)/(Pn*D));}
     return -( ( (Pn*D)+(Pn*O) ) / (Pn*D) );}
</source>
    //return -( (Pn*D)/(Pn*O+Pd) );}
</lsl>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 472: Line 722:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane
| Normal of Plane (unit vector)
|-
|-
| float Pd
| float Pd
Line 489: Line 739:
| Returns float distance of intersection between ray and plane
| Returns float distance of intersection between ray and plane
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# PLANE AND RAY, VECTOR #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Ray, Vector
==== Plane and Ray, Vector ====
|-
|-
|
|
Finds distance vector along a ray to a plane
Finds distance vector along a ray to a plane
<lsl>
<source lang="lsl2">
vector gPRdV(vector Pn,float Pd,vector O,vector D){
vector gPRdV(vector Pn,float Pd,vector O,vector D){
     return D * gPRxZ(Pn,Pd,O,D);}
     return D * gPRxZ(Pn,Pd,O,D);}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 507: Line 762:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane
| Normal of Plane (unit vector)
|-
|-
| float Pd
| float Pd
Line 523: Line 778:
| return vector gPRdV
| return vector gPRdV
| Returns vector along a ray to a plane
| Returns vector along a ray to a plane
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| function float gPRxZ(vector Pn,float Pd,vector O,vector D)
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# PLANE AND RAY, INTERSECTION POINT #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Ray, Intersection Point
==== Plane and Ray, Intersection Point ====
|-
|-
|
|
Finds intersection point along a ray to a plane
Finds intersection point along a ray to a plane
<lsl>
<source lang="lsl2">
vector gPRxX(vector Pn,float Pd,vector O,vector D){
vector gPRxX(vector Pn,float Pd,vector O,vector D){
     return O + gPRdV(Pn,Pd,O,D);}
     return O + gPRdV(Pn,Pd,O,D);}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 542: Line 806:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane
| Normal of Plane (unit vector)
|-
|-
| float Pd
| float Pd
Line 558: Line 822:
| return vector gPRxX
| return vector gPRxX
| Returns vector point of intersection between ray and plane
| Returns vector point of intersection between ray and plane
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| function vector gPRdV(vector Pn,float Pd,vector O,vector D)
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# PLANE AND LINE, INTERSECTION POINT #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Line, Intersection Point
==== Plane and Line, Intersection Point ====
|-
|-
|
|
Finds interesection point of a line and a plane
Finds interesection point of a line and a plane
<lsl>
<source lang="lsl2">
vector gPLxX(vector Pn,float Pd,vector O,vector D){
vector gPLxX(vector Pn,float Pd,vector O,vector D){
     return O -( (Pn*D)/(Pn*O+Pd) )*D;}
     return O + D*-( (Pn*O-Pd)/(Pn*D) );}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 577: Line 850:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane
| Normal of Plane (unit vector)
|-
|-
| float Pd
| float Pd
Line 594: Line 867:
| Returns vector point of intersection between line and plane
| Returns vector point of intersection between line and plane
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# PLANE AND PLANE, INTERSECTION LINE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Plane, Intersection Line
==== Plane and Plane, Intersection Line ====
|-
|-
|
|
Finds line of intersection of two planes
Finds line of intersection of two planes
<lsl>
<source lang="lsl2">
vector oO;vector oD;
vector oO;vector oD;


gPPxL(vector Pn,float Pd,vector Qn,float Qd){
gPPxL(vector Pn,float Pd,vector Qn,float Qd){
     oD = CP(Pn,Qn)/llVecMag(CP(Pn,Qn));
     oD = (Pn%Qn)/llVecMag(Pn%Qn);
     vector Cross = CP(CP(Pn,Qn),Pn);
     vector Cross = (Pn%Qn)%Pn;
     vector Bleh = (-Pd*Pn);
     vector Bleh = (-Pd*Pn);
     oO = Bleh - (Qn*Cross)/(Qn*Bleh+Qd)*Cross/llVecMag(Cross);}
     oO = Bleh - (Qn*Cross)/(Qn*Bleh+Qd)*Cross/llVecMag(Cross);}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 617: Line 895:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane 1
| Normal of Plane 1 (unit vector)
|-
|-
| float Pd
| float Pd
Line 623: Line 901:
|-
|-
| vector Qn
| vector Qn
| Origin of Plane 2
| Normal of Plane 2 (unit vector)
|-
|-
| float Qd
| float Qd
Line 636: Line 914:
| vector oD
| vector oD
| Intersection Line's direction
| Intersection Line's direction
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| global vector oO
|-
|style="background-color: #eed0d0" colspan="2"| global vector oD
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# PLANE AND RAY, PROJECTION #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Plane and Ray, Projection
==== Plane and Ray, Projection ====
|-
|-
|
|
Projects a ray onto a plane
Projects a ray onto a plane
<lsl>
<source lang="lsl2">
vector oO;vector oD;
vector oO;vector oD;


gPRpR(vector Pn,float Pd,vector O,vector D){
gPRpR(vector Pn,float Pd,vector O,vector D){
     oO = O - (Pn * O + Pd) * Pn;
     oO = O - (Pn * O + Pd) * Pn;
     vector t = llVecNorm( D - Project3D(D,Pn) );t = <1.0/t.x,1.0/t.y,1.0/t.z>;
     vector t = llVecNorm( D - (Pn*((D*Pn)/(Pn*Pn))) );t = <1.0/t.x,1.0/t.y,1.0/t.z>;
     oD = CP(Pn,t);}
     oD = Pn%t;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 659: Line 948:
|-
|-
| vector Pn
| vector Pn
| Normal of Plane
| Normal of Plane (unit vector)
|-
|-
| float Pd
| float Pd
Line 679: Line 968:
| Projected Ray Direction
| Projected Ray Direction
|-
|-
!style="background-color: #eed0d0" | Requirement
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| global vector oO
|-
|-
| function Project3D(D,Pn)
|style="background-color: #eed0d0" colspan="2"| global vector oD
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


=== Sphere Functions ===
=== <div style="font-size: 120%;">[[#Geometric Library|Sphere Functions]]</div> ===


<!--############# SPHERE AND RAY, INTERSECTION POINT #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Sphere and Ray, Intersection Point
==== Sphere and Ray, Intersection Point ====
|-
|-
|
|
Finds intersection point of sphere and ray
Finds intersection point of sphere and ray
<lsl>
<source lang="lsl2">
vector gSRxX(vector Sp, float Sr, vector Ro, vector Rd){
vector gSRxX(vector Sp, float Sr, vector Ro, vector Rd){
     float t;Ro = Ro - Sp;
     float t; Ro -= Sp;
     if(Rd == ZERO_VECTOR) return ZERO_VECTOR;
     if(Rd == ZERO_VECTOR) return ZERO_VECTOR;
      
      
Line 730: Line 1,026:
         t = t0;
         t = t0;
      
      
     return Ro + (t * Rd);
     return Sp + Ro + (t * Rd);
}
}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 756: Line 1,052:
| Returns intersection point of sphere and ray otherwise ZERO_VECTOR
| Returns intersection point of sphere and ray otherwise ZERO_VECTOR
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


<!--############# SPHERE AND RAY, INTERSECTION BOOLEAN #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Sphere and Ray, Intersection Boolean
 
==== Sphere and Ray, Intersection Boolean ====
|-
|-
|
|
Finds if there is a intersection of sphere and ray
Finds if there is a intersection of sphere and ray
<lsl>
<source lang="lsl2">
integer gSRx(vector Sp, float Sr, vector Ro, vector Rd){
integer gSRx(vector Sp, float Sr, vector Ro, vector Rd){
     float t;Ro = Ro - Sp;
     float t;Ro = Ro - Sp;
Line 779: Line 1,081:
     return TRUE;
     return TRUE;
}
}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 803: Line 1,105:
| Returns a boolean indicating if there is a valid intersection
| Returns a boolean indicating if there is a valid intersection
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


=== Ray Functions ===
=== <div style="font-size: 120%;">[[#Geometric Library|Ray Functions]]</div> ===


<!--############# RAY AND POINT, PROJECTED DISTANCE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
!style="color: #000000; background-color: #aaaaff;" height="20px"|
Ray and Point, projected distance  
==== Ray and Point, projected distance ====
|-
|-
|
|
Finds projected distance of a point along a ray
Finds projected distance of a point along a ray
<lsl>
<source lang="lsl2">
float gRXpZ(vector O,vector D,vector A){
float gRXpZ(vector O,vector D,vector A){
     return (A-O)*D;}
     return (A-O)*D;}
</lsl>
</source>


{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
Line 837: Line 1,144:
| Returns projected distance of a point along a ray
| Returns projected distance of a point along a ray
|}
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
|}


== Geometric Library Code ==
=== <div style="font-size: 120%;">[[#Geometric Library|Box Functions]]</div> ===
=== Legend ===
I tried to minimize the script function names to be easily readable. All the geometric function names start with a g.
Please note, Nexii Malthus is no maths professor and so one or two functions might not work as expected.


Here is the legend:
<!--############# BOX AND RAY, INTERSECTION DISTANCE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
==== Box and Ray, Intersection Distance ====
|-
|
Finds intersection of a Ray to a Box and returns intersection distance, otherwise -1 if there is no legal intersection.
<source lang="lsl2">
float gBRxZ(vector Ro,vector Rd, vector Bo, vector Bs, rotation Br){
    vector oB = (Ro-Bo)/Br;    vector dB = Rd/Br;    vector eB = 0.5*Bs;
    float mD = -1.0;    float D;    vector X;
   
    if(llFabs(dB.x) > 0.000001){
        D = (-eB.x - oB.x ) / dB.x;
        if(D >= 0.0){
            X = oB + D * dB;
            if(X.y >= -eB.y && X.y <= eB.y && X.z >= -eB.z && X.z <= eB.z)
                mD = D;
        }
        D = ( eB.x - oB.x ) / dB.x;
        if (D >= 0.0){
            X = oB + D * dB;
            if(X.y >= -eB.y && X.y <= eB.y && X.z >= -eB.z && X.z <= eB.z)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
    }
   
    if(llFabs(dB.y) > 0.000001){
        D = (-eB.y - oB.y ) / dB.y;
        if(D >= 0.0){
            X = oB + D * dB;
            if(X.x >= -eB.x && X.x <= eB.x && X.z >= -eB.z && X.z <= eB.z)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
        D = ( eB.y - oB.y ) / dB.y;
        if (D >= 0.0){
            X = oB + D * dB;
            if(X.x >= -eB.x && X.x <= eB.x && X.z >= -eB.z && X.z <= eB.z)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
    }
   
    if(llFabs(dB.z) > 0.000001){
        D = (-eB.z - oB.z ) / dB.z;
        if(D >= 0.0){
            X = oB + D * dB;
            if(X.x >= -eB.x && X.x <= eB.x && X.y >= -eB.y && X.y <= eB.y)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
        D = ( eB.z - oB.z ) / dB.z;
        if (D >= 0.0){
            X = oB + D * dB;
            if(X.x >= -eB.x && X.x <= eB.x && X.y >= -eB.y && X.y <= eB.y)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
    }
   
    return mD;
}
</source>


{| class="sortable" {{Prettytable}}
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
|-{{Hl2}}
!style="background-color: #d0d0ee" | Input
! '''Shorthand'''
!style="background-color: #d0d0ee" | Description
! '''Name'''
|-
! class="unsortable" | '''Description'''
| vector Ro
| Origin of Ray
|-
| vector Rd
| Direction of Ray
|-
| vector Bo
| Origin of Box
|-
| vector Bs
| Size of Box
|-
|-
||X
| rotation Br
||Point
| Rotation of Box
||vector defining a point in space
|-
|-
||L
!style="background-color: #d0d0ee" | Output
||Line
!style="background-color: #d0d0ee" | Description
||A line has an origin and a direction
|-
|-
||R
| float gBRxZ
||Ray
| Returns distance to intersection of a ray and a box
||A ray is like a line, except it is more distinct as it can define weather it points forward or back
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By [http://forums.secondlife.com/showpost.php?p=1984100&postcount=7 Hewee Zetkin]</div>
|}
 
<!--############# BOX AND RAY, INTERSECTION POINT #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
==== Box and Ray, Intersection Point ====
|-
|-
||P
|
||Plane
Finds intersection of a Ray to a Box and returns intersection point, otherwise ZERO_VECTOR if there is no legal intersection.
||A two dimensional doubly ruled surface of infinite size
<source lang="lsl2">
vector gBRxX( vector Ro, vector Rd, vector Bo, vector Bs, rotation Br){
    float k = gBRxZ(Ro,Rd,Bo,Bs,Br);
    if( k != -1.0 ) return Ro + Rd * k;
    else return ZERO_VECTOR;}
</source>
 
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
|-
||S
| vector Ro
||Sphere
| Origin of Ray
||A sphere is defined by origin and radius
|-
|-
||d
| vector Rd
||Distance
| Direction of Ray
||Calculate distance
|-
|-
||n
| vector Bo
||Nearest
| Origin of Box
||Calculate nearest
|-
|-
||p
| vector Bs
||Project
| Size of Box
||Calculates projection
|-
|-
||x
| rotation Br
||Intersection
| Rotation of Box
||Calculates intersection
|-
|-
||Z
!style="background-color: #d0d0ee" | Output
||Float
!style="background-color: #d0d0ee" | Description
||Represents that a float is returned
|-
|-
||V
| vector gBRxX
||Vector
| Returns point of intersection of a ray and a box
||Represents that a vector is returned
|-
|-
||O
!style="background-color: #eed0d0" colspan="2"| Requirement
||Origin
||Represents the Origin of the ray or line
|-
|-
||D
|style="background-color: #eed0d0" colspan="2"| float gBRxZ(vector Ro,vector Rd, vector Bo, vector Bs, rotation Br)
||Direction
|}
||Direction from the Origin
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By [http://forums.secondlife.com/showpost.php?p=1984100&postcount=7 Hewee Zetkin]</div>
|}
|}


=== LSL Script ===
<!--############# BOX AND POINT, INTERSECTION BOOLEAN #############-->
This is the script in its entirety.
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
<lsl>//===================================================//
!style="color: #000000; background-color: #aaaaff;" height="20px"|
//              Geometric Library 1.0              //
//              "May 4 2008", "2:24:30"             //
//  Copyright (C) 2008, Nexii Malthus (cc-by)        //
//    http://creativecommons.org/licenses/by/3.0/    //
//===================================================//


vector CP(vector A,vector B){
==== Box and Point, Intersection Boolean ====
     return A % B;}
|-
|
Finds if there is an intersection of a Point and a Box and returns boolean
<source lang="lsl2">
integer gBXx(vector A, vector Bo, vector Bs, rotation Br){
    vector eB = 0.5*Bs; vector rA = (A-Bo)/Br;
     return (rA.x<eB.x && rA.x>-eB.x && rA.y<eB.y && rA.y>-eB.y && rA.z<eB.z && rA.z>-eB.z); }
</source>


vector Project3D(vector A,vector B){
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
    vector proj;
!style="background-color: #d0d0ee" | Input
    proj.x = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.x;
!style="background-color: #d0d0ee" | Description
    proj.y = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.y;
|-
    proj.z = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.z;
| vector A
    return proj;}
| Origin of Point
|-
| vector Bo
| Origin of Box
|-
| vector Bs
| Size of Box
|-
| rotation Br
| Rotation of Box
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| integer gBXx(vector A, vector Bo, vector Bs, rotation Br)
| Returns boolean check of intersection of a point and a box if there is one, otherwise FALSE
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}


// POINT //
<!--############# BOX AND POINT, NEAREST POINT ON EDGE #############-->
float gXXdZ(vector A,vector B){
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
    // Distance P2P.
!style="color: #000000; background-color: #aaaaff;" height="20px"|
    return llVecDist(A,B);}


vector gXXdV(vector A,vector B){
==== Box and Point, Nearest Point on Edge ====
    // Vector to move from P2P.
|-
    return A-B;}
|
 
Processes point on nearest edge of box to given point
// LINE //
<source lang="lsl2">
vector gLXdV(vector O,vector D,vector A){
vector gBXnEX(vector A, vector Bo, vector Bs, rotation Br){
    // Calculates the vector from a point to the closest point on a line
     vector eB = 0.5*<llFabs(Bs.x),llFabs(Bs.y),llFabs(Bs.z)>;
    return (O-A)-((O-A)*D)*D;}
     vector rA = (A-Bo)/Br;
 
float gLXdZ(vector O,vector D,vector A){
    // Calculates distance of this vector, but faster on it's own
    return llSqrt(CP((A-O),D)*CP((A-O),D));}
 
vector gLLdV(vector O1,vector D1,vector O2,vector D2){
     // Shortest vector of two lines
    return Project3D( (O2-O1), CP(D1,D2) );}
 
float gLLdZ(vector O1,vector D1,vector O2,vector D2){
    // Returns the distance between two lines
    vector A = CP(D1,D2);float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>;
    return (O2-O1) * A;}
 
vector gLLnX(vector O1,vector D1,vector O2,vector D2){
    // Closest point of two lines
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
     vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
   
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
      
      
     t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
     float mD = 3.402823466E+38;
    vector X;
    list EdgesX = [< 0, eB.y, eB.z>, < 0,-eB.y, eB.z>, < 0,-eB.y,-eB.z>, < 0, eB.y,-eB.z>];
    list EdgesY = [< eB.x, 0, eB.z>, <-eB.x, 0, eB.z>, <-eB.x, 0,-eB.z>, < eB.x, 0,-eB.z>];
    list EdgesZ = [< eB.x, eB.y, 0>, <-eB.x, eB.y, 0>, <-eB.x,-eB.y, 0>, < eB.x,-eB.y, 0>];
      
      
     return O1 + D1*t;}
     integer x = (EdgesX != []);
 
    while( x-- ){
vector X1;vector X2;vector V1;float Z1;
        float y = gLXdZ( llList2Vector( EdgesX, x ), <1,0,0>, rA );
 
       
gLLnnXX(vector O1,vector D1,vector O2,vector D2){
        if( rA.x > eB.x ) y += rA.x - eB.x;
     // Two closest points of two lines
        else if( rA.x < -eB.x ) y -= rA.x - -eB.x;
     vector nO1 = < O1*D1, O1*D2, 0>;
       
    vector nO2 = < O2*D1, O2*D2, 0>;
        if( y < mD ){ mD = y; X = gLXnX( llList2Vector( EdgesX, x ), <1,0,0>, rA ); }
     vector nD1 = < D1*D1, O1*D2, 0>;
    }
     vector nD2 = < O2*D1, O2*D2, 0>;
     x = (EdgesY != []);
   
     while( x-- ){
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
        float y = gLXdZ( llList2Vector( EdgesY, x ), <0,1,0>, rA );
       
        if( rA.y > eB.y ) y += rA.y - eB.y;
        else if( rA.y < -eB.y ) y -= rA.y - -eB.y;
       
        if( y < mD ){ mD = y; X = gLXnX( llList2Vector( EdgesY, x ), <0,1,0>, rA ); }
    }
     x = (EdgesZ != []);
     while( x-- ){
        float y = gLXdZ( llList2Vector( EdgesZ, x ), <0,0,1>, rA );
       
        if( rA.z > eB.z ) y += rA.z - eB.z;
        else if( rA.z < -eB.z ) y -= rA.z - -eB.z;
       
        if( y < mD ){ mD = y; X = gLXnX( llList2Vector( EdgesZ, x ), <0,0,1>, rA ); }
    }
      
      
     t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
     if( mD < 0.000001 ) return <-1,-1,-1>;
    if(      X.x >  eB.x ) X.x = eB.x;
    else if( X.x < -eB.x ) X.x = -eB.x;
    if(      X.y >  eB.y ) X.y =  eB.y;
    else if( X.y < -eB.y ) X.y = -eB.y;
    if(      X.z >  eB.z ) X.z =  eB.z;
    else if( X.z < -eB.z ) X.z = -eB.z;
      
      
     X1 = O1 + D1*t;
     return Bo + ( X * Br );}
    X2 = X1 + CP(nD1,nD2);}
</source>


gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
    // Computes two closest points of two lines, vector and distance
!style="background-color: #d0d0ee" | Input
    vector nO1 = < O1*D1, O1*D2, 0>;
!style="background-color: #d0d0ee" | Description
    vector nO2 = < O2*D1, O2*D2, 0>;
|-
    vector nD1 = < D1*D1, O1*D2, 0>;
| vector A
    vector nD2 = < O2*D1, O2*D2, 0>;
| Origin of Point
   
|-
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
| vector Bo
   
| Origin of Box
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
|-
   
| vector Bs
    X1 = O1 + D1*t;
| Size of Box
    X2 = X1 + CP(nD1,nD2);
|-
    V1 = CP(nD1,nD2);
| rotation Br
    Z1 = llVecMag(V1);}
| Rotation of Box
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| vector gBXnEX(vector A, vector Bo, vector Bs, rotation Br)
| Returns nearest point on edge of box B closest to point A. Returns <-1,-1,-1> if already on closest point.
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| float gLXdZ(vector O,vector D,vector A)
|-
|style="background-color: #eed0d0" colspan="2"| vector gLXdV(vector O,vector D,vector A)
|-
|style="background-color: #eed0d0" colspan="2"| vector gLXnX(vector O,vector D,vector A)
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}


// PLANE //
=== <div style="font-size: 120%;">[[#Geometric Library|Cylinder]]</div> ===
float gPXdZ(vector Pn,float Pd,vector A){
    // Finds distance of a point from a plane
    return A * Pn + Pd;}


vector gPXdV(vector Pn,float Pd,vector A){
<!--############# CYLINDER AND POINT, INTERSECTION BOOLEAN #############-->
    // Finds vector that points from point to nearest on plane
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
    return -(Pn * A + Pd)*Pn;}
!style="color: #000000; background-color: #aaaaff;" height="20px"|


vector gPXnX(vector Pn,float Pd,vector A){
==== Cylinder and Point, Intersection Boolean ====
     // Finds closest point on plane given point
|-
     return A - (Pn * A + Pd) * Pn;}
|
Finds if there is an intersection of a Point and a Cylinder and returns boolean
<source lang="lsl2">
integer gCXx( vector A, vector O, rotation R, vector S ) {
     A = ( A - O ) / R;// Converts to local object frame
     return (llPow(A.x/S.x*2,2) + llPow(A.y/S.y*2,2)) <= 1. // Test radius
        && llFabs(A.z/S.z*2) <= 1.;// Test top/bottom
}
</source>


float gPRxZ(vector Pn,float Pd,vector O,vector D){
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
    // Finds distance to intersection of plane along ray
!style="background-color: #d0d0ee" | Input
    return -( ( (Pn*D)+(Pn*O) ) / (Pn*D) );}
!style="background-color: #d0d0ee" | Description
    //return -( (Pn*D)/(Pn*O+Pd) );}
|-
| vector A
| Origin of Point
|-
| vector Bo
| Origin of Cylinder
|-
| vector Bs
| Size of Cylinder
|-
| rotation Br
| Rotation of Cylinder
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| integer gCXx( vector A, vector O, rotation R, vector S )
| Returns boolean check of intersection of a point and a cylinder if there is one, otherwise FALSE
|}
<div style="float:left;font-size: 80%;">
'''3D'''</div>
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}


vector gPRdV(vector Pn,float Pd,vector O,vector D){
    // Finds distance vector along a ray to a plane
    return D * gPRxZ(Pn,Pd,O,D);}
    //return -( (Pn*D)/(Pn*O+Pd) )*D;}


vector gPRxX(vector Pn,float Pd,vector O,vector D){
    // Finds intersection point along a ray to a plane
    return O + gPRdV(Pn,Pd,O,D);}


vector gPLxX(vector Pn,float Pd,vector O,vector D){
=== <div style="font-size: 120%;">[[#Geometric Library|Polygon]]</div> ===
    // Finds interesection point of a line and a plane
    return O -( (Pn*D)/(Pn*O+Pd) )*D;}


vector oO;vector oD;
<!--############# Polygon and Point, Intersection Boolean #############-->
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
gPPxL(vector Pn,float Pd,vector Qn,float Qd){
!style="color: #000000; background-color: #aaaaff;" height="20px"|
    // Finds line of intersection of two planes
==== Polygon and Point, Intersection Boolean ====
    oD = CP(Pn,Qn)/llVecMag(CP(Pn,Qn));
|-
     vector Cross = CP(CP(Pn,Qn),Pn);
|
     vector Bleh = (-Pd*Pn);
Figures out if point is inside of polygon or otherwise.
    oO = Bleh - (Qn*Cross)/(Qn*Bleh+Qd)*Cross/llVecMag(Cross);}
<source lang="lsl2">
 
integer gCPXx( list CP, vector X )
gPRpR(vector Pn,float Pd,vector O,vector D){
{//Copyright (c) 1970-2003, Wm. Randolph Franklin; 2008, Strife Onizuka
    // Projects a ray onto a plane
     integer i = ~(CP != []);
    oO = O - (Pn * O + Pd) * Pn;
     integer c = 0;
    vector t = llVecNorm( D - Project3D(D,Pn) );t = <1.0/t.x,1.0/t.y,1.0/t.z>;
    if(i < -2){
    oD = CP(Pn,t);}
        vector vi = llList2Vector(CP, -1);
 
        do {
// SPHERE //
            vector vj = vi;
vector gSRxX(vector Sp, float Sr, vector Ro, vector Rd){
            vi = llList2Vector(CP, i);
    float t;Ro = Ro - Sp;
            if((vi.y > X.y) ^ (vj.y > X.y)){
    //vector RayOrg = llDetectedPos(x) - llGetPos();
                if(vj.y != vi.y)
    if(Rd == ZERO_VECTOR) return ZERO_VECTOR;
                    c = c ^ (X.x < (((vj.x - vi.x) * (X.y - vi.y) / (vj.y - vi.y)) + vi.x));
   
                else c = c ^ (0 < ((vj.x-vi.x) * (X.y-vi.y)));
    float a = Rd * Rd;
            }
    float b = 2 * Rd * Ro;
         } while (++i);
    float c = (Ro * Ro) - (Sr * Sr);
   
    float disc = b * b - 4 * a * c;
   
    if(disc < 0) return ZERO_VECTOR;
   
    float distSqrt = llSqrt(disc);
    float q;
   
    if(b < 0)
        q = (-b - distSqrt)/2.0;
    else
         q = (-b + distSqrt)/2.0;
   
    float t0 = q / a;
    float t1 = c / q;
   
    if(t0 > t1){
        float temp = t0;
        t0 = t1;
        t1 = temp;
     }
     }
      
     return c;
    if(t1 < 0) return ZERO_VECTOR;
   
    if(t0 < 0)
        t = t1;
    else
        t = t0;
   
    return Ro + (t * Rd);
}
}
</source>


integer gSRx(vector Sp, float Sr, vector Ro, vector Rd){
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
    float t;Ro = Ro - Sp;
!style="background-color: #d0d0ee" | Input
    //vector RayOrg = llDetectedPos(x) - llGetPos();
!style="background-color: #d0d0ee" | Description
    if(Rd == ZERO_VECTOR) return FALSE;
|-
   
| list CP
    float a = Rd * Rd;
| Vertices of Concave Polygon
    float b = 2 * Rd * Ro;
|-
    float c = (Ro * Ro) - (Sr * Sr);
| vector X
   
| Origin of Point
    float disc = b * b - 4 * a * c;
|-
   
!style="background-color: #d0d0ee" | Output
    if(disc < 0) return FALSE;
!style="background-color: #d0d0ee" | Description
    return TRUE;
|-
}
| integer gCPXx( list CP, vector X )
| Returns TRUE if point (X) intersects concave polygon (CP), otherwise FALSE
|}
<div style="float:left;font-size: 80%;">
'''2D'''</div>
<div style="float:right;font-size: 80%;">
Copyright (c) 1970-2003, Wm. Randolph Franklin (Must accept [[#.231|License #1]]), LSL-Port By Strife Onizuka</div>
|}


// RAY //
<!--############# Polygon and Line Segment, Intersection Boolean #############-->
float gRXpZ(vector O,vector D,vector A){
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
    // Finds projected distance of a point along a ray
!style="color: #000000; background-color: #aaaaff;" height="20px"|
    return (A-O)*D;}
==== Polygon and Line Segment, Intersection Boolean ====
 
|-
vector gRZiX(vector O,vector D,float z){
|
    return O+z*D;}
Figures out if line segment intersects with polygon.
 
<source lang="lsl2">
// OTHER //
integer gVPLSx( vector P0, vector P1, list VP ){
vector pN;float pD;
     //Copyright 2001, softSurfer (www.softsurfer.com); 2008, Nexii Malthus
gTiP(vector p1,vector p2,vector p3){
     if( P0 == P1 ) return gCPXx( VP, P0 );
    // Turns three vector points in space into a plane
     float tE = 0; float tL = 1;
    pN = llVecNorm( CP((p2-p1),(p3-p1)) );
     float t; float N; float D;
    pD = -p1*pN;}
     vector dS = P1 - P0;
 
     vector e; integer x; integer y = VP!=[];
integer gTXcC(vector p1,vector p2,vector p3,vector x){
     @start;
    // Can be used to check weather a point is inside a triangle
     for( x = 0; x < y; ++x ){
    gTiP(p1,p2,p3);
        e = llList2Vector( VP, x+1 ) - llList2Vector( VP, x );
    vector Vn;vector En;
        N = Perp( e, P0 - llList2Vector( VP, x ) );
        Vn = p1 - x;
        D = -Perp( e, dS );
        En = CP((p2-p1),pN);
        if( llFabs(D) < 0.00000001 )
    if( ((p1-x)*CP(p2-p1,pN) >= 0)&&((p2-x)*CP(p3-p2,pN) >= 0)&&((p3-x)*CP(p1-p3,pN) >= 0) ) return TRUE;
            if( N < 0 ) return FALSE;
    return FALSE;}
            else jump start;
 
         t = N / D;
integer gTVXcC(vector p1,vector p2,vector p3,vector v,vector x){
         if( D < 0 ){
    if( ((p1-x)*CP(p2-p1,v) >= 0)&&((p2-x)*CP(p3-p2,v) >= 0)&&((p3-x)*CP(p1-p3,v) >= 0) ) return TRUE;
            if( t > tE ){   tE = t; if( tE > tL ) return FALSE;    }
    return FALSE;}
         } else {
 
            if( t < tL ){   tL = t; if( tL < tE ) return FALSE;    }
integer gTRcC(vector p1,vector p2,vector p3,vector O,vector D){
     }  }
    return gTVXcC(p1,p2,p3,O,D);}
     // PointOfEntrance = P0 + tE * dS;
 
     // PointOfExit = P0 + tL * dS;
default{state_entry(){}}
 
</lsl>
 
=="Clean"==
I was informed by Strife that shorthand one/two letter variable names is out of fashion these days, so it's up to the opinion of the reader if they really think this is cleaner and can now choose from two versions.
 
<lsl>
//===================================================//
//              Geometric Library 1.0              //
//              "May 4 2008", "2:24:30"             //
//  Copyright (C) 2008, Nexii Malthus (cc-by)        //
//    http://creativecommons.org/licenses/by/3.0/    //
//===================================================//
 
vector CrossProduct(vector Point1,vector Point2){
    return Point1 % Point2;}
 
vector Project3D(vector A,vector B){
    vector proj;
    proj.x = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.x;
    proj.y = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.y;
    proj.z = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.z;
    return proj;}
 
// POINT
float gPoint2Point_Distance_Float(vector Point1,vector Point2){
     // Distance P2P.
    return llVecDist(Point1,Point2);}
 
vector gPoint2Point_Distance_Vector(vector Point1,vector Point2){
    // Vector to move from P2P.
    return Point1-Point2;}
 
// LINE
vector gLine2Point_Distance_Vector(vector LineOrigin,vector LineDirection,vector Point){
    // Calculates the vector from a point to the closest point on a line
    return (LineOrigin-Point)-((LineOrigin-Point)*LineDirection)*LineDirection;}
 
float gLine2Point_Distance_Float(vector LineOrigin,vector LineDirection,vector Point){
     // Calculates distance of this vector, but faster on it's own
    return llSqrt(CrossProduct((Point-LineOrigin),LineDirection)*CrossProduct((Point-LineOrigin),LineDirection));}
 
vector gLine2Line_Distance_Vector(vector LineOrigin1,vector LineDirection1,vector LineOrigin2,vector LineDirection2){
    // Shortest vector of two lines
    return Project3D( (LineOrigin2-LineOrigin1), CrossProduct(LineDirection1,LineDirection2) );}
 
float gLine2Line_Distance_Float(vector LineOrigin1,vector LineDirection1,vector LineOrigin2,vector LineDirection2){
    // Returns the distance between two lines
    vector A = CrossProduct(LineDirection1,LineDirection2);float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>;
    return (LineOrigin2-LineOrigin1) * A;}
 
vector gLine2Line_Nearest_Point(vector LineOrigin1,vector LineDirection1,vector LineOrigin2,vector LineDirection2){
    // Closest point of two lines
    vector nearestOrigin1 = < LineOrigin1*LineDirection1, LineOrigin1*LineDirection2, 0>;
     vector nearestOrigin2 = < LineOrigin2*LineDirection1, LineOrigin2*LineDirection2, 0>;
    vector nearestDirection1 = < LineDirection1*LineDirection1, LineOrigin1*LineDirection2, 0>;
    vector nearestDirection2 = < LineOrigin2*LineDirection1, LineOrigin2*LineDirection2, 0>;
   
     float t = ( nearestDirection2.x*nearestDirection1.y - nearestDirection1.x*nearestDirection2.y );
   
    t = ( nearestDirection2.y*(nearestOrigin1.x-nearestOrigin2.x) - nearestDirection2.x*(nearestOrigin1.y-nearestOrigin2.y) ) / t;
   
    return LineOrigin1 + LineDirection1*t;}
 
vector outPoint1;vector outPoint2;vector outVector1;float outFloat1;
 
gLine2Line_Nearest_Two_Points(vector LineOrigin1,vector LineDirection1,vector LineOrigin2,vector LineDirection2){
    // Two closest points of two lines
     vector nearestOrigin1 = < LineOrigin1*LineDirection1, LineOrigin1*LineDirection2, 0>;
     vector nearestOrigin2 = < LineOrigin2*LineDirection1, LineOrigin2*LineDirection2, 0>;
    vector nearestDirection1 = < LineDirection1*LineDirection1, LineOrigin1*LineDirection2, 0>;
    vector nearestDirection2 = < LineOrigin2*LineDirection1, LineOrigin2*LineDirection2, 0>;
   
    float t = ( nearestDirection2.x*nearestDirection1.y - nearestDirection1.x*nearestDirection2.y );
   
    t = ( nearestDirection2.y*(nearestOrigin1.x-nearestOrigin2.x) - nearestDirection2.x*(nearestOrigin1.y-nearestOrigin2.y) ) / t;
   
    outPoint1 = LineOrigin1 + LineDirection1*t;
    outPoint2 = outPoint1 + CrossProduct(nearestDirection1,nearestDirection2);}
 
gLine2Line_Nearest_Two_Points_Vector_Distance(vector LineOrigin1,vector LineDirection1,vector LineOrigin2,vector LineDirection2){
    // Computes two closest points of two lines, vector and distance
     vector nearestOrigin1 = < LineOrigin1*LineDirection1, LineOrigin1*LineDirection2, 0>;
     vector nearestOrigin2 = < LineOrigin2*LineDirection1, LineOrigin2*LineDirection2, 0>;
    vector nearestDirection1 = < LineDirection1*LineDirection1, LineOrigin1*LineDirection2, 0>;
    vector nearestDirection2 = < LineOrigin2*LineDirection1, LineOrigin2*LineDirection2, 0>;
   
    float t = ( nearestDirection2.x*nearestDirection1.y - nearestDirection1.x*nearestDirection2.y );
   
    t = ( nearestDirection2.y*(nearestOrigin1.x-nearestOrigin2.x) - nearestDirection2.x*(nearestOrigin1.y-nearestOrigin2.y) ) / t;
   
    outPoint1 = LineOrigin1 + LineDirection1*t;
    outPoint2 = outPoint1 + CrossProduct(nearestDirection1,nearestDirection2);
    outVector1 = CrossProduct(nearestDirection1,nearestDirection2);
    outFloat1 = llVecMag(outVector1);}
 
// PLANE
float gPlane2Point_Distance_Float(vector PlaneNormal,float PlaneDistance,vector A){
    // Finds distance of a point from a plane
    return A * PlaneNormal + PlaneDistance;}
 
vector gPlane2Point_Distance_Vector(vector PlaneNormal,float PlaneDistance,vector A){
    // Finds vector that points from point to nearest on plane
    return -(PlaneNormal * A + PlaneDistance)*PlaneNormal;}
 
vector gPlane2Point_Nearest_Point(vector PlaneNormal,float PlaneDistance,vector A){
    // Finds closest point on plane given point
    return A - (PlaneNormal * A + PlaneDistance) * PlaneNormal;}
 
float gPlane2Ray_Nearest_Float(vector PlaneNormal,float PlaneDistance,vector RayOrigin,vector RayDirection){
    // Finds distance to intersection of plane along ray
    return -( ( (PlaneNormal*RayDirection)+(PlaneNormal*RayOrigin) ) / (PlaneNormal*RayDirection) );}
 
vector gPlane2Ray_Distance_Vector(vector PlaneNormal,float PlaneDistance,vector RayOrigin,vector RayDirection){
    // Finds distance vector along a ray to a plane
    return RayDirection * gPlane2Ray_Nearest_Float(PlaneNormal,PlaneDistance,RayOrigin,RayDirection);}
 
vector gPlane2Ray_Intersection_Point(vector PlaneNormal,float PlaneDistance,vector RayOrigin,vector RayDirection){
    // Finds intersection point along a ray to a plane
    return RayOrigin + gPlane2Ray_Distance_Vector(PlaneNormal,PlaneDistance,RayOrigin,RayDirection);}
 
vector gPlane2Line_Intersection_Point(vector PlaneNormal,float PlaneDistance,vector LineOrigin,vector LineDirection){
    // Finds interesection point of a line and a plane
    return LineOrigin -( (PlaneNormal*LineDirection)/(PlaneNormal*LineOrigin+PlaneDistance) )*LineDirection;}
 
vector outLineOrigin;vector outLineDirection;
 
gPlane2Plane_Intersection_Line(vector PlaneA_Normal,float PlaneA_Distance,vector PlaneB_Normal,float PlaneB_Distance){
    // Finds line of intersection of two planes
    outLineDirection = CrossProduct(PlaneA_Normal,PlaneB_Normal)/llVecMag(CrossProduct(PlaneA_Normal,PlaneB_Normal));
    vector Cross = CrossProduct(CrossProduct(PlaneA_Normal,PlaneB_Normal),PlaneA_Normal);
    vector Bleh = (-PlaneA_Distance*PlaneA_Normal);
    outLineOrigin = Bleh - (PlaneB_Normal*Cross)/(PlaneB_Normal*Bleh+PlaneB_Distance)*Cross/llVecMag(Cross);}
 
float gRay2Point_Project_Float(vector RayOrigin,vector RayDirection,vector Projection){
    // Finds projected distance of a point along a ray
    return (Projection-RayOrigin)*RayDirection;}
 
gPlane2Ray_Project_Ray(vector PlaneNormal,float PlaneDistance,vector RayOrigin,vector RayDirection){
    // Projects a ray onto a plane
    outLineOrigin = RayOrigin - (PlaneNormal * RayOrigin + PlaneDistance) * PlaneNormal;
    vector t = llVecNorm( RayDirection - Project3D(RayDirection,PlaneNormal) );t = <1.0/t.x,1.0/t.y,1.0/t.z>;
    outLineDirection = CrossProduct(PlaneNormal,t);}
 
// SPHERE
vector gSphere2Ray_Intersection_Point(vector SphereOrigin, float SphereRadius, vector RayOrigin, vector RayDirection){
    float t;RayOrigin = RayOrigin - SphereOrigin;
    if(RayDirection == ZERO_VECTOR) return ZERO_VECTOR;
   
    float a = RayDirection * RayDirection;
    float b = 2 * RayDirection * RayOrigin;
    float c = (RayOrigin * RayOrigin)  - (SphereRadius * SphereRadius);
   
    float disc = b * b - 4 * a * c;
   
    if(disc < 0) return ZERO_VECTOR;
   
    float distSqrt = llSqrt(disc);
    float q;
   
    if(b < 0)
         q = (-b - distSqrt)/2.0;
    else
         q = (-b + distSqrt)/2.0;
   
    float t0 = q / a;
    float t1 = c / q;
   
    if(t0 > t1){
        float temp = t0;
        t0 = t1;
        t1 = temp;
    }
   
    if(t1 < 0) return ZERO_VECTOR;
      
    if(t0 < 0)
         t = t1;
    else
        t = t0;
   
    return RayOrigin + (t * RayDirection);
}
 
integer gSphere2Ray_Intersection(vector SphereOrigin, float SphereRadius, vector RayOrigin, vector RayDirection){
    // IS there a intersection?
   
    float t;RayOrigin = RayOrigin - SphereOrigin;
    if(RayDirection == ZERO_VECTOR) return FALSE;
      
     float a = RayDirection * RayDirection;
     float b = 2 * RayDirection * RayOrigin;
     float c = (RayOrigin * RayOrigin)  - (SphereRadius * SphereRadius);
   
    float disc = b * b - 4 * a * c;
   
    if(disc < 0) return FALSE;
     return TRUE;
     return TRUE;
}
}
</source>


// Other
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
vector planeNormal;float planeDistance;
!style="background-color: #d0d0ee" | Input
gTriangle_into_Plane(vector p1,vector p2,vector p3){
!style="background-color: #d0d0ee" | Description
    planeNormal = llVecNorm( CrossProduct((p2-p1),(p3-p1)) );
|-
    planeDistance = -p1*planeNormal;}
| list VP
| Vertices of Convex Polygon
|-
| vector P0, vector P1
| Start and End of Line Segment
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| integer gVPLSx( vector P0, vector P1, list VP )
| Returns TRUE if line segment (P0,P1) intersects convex polygon (VP), otherwise FALSE
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" | <source lang="lsl2">float Perp( vector U, vector V ){ return U.x*V.y - U.y*V.x; }</source>
| Perpendicular dot product
|-
|style="background-color: #eed0d0" | integer gCPXx( list CP, vector X )
| Only needed for (P0 == P1) safety catch check, so optional
|}
<div style="float:left;font-size: 80%;">
'''2D'''</div>
<div style="float:right;font-size: 80%;">
Copyright 2001, softSurfer (www.softsurfer.com) (Must accept [[#.232|License #2]]), LSL-Port By Nexii Malthus</div>
|}


integer gTriangle2Point_CheckCollision(vector p1,vector p2,vector p3,vector x){
=== <div style="font-size: 120%;">[[#Geometric Library|Other Functions]]</div> ===
    gTriangle_into_Plane(p1,p2,p3);
    vector Vn;vector En;
        Vn = p1 - x;
        En = CrossProduct((p2-p1),planeNormal);
    if( ((p1-x)*CrossProduct(p2-p1,planeNormal) >= 0)&&((p2-x)*CrossProduct(p3-p2,planeNormal) >= 0)&&((p3-x)*CrossProduct(p1-p3,planeNormal) >= 0) ) return TRUE;
    return FALSE;}


integer gTVXcC(vector p1,vector p2,vector p3,vector v,vector x){
<!--############# 3D PROJECTION #############-->
    if( ((p1-x)*CrossProduct(p2-p1,v) >= 0)&&((p2-x)*CrossProduct(p3-p2,v) >= 0)&&((p3-x)*CrossProduct(p1-p3,v) >= 0) ) return TRUE;
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
    return FALSE;}
!style="color: #000000; background-color: #aaaaff;" height="20px"|
 
==== 3D Projection ====
integer gTriangel2Ray_CheckCollision(vector p1,vector p2,vector p3,vector Origin,vector Direction){
|-
    return gTVXcC(p1,p2,p3,Origin,Direction);}
|
 
Projects a vector A by vector B.
vector gRay2Float_into_Point(vector Origin,vector Direction,float Float){
<source lang="lsl2">
    return Origin+Float*Direction;}
 
default{state_entry(){}}
 
</lsl>
 
 
==Optimized ESL==
<lsl>//===================================================//
//    Geometric Library 1.0 Optimized ESL Build 1    //
//            "May  4 2008", "14:19:37"            //
//    Copyright (C) 2008, Nexii Malthus (cc-by)    //
//    Copyright (C) 2008, Strife Onizuka (cc-by)    //
//    http://creativecommons.org/licenses/by/3.0/    //
//===================================================//
 
#define CP(A, B) ((A) % (B))
#ifndef CP
vector CP(vector A,vector B){
    return A % B;}
#endif
 
#if 0
vector Project3D(vector A,vector B){
    vector proj;
    proj.x = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.x;
    proj.y = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.y;
    proj.z = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.z;
    return proj;}
#else
vector Project3D(vector A,vector B){
vector Project3D(vector A,vector B){
     return B * ((A*B) / (B*B));}
     return B * ( ( A * B ) / ( B * B ) );}
#endif
</source>


// POINT
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
#define gXXdZ llVecDist
!style="background-color: #d0d0ee" | Input
#ifndef gXXdZ
!style="background-color: #d0d0ee" | Description
float gXXdZ(vector A,vector B){
|-
    // Distance P2P.
| vector A
    return llVecDist(A,B);}
| First Vector
#endif
|-
| vector B
#define gXXdV(A,B) ((A) - (B))
| Second Vector
#ifndef gXXdV
|-
vector gXXdV(vector A,vector B){
!style="background-color: #d0d0ee" | Output
    // Vector to move from P2P.
!style="background-color: #d0d0ee" | Description
    return A-B;}
|-
#endif
| return Project3D(vector A, vector B)
| Returns result of projection
// LINE
|}
#if 0
<div style="float:left;font-size: 80%;">
vector gLXdV(vector O,vector D,vector A){
'''3D'''</div>
    // Calculates the vector from a point to the closest point on a line
<div style="float:right;font-size: 80%;">
    return (O-A)-((O-A)*D)*D;}
By Nexii Malthus</div>
#else
|}
vector gLXdV(vector O,vector D,vector A){
    // Calculates the vector from a point to the closest point on a line
    vector t = (O-A);
    return t - (t*D)*D;}
#endif
#define gLXdZ(O, D, A) llVecMag(CP((A-O),D))
#ifndef gLXdZ
float gLXdZ(vector O,vector D,vector A){
    // Calculates distance of this vector, but faster on it's own
    return llVecMag(CP((A-O),D));}
#endif
#define gLLdV(O, D, A) Project3D( (O2-O1), CP(D1,D2) )
#ifndef gLLdV
vector gLLdV(vector O1,vector D1,vector O2,vector D2){
    // Shortest vector of two lines
    return Project3D( (O2-O1), CP(D1,D2) );}
#endif


#define gLLdZ(O1, D1, O2, D2) ((O2-O1) * llVecNorm(CP(D1,D2)))
<!--############# REFLECTION #############-->
#ifndef gLLdZ
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
float gLLdZ(vector O1,vector D1,vector O2,vector D2){
!style="color: #000000; background-color: #aaaaff;" height="20px"|
     // Returns the distance between two lines
==== Reflection ====
    vector A = CP(D1,D2);float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>;
|-
    return (O2-O1) * A;}
|
#endif
Reflects Ray R with surface normal N
<source lang="lsl2">
vector Reflect(vector R,vector N){
     return R - 2 * N * ( R * N );}
</source>


#if 0
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
vector gLLnX(vector O1,vector D1,vector O2,vector D2){
!style="background-color: #d0d0ee" | Input
    // Closest point of two lines
!style="background-color: #d0d0ee" | Description
    vector nO1 = < O1*D1, O1*D2, 0>;
|-
    vector nO2 = < O2*D1, O2*D2, 0>;
| vector R
    vector nD1 = < D1*D1, O1*D2, 0>;
| Ray Normal
    vector nD2 = < O2*D1, O2*D2, 0>;
|-
| vector N
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
| Surface Normal
|-
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
    return O1 + D1*t;}
|-
#else
| return Reflect(vector R, vector N)
vector gLLnX(vector O1,vector D1,vector O2,vector D2){
| Returns result of reflection
    // Closest point of two lines
|}
    vector t = <O2*D1, O1*D2, O2*D2>;
<div style="float:left;font-size: 80%;">
   
'''3D'''</div>
    return O1 + D1 * (( t.z * ((O1*D1)-t.x) - t.x * (t.y-t.z) ) / ( t.x*t.y - (D1*D1)*t.z ));}
<div style="float:right;font-size: 80%;">
#endif
By Nexii Malthus</div>
|}
vector X1;vector X2;vector V1;float Z1;


#if 0
=== <div style="font-size: 120%;">[[#Geometric Library|Glossary]]</div> ===
gLLnnXX(vector O1,vector D1,vector O2,vector D2){
For anyone curious to the shorthand used and who wish to use a lookup table can use this as a reference. Or anyone who wishes to add a new function to the library is welcome to but it would be recommended to keep consistency.
    // Two closest points of two lines
I tried to minimize the script function names to be easily readable. All the geometric function names start with a g.
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
    X1 = O1 + D1*t;
    X2 = X1 + CP(nD1,nD2);}
#else
gLLnnXX(vector O1,vector D1,vector O2,vector D2){
    // Two closest points of two lines
    #define a (O1*D1)
    #define c (D1*D1)
    #define d (O2*D1)
    #define e (O1*D2)
    #define f (O2*D2)
    vector nD1 = < c, e, 0.0>;
    vector nD2 = < d, f, 0.0>;
    #undef c
    #undef d
    #undef e
    #undef f
    #define c nD1.x
    #define d nD2.x
    #define e nD1.y
    #define f nD2.y
    X2 = (X1 = (O1 + D1 * (( d*(a-d) - d*(e-f) ) / ( d*e - c*f )))) + CP(nD1,nD2);}
    #undef a
    #undef c
    #undef d
    #undef e
    #undef f
#endif


#if 0
''g'' ('''Shape1''') ('''Shape2''') ('''Process''') ('''Return''' (''Only needed if other than integer''))
gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){
    // Computes two closest points of two lines, vector and distance
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
    X1 = O1 + D1*t;
    X2 = X1 + CP(nD1,nD2);
    V1 = CP(nD1,nD2);
    Z1 = llVecMag(V1);}
#else
gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){
    // Computes two closest points of two lines, vector and distance
    #define a (O1*D1)
    #define c (D1*D1)
    #define d (O2*D1)
    #define e (O1*D2)
    #define f (O2*D2)
    vector nD1 = < c, e, 0.0>;
    vector nD2 = < d, f, 0.0>;
    #undef c
    #undef d
    #undef e
    #undef f
    #define c nD1.x
    #define d nD2.x
    #define e nD1.y
    #define f nD2.y
    X2 = (X1 = (O1 + D1 * (( f * (a-d) - d * (e-f) ) / ( d*e - c*f )))) + (V1 = (CP(nD1,nD2)));
    Z1 = llVecMag(V1);}
    #undef a
    #undef c
    #undef d
    #undef e
    #undef f
#endif


// PLANE
Here is the legend:
#define gPXdZ(Pn, Pd, A) ((A) * (Pn) + (Pd))
#ifndef gPXdZ
float gPXdZ(vector Pn,float Pd,vector A){
    // Finds distance of a point from a plane
    return A * Pn + Pd;}
#endif


vector gPXdV(vector Pn,float Pd,vector A){
{| class="sortable" {{Prettytable}}
    // Finds vector that points from point to nearest on plane
|-{{Hl2}}
    return -(Pn * A + Pd)*Pn;}
! '''Shorthand'''
 
! '''Name'''
vector gPXnX(vector Pn,float Pd,vector A){
! class="unsortable" | '''Description'''
    // Finds closest point on plane given point
|-
    return A - (Pn * A + Pd) * Pn;}
! colspan="3" height="50%" | Geometric Types, all the shapes in the library
|-
||X
||Point
||vector defining a point in space
|-
||L
||'''L'''ine
||A line has an origin and a direction and is infinitely long
|-
||LS
||'''L'''ine '''S'''egment
||A line segment is a finite line and therefore consists of a start and end position
|-
||R
||'''R'''ay
||A ray is like a line, except it is more distinct as it defines wether it points forward or back
|-
||P
||'''P'''lane
||A 2D doubly ruled surface of infinite size
|-
||S
||'''S'''phere
||A sphere is defined by origin and radius (No ellipsoid functions available yet)
|-
||B
||'''B'''ox
||A box primitive is six sided and defined by origin, size as well as a rotation.
|-
||C
||'''C'''ylinder
||An elliptic cylinder primitive .
|-
||VP
||Con'''v'''ex '''P'''olygon
||Convex Polygon defined by list of vertices.
|-
||CP
||Con'''c'''ave '''P'''olygon
||Concave Polygon defined by list of vertices. Automatic backward compatibility with Convex Polygons.
|-
! colspan="3" height="50%" | The Process, What does it do?
|-
||d
||'''d'''istance
||Calculate distance
|-
||n
||'''n'''earest
||Calculate nearest
|-
||p
||'''p'''roject
||Calculates projection
|-
||x
||Intersection
||Calculates intersection
|-
||dir
||'''dir'''ection
||Calculates direction
|-
! colspan="3" height="50%" | Return, What kind of data do I get out of it?
|-
||Z
||Float
||Represents that a float is returned
|-
||V
||'''V'''ector
||Represents that a vector is returned
|-
||O
||'''O'''rigin
||Represents the Origin of the ray or line
|-
||D
||'''D'''irection
||Direction from the Origin
|-
||E
||'''E'''dge
||Edge of a shape, such as an edge on a box, suffix may mark special case return type
|}


#if 0
=== <div style="font-size: 120%;">[[#Geometric Library|Licenses]]</div> ===
float gPRxZ(vector Pn,float Pd,vector O,vector D){
    // Finds distance to intersection of plane along ray
    return -( ( (Pn*D)+(Pn*O) ) / (Pn*D) );}
    //return -( (Pn*D)/(Pn*O+Pd) );}
#else
float gPRxZ(vector Pn,float Pd,vector O,vector D){
    // Finds distance to intersection of plane along ray
    float a = (Pn*D);
    return -( ( a+(Pn*O) ) / a );}
    //return -( (Pn*D)/(Pn*O+Pd) );}
#endif


vector gPRdV(vector Pn,float Pd,vector O,vector D){
==== #1 ====
    // Finds distance vector along a ray to a plane
<source lang="lsl2">
    return D * gPRxZ(Pn,Pd,O,D);}
//Copyright (c) 1970-2003, Wm. Randolph Franklin
    //return -( (Pn*D)/(Pn*O+Pd) )*D;}
//Copyright (c) 2008, Strife Onizuka (porting to LSL)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
// 1. Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimers.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice in the documentation and/or other materials provided with the
//    distribution.
// 3. The name of W. Randolph Franklin may not be used to endorse or promote
//    products derived from this Software without specific prior written
//   permission.
   
   
vector gPRxX(vector Pn,float Pd,vector O,vector D){
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    // Finds intersection point along a ray to a plane
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    return O + gPRdV(Pn,Pd,O,D);}
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
vector gPLxX(vector Pn,float Pd,vector O,vector D){
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    // Finds interesection point of a line and a plane
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    return O -( (Pn*D)/(Pn*O+Pd) )*D;}
//SOFTWARE.  
</source>
vector oO;vector oD;
 
#if 0
gPPxL(vector Pn,float Pd,vector Qn,float Qd){
    // Finds line of intersection of two planes
    oD = CP(Pn,Qn)/llVecMag(CP(Pn,Qn));
    vector Cross = CP(CP(Pn,Qn),Pn);
    vector Bleh = (-Pd*Pn);
    oO = Bleh - (Qn*Cross)/(Qn*Bleh+Qd)*Cross/llVecMag(Cross);}
#else
gPPxL(vector Pn,float Pd,vector Qn,float Qd){
    // Finds line of intersection of two planes
    vector a = CP(Pn,Qn);
    oD = llVecNorm(a);
    vector Cross = CP(a,Pn);
    vector Bleh = (-Pd*Pn);
    oO = Bleh - (Qn*Cross)/(Qn*Bleh+Qd)*llVecNorm(Cross);}
#endif
 
#define gRXpZ(O, D, A) (A-O)*D
#ifndef gRXpZ
float gRXpZ(vector O,vector D,vector A){
    // Finds projected distance of a point along a ray
    return (A-O)*D;}
#endif
#if 0
gPRpR(vector Pn,float Pd,vector O,vector D){
    // Projects a ray onto a plane
    oO = O - (Pn * O + Pd) * Pn;
    vector t = llVecNorm( D - Project3D(D,Pn) );t = <1.0/t.x,1.0/t.y,1.0/t.z>;
    oD = CP(Pn,t);}
#else
gPRpR(vector Pn,float Pd,vector O,vector D){
    // Projects a ray onto a plane
    oO = O - (Pn * O + Pd) * Pn;
    O = llVecNorm( D - Project3D(D,Pn) );
    oD = CP(Pn, (<1.0/O.x,1.0/O.y,1.0/O.z>));}
#endif
   
// SPHERE
#if 0
vector gSRxX(vector Sp, float Sr, vector Ro, vector Rd){
    float t;Ro = Ro - Sp;
    //vector RayOrg = llDetectedPos(x) - llGetPos();
    if(Rd == ZERO_VECTOR) return ZERO_VECTOR;
    float a = Rd * Rd;
    float b = 2 * Rd * Ro;
    float c = (Ro * Ro)  - (Sr * Sr);
    float disc = b * b - 4 * a * c;
    if(disc < 0) return ZERO_VECTOR;
    float distSqrt = llSqrt(disc);
    float q;
    if(b < 0)
        q = (-b - distSqrt)/2.0;
    else
        q = (-b + distSqrt)/2.0;
    float t0 = q / a;
    float t1 = c / q;
    if(t0 > t1){
        float temp = t0;
        t0 = t1;
        t1 = temp;
    }
    if(t1 < 0) return ZERO_VECTOR;
    if(t0 < 0)
        t = t1;
    else
        t = t0;
    return Ro + (t * Rd);
}
#else
vector gSRxX(vector Sp, float Sr, vector Ro, vector Rd){
    if(Rd)
    {
        Ro -= Sp;
        //vector RayOrg = llDetectedPos(x) - llGetPos();
        float a = Rd * Rd;
        float b = 2 * Rd * Ro;
        float c = (Ro * Ro)  - (Sr * Sr);
   
        float disc = b * b - 4 * a * c;
   
        if(disc >= 0)
        {
            float q = ((llSqrt(disc) * ~((b > 0) * -2)) - b) / 2.0;
            if(q)//avoid a divide by zero!
            {
                float t0 = q / a;
                float t1 = c / q;
           
                if(((t0 < t1) || (t1 < 0)) && (t0 >= 0))
                    return Ro + (t0 * Rd);
                if(t1 >= 0)
                    return Ro + (t1 * Rd);
            }
        }
    }
    return ZERO_VECTOR;
}
#endif
 
#if 0
integer gSRx(vector Sp, float Sr, vector Ro, vector Rd){
    float t;Ro = Ro - Sp;
    //vector RayOrg = llDetectedPos(x) - llGetPos();
    if(Rd == ZERO_VECTOR) return FALSE;
    float a = Rd * Rd;
    float b = 2 * Rd * Ro;
    float c = (Ro * Ro)  - (Sr * Sr);
    float disc = b * b - 4 * a * c;
    if(disc < 0) return FALSE;
    return TRUE;
}
#else
integer gSRx(vector Sp, float Sr, vector Ro, vector Rd){
    if(Rd)
    {
        Ro -= Sp;
        //vector RayOrg = llDetectedPos(x) - llGetPos();
        float a = Rd * Rd;
        float b = 2 * Rd * Ro;
        float c = (Ro * Ro)  - (Sr * Sr);
        return (b * b - 4 * a * c) >= 0;
    }
    return FALSE;
}
#endif
// Other
vector pN;float pD;
#if 0
gTiP(vector p1,vector p2,vector p3){
    // Turns three vector points in space into a plane
    pN = llVecNorm( CP((p2-p1),(p3-p1)) );
    pD = -p1*pN;}
#else
gTiP(vector p1,vector p2,vector p3){
    // Turns three vector points in space into a plane
    pD = -p1*(pN = llVecNorm( CP((p2-p1),(p3-p1)) ));}
#endif
   
#if 0
integer gTXcC(vector p1,vector p2,vector p3,vector x){
    gTiP(p1,p2,p3);
    vector Vn;vector En;
        Vn = p1 - x;
        En = CP((p2-p1),pN);
    if( ((p1-x)*CP(p2-p1,pN) >= 0)&&((p2-x)*CP(p3-p2,pN) >= 0)&&((p3-x)*CP(p1-p3,pN) >= 0) ) return TRUE;
    return FALSE;}
#else
integer gTXcC(vector p1,vector p2,vector p3,vector x){
    gTiP(p1,p2,p3);
    vector Vn = p1 - x;
    vector En = CP((p2-p1),pN);
    return( ((p1-x)*CP(p2-p1,pN) >= 0) && ((p2-x)*CP(p3-p2,pN) >= 0) && ((p3-x)*CP(p1-p3,pN) >= 0) );}
#endif
 
#if 0
integer gTVXcC(vector p1,vector p2,vector p3,vector v,vector x){
    if( ((p1-x)*CP(p2-p1,v) >= 0)&&((p2-x)*CP(p3-p2,v) >= 0)&&((p3-x)*CP(p1-p3,v) >= 0) ) return TRUE;
    return FALSE;}
#else
integer gTVXcC(vector p1,vector p2,vector p3,vector v,vector x){
    // Can be used to check weather a point is inside a triangle
    return ( ((p1-x)*CP(p2-p1,v) >= 0)&&((p2-x)*CP(p3-p2,v) >= 0)&&((p3-x)*CP(p1-p3,v) >= 0) );}
#endif
 
#define gTRcC gTVXcC
#ifndef gTRcC
integer gTRcC(vector p1,vector p2,vector p3,vector O,vector D){
    return gTVXcC(p1,p2,p3,O,D);}
#endif


#define gRZiX(O, D, z) ((O)+(D)*(z))
==== #2 ====
#ifndef gRZiX
<source lang="lsl2">
vector gRZiX(vector O,vector D,float z){
// Copyright 2001, softSurfer (www.softsurfer.com); 2008, LSL-port by Nexii Malthus
    return O+z*D;}
// This code may be freely used and modified for any purpose
#endif</lsl>
// providing that this copyright notice is included with it.
// SoftSurfer makes no warranty for this code, and cannot be held
// liable for any real or imagined damage resulting from its use.
// Users of this code must verify correctness for their application.
</source>

Latest revision as of 20:00, 24 January 2015

Please vote for: https://jira.secondlife.com/browse/WEB-235 So that I can expand each function into deeper detail without the page starting to fail in readability. --Nexii Malthus 23:05, 24 October 2008 (UTC)

:Break it up so each major section has its own page... thats why hypertext was invented. -Overbrain Unplugged 13:36, 10 October 2010 (UTC)

Geometric Library

Line and Point, Vector

Calculates the vector from a point 'to' the closest point on a line

vector gLXdV(vector O,vector D,vector A){
    return (O-A)-((O-A)*D)*D;}
Input Description
vector O Origin of Line
vector D Direction of Line
vector A Origin of Point
Output Description
return vector gLXdV Returns origin of closest point on Line to Point
3D
By Nexii Malthus

Line and Point, Distance

Calculates distance of line to point, same as measuring magnitude of Line and Point Vector, but faster on it's own

float gLXdZ(vector O,vector D,vector A){
    vector k = ( A - O ) % D;
    return llSqrt( k * k );}
Input Description
vector O Origin of Line
vector D Direction of Line (unit vector)
vector A Origin of Point
Output Description
return float gLXdZ Returns numerical distance from Line to Point
3D
By Nexii Malthus

Line Nearest Point, Nearest Point

Returns nearest point on line to given point

vector gLXnX(vector O,vector D,vector A){
    return gLXdV(O,D,A) + A;}
Input Description
vector O Origin of Line
vector D Direction of Line
vector A Origin of Point
Output Description
return vector gLXnX Returns nearest point on line given point
Requirement
function vector gLXdV(vector O,vector D,vector A)
3D
By Nexii Malthus

Line and Line, Vector

Shortest vector of two lines

vector gLLdV(vector O1,vector D1,vector O2,vector D2){
    vector A = O2 - O1; vector B = D1 % D2;
    return B*( (A*B)/(B*B) );}
Input Description
vector O1 Origin of Line 1
vector D1 Direction of Line 1
vector O2 Origin of Line 2
vector D2 Direction of Line 2
Output Description
return vector gLLdV Returns shortest vector between the two lines
3D
By Nexii Malthus

Line and Line, Distance

Returns the distance between two lines

float gLLdZ(vector O1,vector D1,vector O2,vector D2){
    vector A = D1%D2;float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>;
    return (O2-O1) * A;}
Input Description
vector O1 Origin of Line 1
vector D1 Direction of Line 1
vector O2 Origin of Line 2
vector D2 Direction of Line 2
Output Description
return float gLLdZ Returns numerical distance between the two lines
3D
By Nexii Malthus

Line and Line, Nearest point

Closest point of two lines

vector gLLnX(vector O1,vector D1,vector O2,vector D2){
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
    
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
    
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
    
    return O1 + D1*t;}
Input Description
vector O1 Origin of Line 1
vector D1 Direction of Line 1
vector O2 Origin of Line 2
vector D2 Direction of Line 2
Output Description
return vector gLLnX Returns closest point between the two lines
2D
By Nexii Malthus

Line and Line, intersection point

Computes intersection point of two lines, if there is any, else <-1,-1,-1> if none.

vector gLLxX( vector A, vector B, vector C, vector D ){
    vector b = B-A; vector d = D-C;
    float dotperp = b.x*d.y - b.y*d.x;
    if (dotperp == 0) return <-1,-1,-1>;
    vector c = C-A;
    float t = (c.x*d.y - c.y*d.x) / dotperp;
    return <A.x + t*b.x, A.y + t*b.y, 0>;}
Input Description
vector A Start of Line 1
vector B End of Line 1
vector C Start of Line 2
vector D End of Line 2
Output Description
return vector gLLxX The intersection point of the two lines, else <-1,-1,-1> if none
2D
By Nexii Malthus

Line and Line, two nearest points of lines

Two closest points of two lines on each line

vector X1;vector X2;
gLLnnXX(vector O1,vector D1,vector O2,vector D2){
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
    
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
    
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
    
    X1 = O1 + D1*t;
    X2 = X1 + nD1%nD2;}
Input Description
vector O1 Origin of Line 1
vector D1 Direction of Line 1
vector O2 Origin of Line 2
vector D2 Direction of Line 2
Output Description
vector X1 Closest point on line 1 to line 2
vector X2 Closest point on line 2 to line 1
Requirement
global vector X1
global vector X2
2D
By Nexii Malthus

Line and Line, nearest line

Input two lines, the function will return a list containing two vectors responding to the line nearest between them. As well as two floats corresponding to the scalar value on the two line of where the line has an end located at.

list gLLnL( vector v0, vector v1, vector v2, vector v3 ) {
    float Eps = 0.000001; vector vx; vector vy; vector va; vector vb; vector vc;
    float x; float y; float d0; float d1; float d2; float d3; float d4;
    va = v0-v2; vb = v3-v2; if(llVecMag(vb)<Eps) return [];
    vc = v1-v0; if(llVecMag(vc)<Eps) return [];
    d0 = va*vb; d1 = vb*vc; d2 = va*vc; d3 = vb*vb; d4 = vc*vc;
    float den = d4*d3-d1*d1; if( llFabs(den) < Eps ) return [];
    float num = d0*d1-d2*d3; x = num/den; y = (d0+d1*x)/d3;
    vx = v0+vc*x; vy = v2+vb*y; return [vx,vy,x,y]; }
Input Description
vector v0 Point on Line 1
vector v1 Point on Line 1
vector v2 Point on Line 2
vector v3 Point on Line 2
Output Description
[vx] Nearest point on Line 1 to Line 2
[vy] Nearest point on Line 2 to Line 1
[x] Scalar value representing location of vx on line 1 (range [v0,v1])
[y] Scalar value representing location of vy on line 2 (range [v2,v3])
3D
By Nexii Malthus

Line and Line Segments, nearest line segment

Input two line segments, the function will return a list containing two vectors responding to the line segment nearest between them. As well as two floats corresponding to the scalar value on the two line segments of where the line segment has an end located at.

list gLSLSnLS( vector v0, vector v1, vector v2, vector v3 ) {
    float Eps = 0.000001; vector vx; vector vy; vector va; vector vb; vector vc;
    float x; float y; float d0; float d1; float d2; float d3; float d4;
    va = v0-v2; vb = v3-v2; if(llVecMag(vb)<Eps) return [];
    vc = v1-v0; if(llVecMag(vc)<Eps) return [];
    if( llFabs(vc.x + vc.y + vc.z) < Eps ) return [];
    d0 = va*vb; d1 = vb*vc; d2 = va*vc; d3 = vb*vb; d4 = vc*vc;
    float den = d4*d3-d1*d1; if( llFabs(den) < Eps ) return [];
    float num = d0*d1-d2*d3; x = num/den; y = (d0+d1*x)/d3;
    if(x<0)x=0; else if(x>1)x=1; if(y<0)y=0; else if(y>1)y=1;
    vx = v0+vc*x; vy = v2+vb*y; return [vx,vy,x,y]; }
Input Description
vector v0 Start of Line Segment 1
vector v1 End of Line Segment 1
vector v2 Start of Line Segment 2
vector v3 End of Line Segment 2
Output Description
[vx] Nearest point on Line Segment 1 to Line Segment 2
[vy] Nearest point on Line Segment 2 to Line Segment 1
[x] Scalar value representing location of vx on Line Segment 1 (range [v0,v1])
[y] Scalar value representing location of vy on Line Segment 2 (range [v2,v3])
3D
By Nexii Malthus

Line and Line, two nearest points with vector and distance

Computes two closest points of two lines, vector and distance

vector X1;vector X2;vector V1;float Z1;
gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
    
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
    
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
    
    X1 = O1 + D1*t;
    X2 = X1 + CP(nD1,nD2);
    V1 = nD1%nD2;
    Z1 = llVecMag(V1);}
Input Description
vector O1 Origin of Line 1
vector D1 Direction of Line 1
vector O2 Origin of Line 2
vector D2 Direction of Line 2
Output Description
vector X1 Closest point on line 1 to line 2
vector X2 Closest point on line 2 to line 1
vector V1 Direction vector of line 1 to line 2
float Z1 Numerical distance of line 1 to line 2
Requirement
global vector X1
global vector X2
global vector V1
global float Z1
2D
By Nexii Malthus

Line and Point, Direction

Works out where point (X) is relative to the line of the segment (L0, L1).

float gLSPdir( vector L0, vector L1, vector X ){
    return (L1.x - L0.x)*(X.y - L0.y) - (X.x - L0.x)*(L1.y - L0.y);
}
Input Description
vector L0, vector L1 Start and End of line segment
vector X Origin of Point
Output Description
float isLeft( vector L0, vector L1, vector X ) Returns float, >0 is Left, 0 on Line, <0 is Right, according to line angle
2D
Copyright 2001, softSurfer (www.softsurfer.com) (Must accept License #2), LSL-Port By Nexii Malthus

Plane and Point, Distance

Finds distance of a point from a plane

float gPXdZ(vector Pn,float Pd,vector A){
    return A * Pn + Pd;}
Input Description
vector Pn Normal of Plane (unit vector)
float Pd Distance of Plane
vector A Origin of Point
Output Description
return float gPXdZ Returns Distance between plane and point
3D
By Nexii Malthus

Plane and Point, Vector

Finds vector that points from point to nearest on plane

vector gPXdV(vector Pn,float Pd,vector A){
    return -(Pn * A + Pd)*Pn;}
Input Description
vector Pn Normal of Plane (unit vector)
float Pd Distance of Plane
vector A Origin of Point
Output Description
return vector gPXdV Returns vector from point to closest point on plane
3D
By Nexii Malthus

Plane and Point, Nearest point

Finds closest point on plane given point

vector gPXnX(vector Pn,float Pd,vector A){
    return A - (Pn * A + Pd) * Pn;}
Input Description
vector Pn Normal of Plane (unit vector)
float Pd Distance of Plane
vector A Origin of Point
Output Description
return vector gPXnX Returns vector of a point from closest of point to plane
3D
By Nexii Malthus

Plane and Ray, Intersection Distance

Finds distance to intersection of plane along ray

float gPRxZ(vector Pn,float Pd,vector O,vector D){
    return -((Pn*O+Pd)/(Pn*D));}
Input Description
vector Pn Normal of Plane (unit vector)
float Pd Distance of Plane
vector O Origin of Ray
vector D Direction of Ray
Output Description
return float gPRxZ Returns float distance of intersection between ray and plane
3D
By Nexii Malthus

Plane and Ray, Vector

Finds distance vector along a ray to a plane

vector gPRdV(vector Pn,float Pd,vector O,vector D){
    return D * gPRxZ(Pn,Pd,O,D);}
Input Description
vector Pn Normal of Plane (unit vector)
float Pd Distance of Plane
vector O Origin of Ray
vector D Direction of Ray
Output Description
return vector gPRdV Returns vector along a ray to a plane
Requirement
function float gPRxZ(vector Pn,float Pd,vector O,vector D)
3D
By Nexii Malthus

Plane and Ray, Intersection Point

Finds intersection point along a ray to a plane

vector gPRxX(vector Pn,float Pd,vector O,vector D){
    return O + gPRdV(Pn,Pd,O,D);}
Input Description
vector Pn Normal of Plane (unit vector)
float Pd Distance of Plane
vector O Origin of Ray
vector D Direction of Ray
Output Description
return vector gPRxX Returns vector point of intersection between ray and plane
Requirement
function vector gPRdV(vector Pn,float Pd,vector O,vector D)
3D
By Nexii Malthus

Plane and Line, Intersection Point

Finds interesection point of a line and a plane

vector gPLxX(vector Pn,float Pd,vector O,vector D){
    return O + D*-( (Pn*O-Pd)/(Pn*D) );}
Input Description
vector Pn Normal of Plane (unit vector)
float Pd Distance of Plane
vector O Origin of Line
vector D Direction of Line
Output Description
return vector gPLxX Returns vector point of intersection between line and plane
3D
By Nexii Malthus

Plane and Plane, Intersection Line

Finds line of intersection of two planes

vector oO;vector oD;

gPPxL(vector Pn,float Pd,vector Qn,float Qd){
    oD = (Pn%Qn)/llVecMag(Pn%Qn);
    vector Cross = (Pn%Qn)%Pn;
    vector Bleh = (-Pd*Pn);
    oO = Bleh - (Qn*Cross)/(Qn*Bleh+Qd)*Cross/llVecMag(Cross);}
Input Description
vector Pn Normal of Plane 1 (unit vector)
float Pd Distance of Plane 1
vector Qn Normal of Plane 2 (unit vector)
float Qd Distance of Plane 2
Output Description
vector oO Intersection Line's origin
vector oD Intersection Line's direction
Requirement
global vector oO
global vector oD
3D
By Nexii Malthus

Plane and Ray, Projection

Projects a ray onto a plane

vector oO;vector oD;

gPRpR(vector Pn,float Pd,vector O,vector D){
    oO = O - (Pn * O + Pd) * Pn;
    vector t = llVecNorm( D - (Pn*((D*Pn)/(Pn*Pn))) );t = <1.0/t.x,1.0/t.y,1.0/t.z>;
    oD = Pn%t;}
Input Description
vector Pn Normal of Plane (unit vector)
float Pd Distance of Plane
vector O Origin of Ray
vector D Direction of Ray
Output Description
vector oO Projected Ray Origin
vector oD Projected Ray Direction
Requirement
global vector oO
global vector oD
3D
By Nexii Malthus

Sphere and Ray, Intersection Point

Finds intersection point of sphere and ray

vector gSRxX(vector Sp, float Sr, vector Ro, vector Rd){
    float t; Ro -= Sp;
    if(Rd == ZERO_VECTOR) return ZERO_VECTOR;
    
    float a = Rd * Rd;
    float b = 2 * Rd * Ro;
    float c = (Ro * Ro)  - (Sr * Sr);
    
    float disc = b * b - 4 * a * c;
    
    if(disc < 0) return ZERO_VECTOR;
    
    float distSqrt = llSqrt(disc);
    float q;
    
    if(b < 0)
        q = (-b - distSqrt)/2.0;
    else 
        q = (-b + distSqrt)/2.0;
    
    float t0 = q / a;
    float t1 = c / q;
    
    if(t0 > t1){
        float temp = t0;
        t0 = t1;
        t1 = temp;
    }
    
    if(t1 < 0) return ZERO_VECTOR;
    
    if(t0 < 0)
        t = t1;
    else
        t = t0;
    
    return Sp + Ro + (t * Rd);
}
Input Description
vector Sp Origin of Sphere
float Sr Radius of Sphere
vector Ro Origin of Ray
vector Rd Direction of Ray
Output Description
vector gSRxX Returns intersection point of sphere and ray otherwise ZERO_VECTOR
3D
By Nexii Malthus

Sphere and Ray, Intersection Boolean

Finds if there is a intersection of sphere and ray

integer gSRx(vector Sp, float Sr, vector Ro, vector Rd){
    float t;Ro = Ro - Sp;
    //vector RayOrg = llDetectedPos(x) - llGetPos();
    if(Rd == ZERO_VECTOR) return FALSE;
    
    float a = Rd * Rd;
    float b = 2 * Rd * Ro;
    float c = (Ro * Ro)  - (Sr * Sr);
    
    float disc = b * b - 4 * a * c;
    
    if(disc < 0) return FALSE;
    return TRUE;
}
Input Description
vector Sp Origin of Sphere
float Sr Radius of Sphere
vector Ro Origin of Ray
vector Rd Direction of Ray
Output Description
integer gSRx Returns a boolean indicating if there is a valid intersection
3D
By Nexii Malthus

Ray and Point, projected distance

Finds projected distance of a point along a ray

float gRXpZ(vector O,vector D,vector A){
    return (A-O)*D;}
Input Description
vector O Origin of Ray
vector D Direction of Ray
vector A Origin of Point
Output Description
float gRXpZ Returns projected distance of a point along a ray
3D
By Nexii Malthus

Box and Ray, Intersection Distance

Finds intersection of a Ray to a Box and returns intersection distance, otherwise -1 if there is no legal intersection.

float gBRxZ(vector Ro,vector Rd, vector Bo, vector Bs, rotation Br){
    vector oB = (Ro-Bo)/Br;    vector dB = Rd/Br;    vector eB = 0.5*Bs;
    float mD = -1.0;    float D;    vector X;
    
    if(llFabs(dB.x) > 0.000001){
        D = (-eB.x - oB.x ) / dB.x;
        if(D >= 0.0){
            X = oB + D * dB;
            if(X.y >= -eB.y && X.y <= eB.y && X.z >= -eB.z && X.z <= eB.z)
                mD = D;
        }
        D = ( eB.x - oB.x ) / dB.x;
        if (D >= 0.0){
            X = oB + D * dB;
            if(X.y >= -eB.y && X.y <= eB.y && X.z >= -eB.z && X.z <= eB.z) 
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
    }
    
    if(llFabs(dB.y) > 0.000001){
        D = (-eB.y - oB.y ) / dB.y;
        if(D >= 0.0){
            X = oB + D * dB;
            if(X.x >= -eB.x && X.x <= eB.x && X.z >= -eB.z && X.z <= eB.z)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
        D = ( eB.y - oB.y ) / dB.y;
        if (D >= 0.0){
            X = oB + D * dB;
            if(X.x >= -eB.x && X.x <= eB.x && X.z >= -eB.z && X.z <= eB.z)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
    }
    
    if(llFabs(dB.z) > 0.000001){
        D = (-eB.z - oB.z ) / dB.z;
        if(D >= 0.0){
            X = oB + D * dB;
            if(X.x >= -eB.x && X.x <= eB.x && X.y >= -eB.y && X.y <= eB.y)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
        D = ( eB.z - oB.z ) / dB.z;
        if (D >= 0.0){
            X = oB + D * dB;
            if(X.x >= -eB.x && X.x <= eB.x && X.y >= -eB.y && X.y <= eB.y)
                if (mD < 0.0 || mD > D)
                    mD = D;
        }
    }
    
    return mD;
}
Input Description
vector Ro Origin of Ray
vector Rd Direction of Ray
vector Bo Origin of Box
vector Bs Size of Box
rotation Br Rotation of Box
Output Description
float gBRxZ Returns distance to intersection of a ray and a box
3D

Box and Ray, Intersection Point

Finds intersection of a Ray to a Box and returns intersection point, otherwise ZERO_VECTOR if there is no legal intersection.

vector gBRxX( vector Ro, vector Rd, vector Bo, vector Bs, rotation Br){
    float k = gBRxZ(Ro,Rd,Bo,Bs,Br);
    if( k != -1.0 ) return Ro + Rd * k;
    else return ZERO_VECTOR;}
Input Description
vector Ro Origin of Ray
vector Rd Direction of Ray
vector Bo Origin of Box
vector Bs Size of Box
rotation Br Rotation of Box
Output Description
vector gBRxX Returns point of intersection of a ray and a box
Requirement
float gBRxZ(vector Ro,vector Rd, vector Bo, vector Bs, rotation Br)
3D

Box and Point, Intersection Boolean

Finds if there is an intersection of a Point and a Box and returns boolean

integer gBXx(vector A, vector Bo, vector Bs, rotation Br){
    vector eB = 0.5*Bs; vector rA = (A-Bo)/Br;
    return (rA.x<eB.x && rA.x>-eB.x && rA.y<eB.y && rA.y>-eB.y && rA.z<eB.z && rA.z>-eB.z); }
Input Description
vector A Origin of Point
vector Bo Origin of Box
vector Bs Size of Box
rotation Br Rotation of Box
Output Description
integer gBXx(vector A, vector Bo, vector Bs, rotation Br) Returns boolean check of intersection of a point and a box if there is one, otherwise FALSE
3D
By Nexii Malthus

Box and Point, Nearest Point on Edge

Processes point on nearest edge of box to given point

vector gBXnEX(vector A, vector Bo, vector Bs, rotation Br){
    vector eB = 0.5*<llFabs(Bs.x),llFabs(Bs.y),llFabs(Bs.z)>;
    vector rA = (A-Bo)/Br;
    
    float mD = 3.402823466E+38;
    vector X;
    list EdgesX = [< 0, eB.y, eB.z>, < 0,-eB.y, eB.z>, < 0,-eB.y,-eB.z>, < 0, eB.y,-eB.z>];
    list EdgesY = [< eB.x, 0, eB.z>, <-eB.x, 0, eB.z>, <-eB.x, 0,-eB.z>, < eB.x, 0,-eB.z>];
    list EdgesZ = [< eB.x, eB.y, 0>, <-eB.x, eB.y, 0>, <-eB.x,-eB.y, 0>, < eB.x,-eB.y, 0>];
    
    integer x = (EdgesX != []);
    while( x-- ){
        float y = gLXdZ( llList2Vector( EdgesX, x ), <1,0,0>, rA );
        
        if( rA.x > eB.x ) y += rA.x - eB.x;
        else if( rA.x < -eB.x ) y -= rA.x - -eB.x;
        
        if( y < mD ){ mD = y; X = gLXnX( llList2Vector( EdgesX, x ), <1,0,0>, rA ); }
    }
    x = (EdgesY != []);
    while( x-- ){
        float y = gLXdZ( llList2Vector( EdgesY, x ), <0,1,0>, rA );
        
        if( rA.y > eB.y ) y += rA.y - eB.y;
        else if( rA.y < -eB.y ) y -= rA.y - -eB.y;
        
        if( y < mD ){ mD = y; X = gLXnX( llList2Vector( EdgesY, x ), <0,1,0>, rA ); }
    }
    x = (EdgesZ != []);
    while( x-- ){
        float y = gLXdZ( llList2Vector( EdgesZ, x ), <0,0,1>, rA );
        
        if( rA.z > eB.z ) y += rA.z - eB.z;
        else if( rA.z < -eB.z ) y -= rA.z - -eB.z;
        
        if( y < mD ){ mD = y; X = gLXnX( llList2Vector( EdgesZ, x ), <0,0,1>, rA ); }
    }
    
    if( mD < 0.000001 ) return <-1,-1,-1>;
    if(      X.x >  eB.x ) X.x =  eB.x;
    else if( X.x < -eB.x ) X.x = -eB.x;
    if(      X.y >  eB.y ) X.y =  eB.y;
    else if( X.y < -eB.y ) X.y = -eB.y;
    if(      X.z >  eB.z ) X.z =  eB.z;
    else if( X.z < -eB.z ) X.z = -eB.z;
    
    return Bo + ( X * Br );}
Input Description
vector A Origin of Point
vector Bo Origin of Box
vector Bs Size of Box
rotation Br Rotation of Box
Output Description
vector gBXnEX(vector A, vector Bo, vector Bs, rotation Br) Returns nearest point on edge of box B closest to point A. Returns <-1,-1,-1> if already on closest point.
Requirement
float gLXdZ(vector O,vector D,vector A)
vector gLXdV(vector O,vector D,vector A)
vector gLXnX(vector O,vector D,vector A)
3D
By Nexii Malthus

Cylinder and Point, Intersection Boolean

Finds if there is an intersection of a Point and a Cylinder and returns boolean

integer gCXx( vector A, vector O, rotation R, vector S ) {
    A = ( A - O ) / R;// Converts to local object frame
    return (llPow(A.x/S.x*2,2) + llPow(A.y/S.y*2,2)) <= 1. // Test radius
        && llFabs(A.z/S.z*2) <= 1.;// Test top/bottom
}
Input Description
vector A Origin of Point
vector Bo Origin of Cylinder
vector Bs Size of Cylinder
rotation Br Rotation of Cylinder
Output Description
integer gCXx( vector A, vector O, rotation R, vector S ) Returns boolean check of intersection of a point and a cylinder if there is one, otherwise FALSE
3D
By Nexii Malthus


Polygon and Point, Intersection Boolean

Figures out if point is inside of polygon or otherwise.

integer gCPXx( list CP, vector X )
{//Copyright (c) 1970-2003, Wm. Randolph Franklin; 2008, Strife Onizuka
    integer i = ~(CP != []);
    integer c = 0;
    if(i < -2){
        vector vi = llList2Vector(CP, -1);
        do {
            vector vj = vi;
            vi = llList2Vector(CP, i);
            if((vi.y > X.y) ^ (vj.y > X.y)){
                if(vj.y != vi.y)
                    c = c ^ (X.x < (((vj.x - vi.x) * (X.y - vi.y) / (vj.y - vi.y)) + vi.x));
                else c = c ^ (0 < ((vj.x-vi.x) * (X.y-vi.y)));
            }
        } while (++i);
    }
    return c;
}
Input Description
list CP Vertices of Concave Polygon
vector X Origin of Point
Output Description
integer gCPXx( list CP, vector X ) Returns TRUE if point (X) intersects concave polygon (CP), otherwise FALSE
2D
Copyright (c) 1970-2003, Wm. Randolph Franklin (Must accept License #1), LSL-Port By Strife Onizuka

Polygon and Line Segment, Intersection Boolean

Figures out if line segment intersects with polygon.

integer gVPLSx( vector P0, vector P1, list VP ){
    //Copyright 2001, softSurfer (www.softsurfer.com); 2008, Nexii Malthus
    if( P0 == P1 ) return gCPXx( VP, P0 );
    float tE = 0; float tL = 1;
    float t; float N; float D;
    vector dS = P1 - P0;
    vector e; integer x; integer y = VP!=[];
    @start;
    for( x = 0; x < y; ++x ){
        e = llList2Vector( VP, x+1 ) - llList2Vector( VP, x );
        N = Perp( e, P0 - llList2Vector( VP, x ) );
        D = -Perp( e, dS );
        if( llFabs(D) < 0.00000001 )
            if( N < 0 ) return FALSE;
            else jump start;
        t = N / D;
        if( D < 0 ){
            if( t > tE ){   tE = t; if( tE > tL ) return FALSE;     }
        } else {
            if( t < tL ){   tL = t; if( tL < tE ) return FALSE;     }
    }   }
    // PointOfEntrance = P0 + tE * dS;
    // PointOfExit = P0 + tL * dS;
    return TRUE;
}
Input Description
list VP Vertices of Convex Polygon
vector P0, vector P1 Start and End of Line Segment
Output Description
integer gVPLSx( vector P0, vector P1, list VP ) Returns TRUE if line segment (P0,P1) intersects convex polygon (VP), otherwise FALSE
Requirement
float Perp( vector U, vector V ){ return U.x*V.y - U.y*V.x; }
Perpendicular dot product
integer gCPXx( list CP, vector X ) Only needed for (P0 == P1) safety catch check, so optional
2D
Copyright 2001, softSurfer (www.softsurfer.com) (Must accept License #2), LSL-Port By Nexii Malthus

3D Projection

Projects a vector A by vector B.

vector Project3D(vector A,vector B){
    return B * ( ( A * B ) / ( B * B ) );}
Input Description
vector A First Vector
vector B Second Vector
Output Description
return Project3D(vector A, vector B) Returns result of projection
3D
By Nexii Malthus

Reflection

Reflects Ray R with surface normal N

vector Reflect(vector R,vector N){
    return R - 2 * N * ( R * N );}
Input Description
vector R Ray Normal
vector N Surface Normal
Output Description
return Reflect(vector R, vector N) Returns result of reflection
3D
By Nexii Malthus

For anyone curious to the shorthand used and who wish to use a lookup table can use this as a reference. Or anyone who wishes to add a new function to the library is welcome to but it would be recommended to keep consistency. I tried to minimize the script function names to be easily readable. All the geometric function names start with a g.

g (Shape1) (Shape2) (Process) (Return (Only needed if other than integer))

Here is the legend:

Shorthand Name Description
Geometric Types, all the shapes in the library
X Point vector defining a point in space
L Line A line has an origin and a direction and is infinitely long
LS Line Segment A line segment is a finite line and therefore consists of a start and end position
R Ray A ray is like a line, except it is more distinct as it defines wether it points forward or back
P Plane A 2D doubly ruled surface of infinite size
S Sphere A sphere is defined by origin and radius (No ellipsoid functions available yet)
B Box A box primitive is six sided and defined by origin, size as well as a rotation.
C Cylinder An elliptic cylinder primitive .
VP Convex Polygon Convex Polygon defined by list of vertices.
CP Concave Polygon Concave Polygon defined by list of vertices. Automatic backward compatibility with Convex Polygons.
The Process, What does it do?
d distance Calculate distance
n nearest Calculate nearest
p project Calculates projection
x Intersection Calculates intersection
dir direction Calculates direction
Return, What kind of data do I get out of it?
Z Float Represents that a float is returned
V Vector Represents that a vector is returned
O Origin Represents the Origin of the ray or line
D Direction Direction from the Origin
E Edge Edge of a shape, such as an edge on a box, suffix may mark special case return type

#1

//Copyright (c) 1970-2003, Wm. Randolph Franklin
//Copyright (c) 2008, Strife Onizuka (porting to LSL)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimers.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice in the documentation and/or other materials provided with the
//    distribution.
// 3. The name of W. Randolph Franklin may not be used to endorse or promote
//    products derived from this Software without specific prior written
//    permission. 
 
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

#2

// Copyright 2001, softSurfer (www.softsurfer.com); 2008, LSL-port by Nexii Malthus
// This code may be freely used and modified for any purpose
// providing that this copyright notice is included with it.
// SoftSurfer makes no warranty for this code, and cannot be held
// liable for any real or imagined damage resulting from its use.
// Users of this code must verify correctness for their application.