root/branch/joao/umitMapper/Coordinate.py @ 594

Revision 594, 3.9 kB (checked in by ignotus21, 6 years ago)

Copyright changes

Line 
1# Copyright (C) 2007 Adriano Monteiro Marques <py.adriano@gmail.com>
2#
3# Author: Joao Paulo de Souza Medeiros <ignotus21@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19import math
20
21
22class PolarCoordinate:
23    """
24    Class to implement a polar coordinate object
25    """
26
27    def __init__(self, r=0, t=0):
28        """
29        Constructor method of PolarCoordinate class
30        @type  r: number
31        @param r: The radius of coordinate
32        @type  t: number
33        @param t: The angle (theta) of coordinate in radians
34        """
35
36        self.__r = r
37        """Radius of polar coordinate"""
38        self.__t = t
39        """Angle (theta) of polar coordinate in radians"""
40
41
42    def getCoordinate(self):
43        """
44        Set polar coordinate
45        @rtype: tuple
46        @return: Polar coordinates (r, t)
47        """
48        return (self.__r, self.__t)
49
50
51    def setCoordinate(self, r, t):
52        """
53        Set polar coordinate
54        @type  r: number
55        @param r: The radius of coordinate
56        @type  t: number
57        @param t: The angle (theta) of coordinate
58        """
59        self.__r = r
60        self.__t = t
61
62
63    def toCartesian(self):
64        """
65        Convert polar in cartesian coordinate
66        @rtype: tuple
67        @return: cartesian coordinates (x, y)
68        """
69        x = self.__r * math.cos(self.__t)
70        y = self.__r * math.sin(self.__t)
71
72        return (x, y)
73
74
75
76class CartesianCoordinate:
77    """
78    Class to implement a cartesian coordinate object
79    """
80
81    def __init__(self, x=0, y=0):
82        """
83        Constructor method of CartesianCoordinate class
84        @type  x: number
85        @param x: The x component of coordinate
86        @type  y: number
87        @param y: The y component of coordinate
88        """
89        self.__x = x
90        """X component of cartesian coordinate"""
91        self.__y = y
92        """Y component of cartesian coordinate"""
93
94
95    def getCoordinate(self):
96        """
97        Get cartesian coordinate
98        @rtype: tuple
99        @return: Cartesian coordinates (x, y)
100        """
101        return (self.__x, self.__y)
102
103
104    def setCoordinate(self, x, y):
105        """
106        Set cartesian coordinate
107        @type  x: number
108        @param x: The x component of coordinate
109        @type  y: number
110        @param y: The y component of coordinate
111        """
112        self.__x = x
113        self.__y = y
114
115
116    def toPolar(self):
117        """
118        Convert cartesian in polar coordinate
119        @rtype: tuple
120        @return: polar coordinates (r, t)
121        """
122        r = math.sqrt(self.__x**2 + self.__y**2)
123
124        if self.__x > 0:
125            if self.__y >= 0:
126                t = math.atan( self.__y / self.__x );
127            else:
128                t = math.atan( self.__y / self.__x ) + 2 * math.pi;
129        elif self.__x < 0:
130            t = math.atan( self.__y / self.__x ) + math.pi;
131        elif self.__x == 0:
132            if self.__y == 0:
133                t = 0;
134            if self.__y > 0:
135                t = math.pi / 2;
136            else:
137                t = -math.pi / 2;
138
139        return (r, t)
140
141
142
143if __name__ == "__main__":
144
145    # Testing application
146
147    polar     = PolarCoordinate( 1, math.pi )
148    cartesian = CartesianCoordinate( -1,  0 )
149
150    print polar.toCartesian()
151    print cartesian.toPolar()
Note: See TracBrowser for help on using the browser.