Just a simple function to see if a specified point is inside a triangle.
Example:
`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:
`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
Brains are for idiots.
Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM