0 votes
by (140 points)
edited by
Sugarcube 2.21

I'm attempting to find a method which would allow me to automatically establish the distance between a variable and other positions inside a 2d grid  using cartesian coordinates.  As an example if X0Y0 is 10 units away from $pos, how could I establish how far away X5Y5 would be from $pos without actually plotting it out and measuring it with my Cad software, which is what I am doing now. I'm sure it's possible somehow with javascript, but I'm at a loss with how to implement it.

 

Thanks for any help,

Sean

2 Answers

+1 vote
by (159k points)

note: you don't state the co-ordinates of $pos, which makes it hard to know exactly what 10 units means.

Is the 'distance':
A. along the grid's two axis, measure along the X axis then measure along the Y axis.
B. as the crow-flies, measure direct distance between two points.

Both methods start off the same way:

1. If the X or Y axis of the two points is the same value then you only need to calculate the absolute difference between the two points for the axis that isn't the same.
eg. if the X axis of the two points is the same value then you calculate the 'distance' as

distance = Math.abs(Y1 - Y2)

2. If neither the X or Y axis is the same for the two points then you first calculate the above for both the X and Y axis...

distanceX = Math.abs(X1 - X2)
distanceY = Math.abs(Y1 - Y2)

... then to calculate the overall distance you either.

2a. Add the distanceX and distanceY together.

distance = distanceX + distanceY

2b. Use distanceX and distanceY to calculate the hypotenuse (x2 + y2 = z2) of a right angle triangle.

distance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2))

 

by (140 points)
First of all, thanks for the quick answer. To be more specific. If my character's position ($pos) is X0Y0, and my target ($tar) is at X5Y5, I need a way to establish my target distance ($tarDist) "as the crow flies"  by calculating the hypotenuse, as you mentioned.

Right now what I'm doing is just setting a variable for distance and updating it for a particular position based on measuring the actual distance with CAD. The problem, besides it being tedious, is that I get no heading.  

As far as your answer, how exactly do I make that work in Sugarcube? I'm a newbie to Twine and Javascript as well. I go from epic joy to epic frustration on a daily basis.  When I get this figured out I am going to be ecstatic.
by (44.7k points)

If that's what you need, then just do what greyelf said, and calculate the hypotenuse.

Assuming the distance from 0x-0y to 1x-0y is 1 unit, then do:

<<set $pos = { X : 0, Y : 0}>>
<<set $tarDist = { X : 5, Y : 5}>>
<<set $distance = Math.sqrt(Math.pow($pos.X - $tarDist.X, 2) + Math.pow($pos.Y - $tarDist.Y, 2))>>

Then $distance will be the distance in units from $pos to $tarDist.

by (140 points)
I have to thank you again personally, greyelf. You've made my week. This is going to make it so much easier for me to move ahead with my project. I wish I could buy you a beer.

 

Sean
by (159k points)

I wish I could buy you a beer.

I live in a country where it's people are renown for their beer drinking but unfortunate for you I'm not one of them, so you will have to have one for me instead and 'put the money on the fridge'.(*)

(*) that reference is also showing my age because the show that made it popular here comes from the early '80s

+1 vote
by (44.7k points)
edited by

In addition to what greyelf said, if you only know that $pos is 10 units away from 0xy, but don't actually know where $pos is, then you can't say how far $pos is from 5xy, because $pos could be anywhere on the edge of a circle with a 10 unit radius centered on 0x-0y.

Even if you know the distance of $pos from two points, you may still not know where $pos is, because that usually just narrows it down to two possible (mirrored) locations (on a 2D grid, at least).

That said, if you know that $pos is A units away from 0xy (in this case A=10), 0xy is B units away from 5xy, and the degrees of angle $pos-0xy-5xy, then you can use that to solve for the length of side C ($pos-5xy) by using the cosine rule: C = Math.sqrt(A^2 + B^2 - (2*A*B*Math.cos(deg)))

In any case, we need a bit more information on what's going on in order to answer this question.

by (140 points)
I did not initially provide enough information, HiEv. If my position is X0Y0, and I want to calculate the distance of my target (X5Y5) in a straight line and have it be reflected as I move the target along an updated set of cartesian coordinates, how could I accomplish this? It looks as if greyelf has provided good information, but I need help implementing this within Twine Sugarcube.

Could you help?

Sean
by (44.7k points)
edited by

Like I said in my reply to your comment to greyelf, assuming the distance from 0x-0y to 1x-0y is 1 unit, then do:

<<set $pos = { X : 0, Y : 0}>>
<<set $tarDist = { X : 5, Y : 5}>>
<<set $distance = Math.sqrt(Math.pow($pos.X - $tarDist.X, 2) + Math.pow($pos.Y - $tarDist.Y, 2))>>

Then $distance will be the distance in units from $pos to $tarDist.

P.S. "$tarDist" - I see what you did there.  ;-)

by (140 points)
I think I got it ! Awesome. Thanks guys!

Sean
by (140 points)

HiEv, your help is much appreciated. I really look up to guys like you who take time out of their lives to help us noobs. I hope to one day be able to return the favor somehow.

Sean

This site has been placed in read-only mode. Please use the Interactive Fiction Community Forum instead.
...