Here is a weird logical error that I would like help to understand. I pass a value into the parameter (p1) of a method and the double value changes from a -5 to a +5.
The coordinate structure is shown below
Structure Coordinate
x as double
y as double
z as double
End Structure
Here is the call to the method
If RayPlane(0, 0, 1, RayStart.x, RayStart.y, RayStart.z, 0, 0, -1, p1, p2, p3, p4) Then
The value of p1.z changes from -5 to +5 at the start of the RayPlane method when I stopped it in the debugger. Here is the RayPlane method with the p1 parameter highlighted.
Public Function RayPlane(nx as Double, ny as Double, nz as Double, xs as Double, ys as Double, zs as Double, xd as Double, yd as Double, zd as Double, **p1 as Coordinate**, p2 as Coordinate, p3 as Coordinate, p4 as Coordinate) As Boolean
//Determine if the ray has
Dim a as Double =xd*nx+yd*ny+zd*nz
If (a=0) then
Return False
End If
Dim t as Double =((p1.x*nx+p1.y*ny+p1.z*nz-nx*xs-ny*ys-nz*zs)/a)
If (t<0) then
Return False
End If
Dim cp as Coordinate
cp.x = xs+t*xd
cp.y = ys+t*yd
cp.z = zs+t*zd
If ((abs(TriangleArea(p1,p3,p4)-TriangleArea(p1,p4,cp)-trianglearea(p1,p3,cp)-trianglearea(p3,p4,cp))<0.000001) or (abs(trianglearea(p1,p2,p3)-trianglearea(p1,p2,cp)-trianglearea(p2,p3,cp)-trianglearea(p1,p3,cp))<0.000001)) Then
Return True //Yes it intersects
Else
Return False //No intersect
End If
End Function
I don’t know why, and adding the ByRef prefix in front of the p1 parameter prevents the sign reversal (this is how I believe it should work?):
Public Function RayPlane(nx as Double, ny as Double, nz as Double, xs as Double, ys as Double, zs as Double, xd as Double, yd as Double, zd as Double, **ByRef p1 as Coordinate**, p2 as Coordinate, p3 as Coordinate, p4 as Coordinate) As Boolean
//Determine if the ray has
Dim a as Double =xd*nx+yd*ny+zd*nz
If (a=0) then
Return False
End If
Dim t as Double =((p1.x*nx+p1.y*ny+p1.z*nz-nx*xs-ny*ys-nz*zs)/a)
If (t<0) then
Return False
End If
Dim cp as Coordinate
cp.x = xs+t*xd
cp.y = ys+t*yd
cp.z = zs+t*zd
If ((abs(TriangleArea(p1,p3,p4)-TriangleArea(p1,p4,cp)-trianglearea(p1,p3,cp)-trianglearea(p3,p4,cp))<0.000001) or (abs(trianglearea(p1,p2,p3)-trianglearea(p1,p2,cp)-trianglearea(p2,p3,cp)-trianglearea(p1,p3,cp))<0.000001)) Then
Return True //Yes it intersects
Else
Return False //No intersect
End If
End Function
Maybe I am tired, and could someone help me understand why adding ByRef to a parameter allows a double value to be sent to the method with the correct number sign, and why removing ByRef does not?
Thank you!
Edit: added clarification
Windows 11
Xojo 2022 r3.1