"Fossies" - the Fresh Open Source Software Archive

Member "radialnet/util/geometry.py" (18 Feb 2008, 2420 Bytes) of package /linux/privat/old/radialnet-0.44.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "geometry.py" see the Fossies "Dox" file reference documentation.

    1 # vim: set fileencoding=utf-8 :
    2 
    3 # Copyright (C) 2007 Insecure.Com LLC.
    4 #
    5 # Author: João Paulo de Souza Medeiros <ignotus21@gmail.com>
    6 #
    7 # This program is free software; you can redistribute it and/or modify
    8 # it under the terms of the GNU General Public License as published by
    9 # the Free Software Foundation; either version 2 of the License, or
   10 # (at your option) any later version.
   11 #
   12 # This program is distributed in the hope that it will be useful,
   13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 # GNU General Public License for more details.
   16 #
   17 # You should have received a copy of the GNU General Public License
   18 # along with this program; if not, write to the Free Software
   19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   20 
   21 import math
   22 
   23 
   24 def is_in_square(point, half_side, center=(0, 0)):
   25     """
   26     """
   27     x, y = point
   28     a, b = center
   29 
   30     if a + half_side >= x >= a - half_side:
   31         if b + half_side >= y >= b - half_side:
   32             return True
   33 
   34     return False
   35 
   36 
   37 def is_in_circle(point, radius=1, center=(0, 0)):
   38     """
   39     """
   40     x, y = point
   41     a, b = center
   42 
   43     if ((x - a)**2 + (y - b)**2) <= (radius**2):
   44         return True
   45 
   46     return False
   47 
   48 
   49 def atan_scale(point, scale_ceil):
   50     """
   51     """
   52     new_point = float(10.0 * point / scale_ceil) - 5
   53     return math.atan(abs(new_point))
   54 
   55 
   56 def normalize_angle(angle):
   57     """
   58     """
   59     new_angle = 360.0 * (float(angle / 360) - int(angle / 360))
   60 
   61     if new_angle < 0:
   62         return 360 + new_angle
   63 
   64     return new_angle
   65 
   66 
   67 def is_between_angles(a, b, c):
   68     """
   69     """
   70     a = normalize_angle(a)
   71     b = normalize_angle(b)
   72     c = normalize_angle(c)
   73 
   74     if a > b:
   75 
   76         if c >= a and c <= 360 or c <= b:
   77             return True
   78 
   79         return False
   80 
   81     else:
   82 
   83         if c >= a and c <= b:
   84             return True
   85 
   86         return False
   87 
   88 
   89 def angle_distance(a, b):
   90     """
   91     """
   92     distance = abs(normalize_angle(a) - normalize_angle(b))
   93 
   94     if distance > 180:
   95         return 360 - distance
   96 
   97     return distance
   98 
   99 
  100 def calculate_short_path(iangle, fangle):
  101     """
  102     """
  103     if iangle - fangle > 180:
  104         fangle += 360
  105 
  106     if iangle - fangle < -180:
  107         fangle -= 360
  108 
  109     return iangle, fangle
  110 
  111 
  112 def angle_from_object(distance, size):
  113     """
  114     """
  115     return math.degrees(math.atan2(size / 2.0, distance))