## 2015. január 30., péntek

### PROJECT A POINT ONTO THE SURFACE OF THE TRIANGLE

PROJECT A POINT ONTO THE SURFACE OF THE TRIANGLE

in the variable names d means distance,length, magnitude; 3d means three dimensional. If 3d not used then it is 2D.
To get IT point I will use line-line intersection.
The coordinates should be flattened so this time I project from above so I flatten Z.
Example:  P1 [x,y,z] = P1[x,y,0] or just use point2 variable type to calculate P1[x,y]
Draw a line L1 from any Triangle point to T point.
The Vector from P3 through T which is L1 will be then:
normalize(T-P3)

Now, this line we intersect with the other line P2-P1 to get the exact point through T lies on the Line.
This is necessary because our IT point lays exactly on this line.
Get the angles:
α = acos(dot normalize(P3-P2) normalize(P3-T))
Γ = acos(dot normalize(T-P3) normalize(P1-P2))
To Get the lenght of the dP2IP1 first get the lenght of dP2P3
dP2P3 = SQRT((P3.x-P2x)^2+(P3.y-P2y)^2)
Then
dP2P3/(sin Γ)=dP2IP1/(sin α) therefore:
dP2IP1 = asin (dP2P3/(sin Γ) * (sin α))
Make IP1 out of the gathered data:
IP1 = normalize(P2-P1)*dP2IP1 + P2

We have to convert this length dP2IP1 to the 3d lenght of the P2-IP1by using ratios. This means if the P2-IP1 lenght have a ratio of the whole length of the 2d P2-P1 line then the 3D line 3dP2IP1 lenght will have the same ratio. Therefore:
ratio = dP2IP1/dP2P1
Now have to caclulate the other lenght dTP3 and ratio2. Width 3dIP1P3 whic will give the IT point.
3dIP1P3 = SQRT((IP1.x-P3.x)^2+(IP1.y-P3.y)^2)
ratio2 = SQRT((T.x-P3.x)^2+(T.y-P3.y)^2)/3dIP1P3
Calulate the real 3d lenght of P2-P1 by using triagle law.
So it is dP2P1 2d lenght and the Z coords will make the other lenght of
the 90 degree triangle to calculate the 3d lenght of the P2-P1. 3ddP2P1 = SQRT(P2.z-P1.Z)^2+dP2P1^2)

Now the rest With 3d coords:
3dIP1 = normalize(P2-P1) * 3ddP2P1*ratio + P2
3ddP3IP1 = sqrt((IP1.z-P3.z)^2 + sqrt((IP1.x-P3.x)^2+(IP1.y-P3.y)^2))
Finally the T1 point:
T1=normalize (P3-IP1)*3ddP3IP1*ratio2 + P3

To accelerate the code use the magnitudes and normalizations only once where possible.