## Convert Cartesian coordinate system to polar coordinate system with multi-points

I would like to convert many points from the Cartesian coordinate system to polar coordinate system.

This is my code:

import numpy as np x = 1 y = 1 def cart_to_pol(x, y): rho = np.sqrt(x**2 + y**2) phi = np.arctan2(y, x) return(rho, phi) print(cart_to_pol(x, y))

But I have many points in an array that needed to convert.

- Is there any method that I can covert all points at the same time without one by one?
- If the center is not at (0,0), but at (50,50) for example. How can I set the center of the circle in coding?
- The result from the code above the degree is in radius (0~2pi), but I need the degree in angle (0~360). I try
`phi = np.arctan2(y, x)*180/pi`

but the result is wrong. How can I fix it?

Thanks!!!

points = [(10, 43), (10, 44), (10, 45), (10, 46), (10, 47), (10, 48), (10, 49), (10, 50), (10, 51), (10, 52), (10, 53), (10, 54), (10, 55), (10, 56), (11, 39), (11, 40), (11, 41), (11, 42), (11, 57), (11, 58), (11, 59), (11, 60), (12, 36), (12, 37), (12, 38), (12, 61), (12, 62), (12, 63), (13, 33), (13, 34), (13, 35), (13, 64), (13, 65), (13, 66), (14, 31), (14, 32), (14, 33), (14, 66), (14, 67), (14, 68), (15, 30), (15, 31), (15, 68), (15, 69), (16, 28), (16, 29), (16, 70), (16, 71), (17, 27), (17, 72), (18, 25), (18, 26), (18, 73), (18, 74), (19, 24)]

Just use complex math

import numpy as np x = 1 y = 1 def cart_to_pol(x, y, x_c = 0, y_c = 0, deg = True): complex_format = x - x_c + 1j * (y - y_c) return np.abs(complex_format), np.angle(complex_format, deg = deg) print(cart_to_pol(x, y)) (1.4142135623730951, 45.0)

Just pass your center coordinates to `(x_c, y_c)`

Or you could do it with 2d arrays:

def cart_to_pol(coords, center = [0,0], deg = True): complex_format = np.array(coords, dtype = float).view(dtype = np.complex) -\ np.array(center, dtype = float).view(dtype = np.complex) return np.abs(complex_format).squeeze(), np.angle(complex_format, deg = deg).squeeze() print(cart_to_pol(points)) (array([44.14748011, 45.12205669, 46.09772229, 47.07440918, 48.05205511, 49.03060269, 50.009999 , 50.99019514, 51.97114584, 52.95280918, 53.93514624, 54.91812087, 55.90169944, 56.88585061, 40.52159918, 41.48493703, 42.44997055, 43.41658669, 58.05170109, 59.03388857, 60.01666435, 61. , 37.94733192, 38.89730068, 39.84971769, 62.16912417, 63.15061362, 64.13267498, 35.4682957 , 36.40054945, 37.33630941, 65.30696747, 66.28725368, 67.26812024, 34.0147027 , 34.92849839, 35.84689666, 67.46851117, 68.44705983, 69.42621983, 33.54101966, 34.43835072, 69.63476143, 70.61161378, 32.24903099, 33.12099032, 71.80529228, 72.78049189, 31.90611227, 73.97972695, 30.8058436 , 31.6227766 , 75.18643495, 76.15773106, 30.61045573]), array([76.90810694, 77.19573393, 77.47119229, 77.73522627, 77.98852161, 78.23171107, 78.46537935, 78.69006753, 78.90627699, 79.11447295, 79.3150876 , 79.50852299, 79.69515353, 79.87532834, 74.24882634, 74.62374875, 74.98163937, 75.32360686, 79.07719528, 79.2611029 , 79.43898931, 79.61114218, 71.56505118, 72.03086026, 72.47443163, 78.87081071, 79.04593736, 79.21570213, 68.49856568, 69.07549826, 69.62356479, 78.51800865, 78.69006753, 78.85711014, 65.69545073, 66.37062227, 67.0112832 , 78.02386756, 78.19756579, 78.366366 , 63.43494882, 64.17900803, 77.56043798, 77.73522627, 60.2551187 , 61.11341823, 77.12499844, 77.30041551, 57.80426607, 76.71513352, 54.24611275, 55.30484647, 76.14858099, 76.32869287, 51.63251462]))

