Added function to calculate the distance between systems.

This commit is contained in:
2013-11-11 21:26:21 +00:00
parent 813358271d
commit 360959dab1
2 changed files with 18 additions and 1 deletions

View File

@@ -1,5 +1,8 @@
from math import sqrt
from django.db import models from django.db import models
from .utils import InheritanceQuerySet from evesde.utils import euclidean_distance
from evesde.app_defines import DISTANCE_LIGHT_YEAR
from evesde.models.utils import InheritanceQuerySet
class LocationManager(models.Manager): class LocationManager(models.Manager):
@@ -132,6 +135,14 @@ class System(Location):
def moons(self): def moons(self):
return Moon.objects.filter(planet__in=self.planets.all()) return Moon.objects.filter(planet__in=self.planets.all())
def distance_to(self, destination):
"""Calculates the ly distance between the two systems"""
if not isinstance(destination, System):
raise ValueError('Provided destination is not a System.')
origin = (self.x, self.y, self.z)
destination = (destination.x, destination.y, destination.z)
return euclidean_distance(origin, destination) / DISTANCE_LIGHT_YEAR
class Meta: class Meta:
app_label = 'evesde' app_label = 'evesde'
ordering = ['name'] ordering = ['name']

6
evesde/utils.py Normal file
View File

@@ -0,0 +1,6 @@
from math import sqrt
def euclidean_distance(origin, destination):
"""Calculates the Euclidean distance of two sets of x/y/z tuples"""
return sqrt(sum((a - b)**2 for a, b in zip(origin, destination)))