Package mosp :: Package geo :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module mosp.geo.utils

 1  # -*- coding: utf-8 -*- 
 2  """Utils""" 
 3   
 4  from math import pi, sqrt, atan2 
 5   
 6  __author__ = "F. Ludwig, B. Henne" 
 7  __maintainer__ = "B. Henne" 
 8  __contact__ = "henne@dcsec.uni-hannover.de" 
 9  __copyright__ = "(c) 2010, DCSec, Leibniz Universitaet Hannover, Germany" 
10  __license__ = "GPLv3" 
11   
12   
13 -def distance(node1, node2):
14 """Euclidean distance of two nodes given by the Pythagorean formula. 15 @author: F. Ludwig""" 16 x = node1.x - node2.x 17 y = node1.y - node2.y 18 return sqrt(x**2 + y**2)
19
20 -def atan2Deg(x, y):
21 """Returns the atan2(y, x) in degrees""" 22 return (atan2(y, x) / (2*pi) * 360)
23
24 -def angleToXAxis(x, y):
25 """Returns the angle in degrees between the x-axis and the line through origin and point (x,y)""" 26 if y >= 0: 27 return atan2Deg(x,y) 28 else: 29 return 360+atan2Deg(x,y)
30
31 -def inDistance(x1, y1, x2, y2, distance):
32 """Returns, whether point (x1,y1) and point (x2,y2) are in a distance lower or equal distance""" 33 return round(sqrt((x2-x1)**2 + (y2-y1)**2)) <= distance
34
35 -def pointInSector(pointX, pointY, originX, originY, radius, centralAngle, direction):
36 """Returns whether a point (x,y) is in a sector of a circle with given coordinates (x,y), radius, angle and direction""" 37 38 translatX = pointX - originX 39 translatY = pointY - originY 40 41 # is point in radius? 42 if not inDistance(0, 0, translatX, translatY, radius): 43 return False 44 45 # is central angle greater or equal 360° (full view) 46 if centralAngle >= 360: 47 return True 48 49 # define borders and angles of sector, turn all angles to positive values 50 51 # positive direction 0° - 360°, defined for -360° < sectorDirection < infinity 52 dir = (direction+360) % 360 53 ## right border: -180° <= sRightBorder <= 180° 54 rightBorder = dir - (centralAngle / 2) 55 # left border: 0° <= sLeftBorder <= 540° 56 leftBorder = dir + (centralAngle / 2) 57 # angle from x-axis to vector to point, 0° <= pointAngle < 360° 58 pointAngle = angleToXAxis(translatX, translatY) 59 60 # rotate angles to fit sector's right border to x-axis 61 leftBorder = (leftBorder - rightBorder+360) % 360 62 pointAngle = (pointAngle - rightBorder+360) % 360; 63 64 # is point in or outside sector borders 65 if pointAngle <= leftBorder: 66 return True 67 else: 68 return False
69