Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/api/Math.py
blob: 41d67aefc237399fe9e5eb078d8029f19a43bd42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math

#-------------------------------------------------------------------------------
# toStandardAngle().
# Returns the given angle in the 0-359 range. 
#
# Parameters:
# aAngle: Angle to check.
#
# Returns: The same angle in the 0-359 range.
#-------------------------------------------------------------------------------
def toStandardAngle(aAngle):
    aAngle = aAngle % 360;
    if (aAngle < 0):
        aAngle = aAngle + 360
    return(aAngle)

#-------------------------------------------------------------------------------
# radToDeg().
# Convert the angle given in radians to degrees. 
#
# Parameters:
# aAngle: Angle in radians to be converted.
#
# Returns: The angle in degrees.
#-------------------------------------------------------------------------------
def radToDeg(aAngle):
    return aAngle * 180 / math.pi
    #return aAngle * 180 / 3.1415927
    

#-------------------------------------------------------------------------------
# degToRad().
# Convert the angle given in degrees to radians. 
#
# Parameters:
# aAngle: Angle in degrees to be converted.
#
# Returns: The angle in radians.
#-------------------------------------------------------------------------------
def degToRad(aAngle):
    return aAngle * math.pi / 180
    #return aAngle * 3.1415927 / 180


def dist(ax1, ay1, ax2, ay2):
    return math.sqrt((ax2-ax1)**2 + (ay2-ay1)**2)

# determine if a point is inside a given polygon or not
# Polygon is a list of (x,y) pairs.
# http://www.ariel.com.au/a/python-point-int-poly.html
# http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
def pointInsidePolygon(x, y, poly):

    n = len(poly)
    inside =False

    p1x,p1y = poly[0]
    for i in range(n+1):
        p2x,p2y = poly[i % n]
        if y > min(p1y,p2y):
            if y <= max(p1y,p2y):
                if x <= max(p1x,p2x):
                    if p1y != p2y:
                        xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                    if p1x == p2x or x <= xinters:
                        inside = not inside
        p1x,p1y = p2x,p2y

    return inside


def pointInsideRectangle(x, y, rect):
    
    x1 = rect[0][0]
    y1 = rect[0][1]
    x2 = rect[1][0]
    y2 = rect[1][1]
    
    if (x > x1) and (x < x2):
        if (y > y1) and (y < y2):
            return True
    return False