import math as m def rec_to_pol(x,y): r = m.sqrt (x**2 + y**2) theta = m.degrees(m.atan2(y,x)) return (r,theta) def pol_to_rec(r,theta): x = r*m.cos(m.radians(theta)) y = r*m.sin(m.radians(theta)) return (x,y) def add_two_vectors(r1, theta1, r2, theta2): x1 = r1*m.cos(m.radians(theta1)) y1 = r1*m.sin(m.radians(theta1)) x2 = r2*m.cos(m.radians(theta2)) y2 = r2*m.sin(m.radians(theta2)) c = x1+x2 d = y1 + y2 r = m.sqrt(c**2 + d**2) theta = m.degrees(m.atan2(d,c)) return (r, theta) print rec_to_pol (3,4) print pol_to_rec (5,-126.87) print add_two_vectors(5, 45, 10, 120)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question