# Estimating the Pythagorean Theorem

**URL:** <https://discourse.codecombat.com/t/estimating-the-pythagorean-theorem/4741>\
**Category:** Bugs\
**Created:** [August 21, 2015, 6:36pm UTC](https://discourse.codecombat.com/t/estimating-the-pythagorean-theorem/4741 "2015-08-21T18:36:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![keeper0](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@keeper0](https://discourse.codecombat.com/u/keeper0)\
**Post date:** [August 21, 2015, 6:36pm UTC](https://discourse.codecombat.com/t/estimating-the-pythagorean-theorem/4741/1 "2015-08-21T18:36:31Z")

</div>

I wrote a function to calculate the distance between two positions:

```

   def distanceBetween(pos1, pos2):
        xDiff = pos2.x - pos1.x 
        yDiff = pos2.y - pos1.y
        dist = Math.sqrt(xDiff **2 + yDiff** 2)
        # self.say(dist)
        return(dist)

```

However, this seems to tax the system. If I use this function in Summit’s Gate, the Chrome Browser crashes. Even on my powerful desktop system.

I suggest using the following approximation:

```

   def distanceBetween(pos1, pos2):
        xDiff = pos2.x - pos1.x 
        yDiff = pos2.y - pos1.y
        if xDiff < 0 : 
            xDiff = -xDiff
        if yDiff < 0 : 
            yDiff = -yDiff

        if xDiff > yDiff : 
            dist = xDiff + 0.33 *yDiff
        else : 
            dist = yDiff + 0.33 *xDiff
        # self.say(dist)
        return(dist)

```

The answer returned is within 6% of the exact value and avoids the square and square root functions.

(Note: I see an old topic which says that you should be able to call distanceTo on other objects, but that didn’t seem to be working for me. I will re-investigate.)

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [August 27, 2015, 12:29pm UTC](https://discourse.codecombat.com/t/estimating-the-pythagorean-theorem/4741/2 "2015-08-27T12:29:23Z")

</div>

Pretty cool! You should be able to get to our distance function if you have a Vector object, then you can do `pos1.distance(pos2)`. If you have a unit or other visible object which _has_ a `pos`, then you can do `unit1.distanceTo(pos2)`. Note that the first is `distance` and the second is `distanceTo`, unfortunately.

If you just have two plain `{x, y}` objects, you can make one into a Vector to get access to `distance` like this: `Vector(pos1.x, pos1.y).distance(pos2)`.

Also note that if you are just comparing distances and don’t care about the absolute value, you can always use `pos1.distanceSquared(pos2)` which also avoids the square root (the square root is the slow part).

Basically, everything you can do outside of player code will run a lot faster, because player code gets transpiled and error-checked and instrumented and all this stuff, whereas game engine code (even though it does the same things) just runs. CodeCombat’s implementation of Vector `distance`, for example, is [pretty straightforward CoffeeScript](https://github.com/codecombat/codecombat/blob/master/app/lib/world/vector.coffee#L45-L61):

```coffeescript
  distance: (other, useZ) ->
    dx = @x - other.x
    dy = @y - other.y
    sum = dx * dx + dy * dy
    if useZ
      dz = @z - other.z
      sum += dz * dz
    Math.sqrt sum

  distanceSquared: (other, useZ) ->
    dx = @x - other.x
    dy = @y - other.y
    sum = dx * dx + dy * dy
    if useZ
      dz = @z - other.z
      sum += dz * dz
    sum

```

---

<div class="post-metadata">

**Author:** ![ant](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ant/32/3362_2.png) [@ant](https://discourse.codecombat.com/u/ant)\
**Post date:** [October 6, 2015, 11:34am UTC](https://discourse.codecombat.com/t/estimating-the-pythagorean-theorem/4741/3 "2015-10-06T11:34:11Z")

</div>

> [@nick](#):
>
> If you just have two plain `{x, y}` objects, you can make one into a Vector

So an `{x, y}` object is not a vector by default? Probably that’s why [some vector functions do not work with them](http://discourse.codecombat.com/t/cryptic-error-message-t-copy-is-not-a-function/5000/3)?

* * *

I was trying to get the distance between a shells impact point and the catapult in Summit’s Gate, with varying results:

```python
loop:
    shell = self.findNearest(self.findEnemyMissiles())
    catapult = self.findNearest(self.findByType("catapult"))
    if shell and catapult:
        self.say(shell +" "+ shell.targetPos.distance(catapult.pos))

```

This gives a certain value in the first few seconds, then it gives an error: _“Cannot read property ‘distance’ of undefined”_

I tried to make the first object a vector:

```python
        self.say(shell +" "+ Vector(shell.targetPos).distance(catapult.pos))

```

This gives sometimes a `NaN` result, but if it “catches” a spear, I get a value.

I’m probably confusing things, but the situation is far from clear…

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [October 6, 2015, 10:38pm UTC](https://discourse.codecombat.com/t/estimating-the-pythagorean-theorem/4741/4 "2015-10-06T22:38:27Z")

</div>

Hmm, I think probably you are finding the shell in the moment after it exploded but before it ceased to exist (while the explosion was ongoing). Maybe ignore shells without a `targetPos`? If `shell.targetPos` is defined, it’s already a Vector.

For reference, here’s the Vector class: [https://github.com/codecombat/codecombat/blob/master/app/lib/world/vector.coffee](https://github.com/codecombat/codecombat/blob/master/app/lib/world/vector.coffee)

---

<div class="post-metadata">

**Author:** ![ant](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ant/32/3362_2.png) [@ant](https://discourse.codecombat.com/u/ant)\
**Post date:** [October 8, 2015, 7:51am UTC](https://discourse.codecombat.com/t/estimating-the-pythagorean-theorem/4741/5 "2015-10-08T07:51:54Z")

</div>

> [@nick](#):
>
> For reference, here’s the Vector class: [https://github.com/codecombat/codecombat/blob/master/app/lib/world/vector.coffee](https://github.com/codecombat/codecombat/blob/master/app/lib/world/vector.coffee)

Thanks for the answer. I’m afraid it exceeds my understanding of code, but I will give it a try 😄
