How to resolve the algorithm Find the intersection of a line with a plane step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find the intersection of a line with a plane step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

Finding the intersection of an infinite ray with a plane in 3D is an important topic in collision detection.

Find the point of intersection for the infinite ray with direction   (0, -1, -1)   passing through position   (0, 0, 10)   with the infinite plane with a normal vector of   (0, 0, 1)   and which passes through [0, 0, 5].

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find the intersection of a line with a plane step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' version 11-07-2018
' compile with: fbc -s console

Type vector3d
    Dim As Double x, y ,z
    Declare Constructor ()
    Declare Constructor (ByVal x As Double, ByVal y As Double, ByVal z As Double)
End Type

Constructor vector3d()
    This.x = 0
    This.y = 0
    This.z = 0
End Constructor

Constructor vector3d(ByVal x As Double, ByVal y As Double, ByVal z As Double)
    This.x = x
    This.y = y
    This.z = z
End Constructor

Operator + (lhs As vector3d, rhs As vector3d) As vector3d
    Return Type(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z)
End Operator

Operator - (lhs As vector3d, rhs As vector3d) As vector3d
    Return Type(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z)
End Operator

Operator * (lhs As vector3d, d As Double) As vector3d 
    Return Type(lhs.x * d, lhs.y * d, lhs.z * d)
End Operator

Function dot(lhs As vector3d, rhs As vector3d) As Double
    Return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z
End Function

Function tostring(vec As vector3d) As String
    Return "(" + Str(vec.x) + ", " + Str(vec.y) + ", " + Str(vec.z) + ")"
End Function

Function intersectpoint(rayvector As vector3d, raypoint As vector3d, _
                    planenormal As vector3d, planepoint As vector3d) As vector3d

    Dim As vector3d diff = raypoint - planepoint
    Dim As Double prod1 = dot(diff, planenormal)
    Dim As double prod2 = dot(rayvector, planenormal)
    Return raypoint - rayvector * (prod1 / prod2)

End Function

' ------=< MAIN >=------

Dim As vector3d rv = Type(0, -1, -1)
Dim As vector3d rp = Type(0,  0, 10)
Dim As vector3d pn = Type(0,  0,  1)
Dim As vector3d pp = Type(0,  0,  5)
Dim As vector3d ip = intersectpoint(rv, rp, pn, pp)

print
Print "line intersects the plane at "; tostring(ip)

' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

  

You may also check:How to resolve the algorithm User input/Text step by step in the Retro programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Retro programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Chapel programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Plain English programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the TUSCRIPT programming language