Page 2 of 2
Re: Getting direction from point a to point b
Posted: Wed Nov 03, 2021 11:49 pm
by Xii
Gunroar:Cannon() wrote: ↑Sat Oct 30, 2021 10:47 pm
Though the sentence confuses me
Does it find the origin of a line moving in a certain direction?
Let's say you want to find the point
halfway between a and b.
Code: Select all
angl = direction(a.x, a.y, b.x, b.y)
dist = distance(a.x, a.y, b.x, b.y)
halfway.x, halfway.y = transposition(a.x, a.y, angl, dist/2)
transposition means "Move this point in this direction by this distance, and give me the new coordinates."
It tells you where you are
going to be, if you start here and go this way that far.
Re: Getting direction from point a to point b
Posted: Thu Nov 04, 2021 9:23 am
by darkfrei
Xii wrote: ↑Wed Nov 03, 2021 11:49 pm
Gunroar:Cannon() wrote: ↑Sat Oct 30, 2021 10:47 pm
Though the sentence confuses me
Does it find the origin of a line moving in a certain direction?
Let's say you want to find the point
halfway between a and b.
Over 9000 times faster:
Code: Select all
halfway.x, halfway.y = b.x-a.x, b.y-a.y
With normalization:
Code: Select all
local dx, dy = b.x-a.x, b.y-a.y
local length = (dx*dx + dy*dy)^0.5
local nx, ny = dx/length, dy/length
halfwayX, halfwayY = a.x + 0.5*length*nx, a.y + 0.5*length*ny
Re: Getting direction from point a to point b
Posted: Thu Nov 04, 2021 11:22 pm
by Xii
darkfrei wrote: ↑Thu Nov 04, 2021 9:23 am
Over 9000 times faster:
Code: Select all
halfway.x, halfway.y = b.x-a.x, b.y-a.y
This gives you the difference (distance) on each axis. You cannot get a halfway without dividing by 2 somewhere. It'd be more like this:
Code: Select all
halfway.x, halfway.y = a.x + (b.x - a.x) / 2, a.y + (b.y - a.y) / 2
Anyway that's besides the point. There's always a better way to do any given thing. I was just illustrating how the function works.
Re: Getting direction from point a to point b
Posted: Sat Nov 06, 2021 6:27 am
by Gunroar:Cannon()
Oh, so that last one is the best option?
Re: Getting direction from point a to point b
Posted: Sat Nov 06, 2021 10:05 am
by pgimeno
The best option, for best clarity, is probably to lerp. Lerping is an operation that should be in every programmer's toolbox. Lerp is short for linear interpolation; you pass it the origin value and the destination value, and then a value between 0 (for which it returns the origin) and 1 (for which it returns the destination).
Code: Select all
function lerp(a, b, t)
return a + (b - a) * t
end
Due to floating point rounding issues, however, it does not always return the destination when passing t = 1; in that case it's better to split it as follows:
Code: Select all
function lerp(a, b, t)
return t < 0.5 and a + (b - a) * t or b + (a - b) * (1 - t)
end
Never use a formulation like a * (1 - t) + b * t because that's not monotonic, which causes serious issues in some circumstances; see
https://math.stackexchange.com/question ... erpolation
Applied to the case at hand:
Code: Select all
halfway.x, halfway.y = lerp(a.x, b.x, 0.5), lerp(a.y, b.y, 0.5)