Posted: 25th Sep 2003 22:38
Just a simple function to see if a specified point is inside a triangle.

Example:
+ Code Snippet
`inTriangle() Function
`By Hamish McHaggis
`25/9/03

#CONSTANT NUMTRIANGLES 100

dim pointX(NUMTRIANGLES,3) as integer
dim pointY(NUMTRIANGLES,3) as integer

sync on

`Randomize triangles
for x=1 to NUMTRIANGLES
   xPos=rnd(540)
   yPos=rnd(380)
   for y=1 to 3
      pointX(x,y)=rnd(100)+xPos
      pointY(x,y)=rnd(100)+yPos
   next y
next x

do
   cursorX=mousex()
   cursorY=mousey()

   `If spacekey is pressed, randomize triangles again
   if spacekey()=1
      wait 500
      for x=1 to NUMTRIANGLES
         xPos=rnd(540)
         yPos=rnd(380)
         for y=1 to 3
            pointX(x,y)=rnd(100)+xPos
            pointY(x,y)=rnd(100)+yPos
         next y
      next x
   endif

   `Loop through triangles
   for x=1 to NUMTRIANGLES
      `Detect if point is inside triangle, if so, colour it red
      if inTriangle(pointX(x,1),pointY(x,1),pointX(x,2),pointY(x,2),pointX(x,3),pointY(x,3),cursorX,cursorY)=1
         ink rgb(255,0,0),0
      else
         ink rgb(255,255,255),0
      endif
      `Draw triangle
      line pointX(x,1),pointY(x,1),pointX(x,2),pointY(x,2)
      line pointX(x,2),pointY(x,2),pointX(x,3),pointY(x,3)
      line pointX(x,3),pointY(x,3),pointX(x,1),pointY(x,1)
   next x

   text 10,10,str$(screen fps())

   sync
   cls
loop

`Finds if a point is inside a triangle
function inTriangle(triX1,triY1,triX2,triY2,triX3,triY3,pointX,pointY)
   for x=1 to 3
      vectorX1 = triX2 - triX1 : vectorX2 = triX3 - triX2 : vectorX3 = triX1 - triX3
      vectorY1 = triY2 - triY1 : vectorY2 = triY3 - triY2 : vectorY3 = triY1 - triY3
      cVectorX1 = pointX - triX1 : cVectorX2 = pointX - triX2 : cVectorX3 = pointX - triX3
      cVectorY1 = pointY - triY1 : cVectorY2 = pointY - triY2 : cVectorY3 = pointY - triY3
      dotProduct1#=vectorX1*-cVectorY1+vectorY1*cVectorX1 : dotProduct2#=vectorX2*-cVectorY2+vectorY2*cVectorX2 : dotProduct3#=vectorX3*-cVectorY3+vectorY3*cVectorX3
   next x

   if (dotProduct1#<0 and dotProduct2#<0 and dotProduct3#<0) or (dotProduct1#>0 and dotProduct2#>0 and dotProduct3#>0) then exitfunction 1
endfunction 0


Function:
+ Code Snippet
`inTriangle() Function
`By Hamish McHaggis
`25/9/03

`Finds if a point is inside a triangle
function inTriangle(triX1,triY1,triX2,triY2,triX3,triY3,pointX,pointY)
   for x=1 to 3
      vectorX1 = triX2 - triX1 : vectorX2 = triX3 - triX2 : vectorX3 = triX1 - triX3
      vectorY1 = triY2 - triY1 : vectorY2 = triY3 - triY2 : vectorY3 = triY1 - triY3
      cVectorX1 = pointX - triX1 : cVectorX2 = pointX - triX2 : cVectorX3 = pointX - triX3
      cVectorY1 = pointY - triY1 : cVectorY2 = pointY - triY2 : cVectorY3 = pointY - triY3
      dotProduct1#=vectorX1*-cVectorY1+vectorY1*cVectorX1 : dotProduct2#=vectorX2*-cVectorY2+vectorY2*cVectorX2 : dotProduct3#=vectorX3*-cVectorY3+vectorY3*cVectorX3
   next x

   if (dotProduct1#<0 and dotProduct2#<0 and dotProduct3#<0) or (dotProduct1#>0 and dotProduct2#>0 and dotProduct3#>0) then exitfunction 1
endfunction 0
Posted: 25th Sep 2003 23:10
Cool, I just wrote a similar function a few weeks ago. Though I didn't use the 3d math commands, and the results were accurate enough for some reason.
Posted: 25th Sep 2003 23:52
Can I see?
Posted: 26th Sep 2003 0:17
I posted my own version into Codebase a few days ago

Mine uses a slightly faster and more compressed method.
Posted: 26th Sep 2003 2:23
I quit fussing over it, cause I found UWDesign had the function I needed.
Posted: 26th Sep 2003 19:32
You did Ian? Oh well, I'll take a look.

EDIT

Ahh, I see, you test to see if the first two dot products are on different sides of 0 and then if not test the third.