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 .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):
@@ -132,6 +135,14 @@ class System(Location):
def moons(self):
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:
app_label = 'evesde'
ordering = ['name']