mirror of
https://github.com/nikdoof/django-evesde.git
synced 2025-12-13 10:52:17 +00:00
Added function to calculate the distance between systems.
This commit is contained in:
@@ -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']
|
||||
|
||||
6
evesde/utils.py
Normal file
6
evesde/utils.py
Normal 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)))
|
||||
Reference in New Issue
Block a user