ⓘ This web page is about the old AlgoSim 2 software. Perhaps you are looking for the new Algosim 3 application?
AlgoSim
Homepage | Screenshots | Gallery of Visualisation | User's Guide | Function Reference | Download | FAQ | Support | Bugs and Solutions | Donate

Bug #006

This page describes a known bug in AlgoSim.

Symptom: The reflected rays have incorrect directions in the example program mirrorSim3.prg.

Applies to: This problem is found in AlgoSim 0.9.0.117 and probably most earlier versions.

Cause: Incorrect formula used to compute the direction of the reflected ray.

Solution: Replace the program mirrorSim3.prg by the following, corrected, version:

;; Mirror simulator in 3D
;; Now without bugs.

clearView3(1)

;if ¬identExists("t")
  t ≔ choiceDialog("parabolic", "spherical", "hyperbolic cosine")
;endif

;if t ∉ {"parabolic", "spherical", "hyperbolic cosine"}
  t ≔ choiceDialog("parabolic", "spherical", "hyperbolic cosine")
;endif

;if t = "parabolic"
  mirrorFunction ≔ "y, z""(y^2 + z^2) / 50"
;elseIf t = "spherical"
  mirrorFunction ≔ "y, z""-sqrt(625 − y^2 − z^2) + 25"
;elseIf t = "hyperbolic cosine"
  mirrorFunction ≔ "y, z""cosh(√(y^2 + z^2) / 6) − 1"
;endif

mirror ≔ createSurfParamCurves("❨mirrorFunction(y, z), y, z❩", "y, z", -10, 10, -10, 10)
beginDrawing(0)
drawSurfParamCurves("mirror", "colour:red")

;iterate y:-8:8:2, z:-8:8:2
    ;; Incoming ray
    mFyz ≔ mirrorFunction(y, z)
    drawLine3(❨20, y, z❩, ❨mFyz, y, z❩, "colour:grey")
    ;; Reflected ray
    grad ≔ ❨diff("mirrorFunction(y, z)", "y", y), diff("mirrorFunction(y, z)", "z", z)❩
    tangent1 ≔ ❨grad_1, 1, 0
    tangent2 ≔ ❨grad_2, 0, 1
    normal ≔ tangent1 × tangent2
    normal ≔ 1/norm(normal) ⋅ normal
    incident ≔ ❨-1, 0, 0
    reflection ≔ incident − 2⋅(incident|normal)⋅normal
    endpoint ≔ ❨mFyz, y, z❩ + 20⋅reflection
    drawLine3(❨mFyz, y, z❩, endpoint)
;enditerate

delete("mFyz")
delete("grad")
delete("tangent1")
delete("tangent2")
delete("incident")
delete("normal")
delete("reflection")
delete("endpoint")
delete("y")
delete("z")
delete("t")

endDrawing(0)
redraw3(0)

Naturally, this bug will be fixed in a future version of AlgoSim.