**Calculus II,** For instance in the Cartesian coordinate system at point is given the coordinates ( x,y) ( x , y ) and we Polar to Cartesian Conversion Formulas. I would like to convert many points from the Cartesian coordinate system to polar coordinate system. This is my code: import numpy as np x = 1 y = 1 def cart_to_pol(x, y): rho = np.sqrt(x**2

I didn't answer the bottom two questions, but this is how you can convert all points in one swoop. Just a for loop defining for x,y to get each point the tuple. Good luck!

import numpy as np points = np.array([(10, 43), (10, 44), (10, 45), (10, 46), (10, 47), (10, 48), (10, 49), (10, 50), (10, 51), (10, 52), (10, 53), (10, 54), (10, 55), (10, 56), (11, 39), (11, 40), (11, 41), (11, 42), (11, 57), (11, 58), (11, 59), (11, 60), (12, 36), (12, 37), (12, 38), (12, 61), (12, 62),(12, 63), (13, 33), (13, 34), (13, 35), (13, 64), (13, 65), (13, 66), (14, 31), (14, 32), (14, 33), (14, 66), (14, 67), (14, 68), (15, 30), (15, 31), (15, 68), (15, 69), (16, 28), (16, 29), (16, 70), (16, 71), (17, 27), (17, 72), (18, 25), (18, 26), (18, 73), (18, 74), (19, 24)]) polar = [] index = 0 for x, y in points: r = np.sqrt(x**2 + y**2) phi = np.arctan2(y, x) polar.append((r, phi)) index += 1 print(polar)

**The Polar Coordinate System,** Examples of Polar Coordinates: Points in the polar coordinate system with To convert from polar to rectangular (Cartesian) coordinates use the following Multiple sets of polar coordinates can have the same location as our first solution. A Cartesian coordinate system (UK: / k ɑː ˈ t iː zj ə n /, US: / k ɑːr ˈ t i ʒ ə n /) is a coordinate system that specifies each point uniquely in a plane by a set of numerical coordinates, which are the signed distances to the point from two fixed perpendicular oriented lines, measured in the same unit of length.

numpy can handle matrix or 2D numpy array

import numpy as np points = np.array([(10, 43), (10, 44), (10, 45), (10, 46), (10, 47), (10, 48), (10, 49), (10, 50), (10, 51), (10, 52), (10, 53), (10, 54), (10, 55), (10, 56), (11, 39), (11, 40), (11, 41), (11, 42), (11, 57), (11, 58), (11, 59), (11, 60), (12, 36), (12, 37), (12, 38), (12, 61), (12, 62),(12, 63), (13, 33), (13, 34), (13, 35), (13, 64), (13, 65), (13, 66), (14, 31), (14, 32), (14, 33), (14, 66), (14, 67), (14, 68), (15, 30), (15, 31), (15, 68), (15, 69), (16, 28), (16, 29), (16, 70), (16, 71), (17, 27), (17, 72), (18, 25), (18, 26), (18, 73), (18, 74), (19, 24)]) #passing the points[:,0] as x, points[:,1] as y, (a,b) as center def cart_to_pol(points, a = 0, b = 0): rho = np.sqrt((points[:,0]-a)**2 + (points[:,1]-b)**2) phi = np.arctan2((points[:,1]-a), (points[:,0]-b)) return rho, phi #for center at (0,0) cart_to_pol(points) #for center at (1,1) cart_to_pol(points,1,1)

Hope this will solve your 1 and 2 questions. for last question..

func = lambda x : x if x>0 else (2*np.pi + phi) phi = func(phi) print(phi)

Please tell if this helps.

**[PDF] The Polar Coordinate System - UNL Digital Commons,** For any given point in the polar coordinate plane, there are multiple ways to represent that point (as opposed to Cartesian coordinates, where point important to be able to convert both points and equations from one system to the other . Today, I'd like to introduce another coordinate system - polar coordinates. In polar coordinates, a point is represented with r (the length from the origin) and θ (the angle between the vector and the x axis). A sample program TJB764 shows both coordinates. Conversion from polar coordinates to Cartesian coordinates This is easy.

**7.3 Polar Coordinates,** Converting Points between Coordinate Systems. Given a point P in the plane with Cartesian coordinates (x,y) and polar coordinates (r,θ), the following� The polar coordinate system is extended into three dimensions with two different coordinate systems, the cylindrical and spherical coordinate system. Applications [ edit ] Polar coordinates are two-dimensional and thus they can be used only where point positions lie on a single two-dimensional plane.

**[PDF] Chapter 5 Complex Numbers, Polar Coordinates, and Parametric ,** plotting points, graphing equations, and using the properties of the Cartesian plane to investigate A point P in the polar coordinate system is represented by an ordered pair of numbers ( , ). 2 Find multiple sets of polar coordinates for a given point. ( 3 Convert a point from polar to rectangular coordinates. The polar coordinate system is a two-dimensional coordinate system in which each point on a plane is determined by a distance from a reference point and an angle from a reference direction.

Cartesian coordinate system and the Polar coordinate systems are two of the common coordinate systems used in mathematics. Cartesian Coordinates. Cartesian coordinate system uses the real number line as the reference. In one dimension, the number line extends from negative infinity to positive infinity.

##### Comments

- Could you run your code in a for loop or list comprehension? And are you using
`np.pi`

or did you set`pi`

to some value? - Hi, your answer is very helpful and I really appreciate that. May I ask how can I do if I want to show angles between 0~360? For example, if I set center = [50,50], for position (10,43), the result shows the angle is -170.07375449. How can I make this shows as 189.9262455?
`np.angle(complex_format, deg = deg) % 360`

? Be sure to upvote if you found it helpful and mark solved if it is..- Hi, your answer is very helpful and I really appreciate that. May I ask how can I do if I want to show angles between 0~2π? For example, if I set center = [50,50], for position (10,43), the result shows the angle (in radius) is -2.96834699. How can I make this shows as 3.314838317179586?
- Could you please tell me where should I insert "func = lambda x...." I tried to insert it into the coding above (your original answer), but I got errors. Thank you!!!