Difference between revisions of "Talk:Sqrt - Math"

From the CreationKit Wiki
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
== Calculating distance (example) ==
== Calculating distance (example) ==
Calculating distance travelled with Pythagorean theorem, uses sqrt and [[Pow - Math|pow]]:
Calculating distance travelled with the Pythagorean theorem, using sqrt and [[Pow - Math|pow]]:


<source lang="papyrus">
<source lang="papyrus">
Line 21: Line 21:
; Pythagorean theorem for 3D planes: sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)
; Pythagorean theorem for 3D planes: sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)


float dist = Math.sqrt(Math.pow((coords[3] - coords[0]), 2) + Math.pow((coords[4] - coords[1]), 2) + Math.pow((coords[5] - coords[2]), 2))
float fDist = Math.sqrt(Math.pow((coords[3] - coords[0]), 2) + Math.pow((coords[4] - coords[1]), 2) + Math.pow((coords[5] - coords[2]), 2))
Debug.trace("Travelled " + dist + " units.")
Debug.trace("Travelled " + fDist + " units.")
EndEvent
EndEvent
</source>
</source>
--[[User:Ingvion|Ingvion]] ([[User talk:Ingvion|talk]]) 10:13, 13 July 2023 (EDT)
--[[User:Ingvion|Ingvion]] ([[User talk:Ingvion|talk]]) 10:13, 13 July 2023 (EDT)

Latest revision as of 15:58, 13 July 2023

Calculating distance (example)[edit source]

Calculating distance travelled with the Pythagorean theorem, using sqrt and pow:

Event OnEffectStart(Actor akTarget, Actor akCaster)
	target = akTarget

	coords = new float[6]  ; let's not declare 6 separate vars
    coords[0] = target.X   ; X = hardcoded short for GetPositionX(), same for other axes
    coords[1] = target.Y
    coords[2] = target.Z

	RegisterForSingleUpdate(5)
EndEvent

Event OnUpdate()
    coords[3] = target.X
    coords[4] = target.Y
    coords[5] = target.Z

	; Pythagorean theorem for 3D planes: sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)

	float fDist = Math.sqrt(Math.pow((coords[3] - coords[0]), 2) + Math.pow((coords[4] - coords[1]), 2) + Math.pow((coords[5] - coords[2]), 2))
	Debug.trace("Travelled " + fDist + " units.")
EndEvent

--Ingvion (talk) 10:13, 13 July 2023 (EDT)