극좌표와 직교좌표 사이의 변환

420 days ago by namy0727

@interact def Cartesian(xpt=input_box(1, label='x ='), ypt=input_box(-1, label='y ='), radius=selector(['r>0', 'r<0'], label="r"), angle=selector(['(-pi,pi]', '[0,2*pi)'], label="theta"), do_plot = ("Draw Plots", True)): (r,theta) = (0,0) if ypt == 0: if xpt == 0: (r,theta) = (0,'theta') elif xpt > 0: if radius == 'r>0': (r,theta) = (xpt,0) elif radius == 'r<0': (r,theta) = (-xpt,pi) elif xpt < 0: if radius == 'r>0': (r,theta) = (-xpt,pi) elif radius == 'r<0': (r,theta) = (xpt,0) elif ypt > 0: if xpt == 0: if radius == 'r>0': (r,theta) = (ypt,1/2*pi) elif radius == 'r<0': if angle == '(-pi,pi]': (r,theta) = (-ypt,-1/2*pi) elif angle == '[0,2*pi)': (r,theta) = (-ypt,3/2*pi) elif xpt > 0: if radius == 'r>0': (r,theta) = (sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)) elif radius == 'r<0': if angle == '(-pi,pi]': (r,theta) = (-sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)-pi) elif angle == '[0,2*pi)': (r,theta) = (-sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)+pi) elif xpt < 0: if radius == 'r>0': (r,theta) = (sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)+pi) elif radius == 'r<0': if angle == '(-pi,pi]': (r,theta) = (-sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)) elif angle == '[0,2*pi)': (r,theta) = (-sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)+2*pi) elif ypt < 0: if xpt == 0: if radius == 'r>0': if angle == '(-pi,pi]': (r,theta) = (-ypt,-1/2*pi) elif angle == '[0,2*pi)': (r,theta) = (-ypt,3/2*pi) elif radius == 'r<0': (r,theta) = (ypt,1/2*pi) elif xpt < 0: if radius == 'r>0': if angle == '(-pi,pi]': (r,theta) = (sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)-pi) elif angle == '[0,2*pi)': (r,theta) = (sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)+pi) elif radius == 'r<0': (r,theta) = (-sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)) elif xpt > 0: if radius == 'r>0': if angle == '(-pi,pi]': (r,theta) = (sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)) elif angle == '[0,2*pi)': (r,theta) = (sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)+2*pi) elif radius == 'r<0': (r,theta) = (-sqrt(xpt*xpt + ypt*ypt),arctan(ypt/xpt)+pi) #R = RealField(16); theta = R(theta) pretty_print(html('$$(r,\\theta) = %s$$'%latex((r,theta)))) if do_plot: p= point((xpt,ypt), rgbcolor = (0,0,1), size=40) if r == 0: p.show(xmin = -1, xmax = 1, ymin = -1, ymax = 1, aspect_ratio=1) else: if r < 0: p+= line([(0,0),(-xpt,-ypt)], color='black') p+= line([(0,0),(xpt,ypt)], rgbcolor = (0,0,1), linestyle='--') p+= arc([0,0], 0.2*abs(r), sector=(0,theta), rgbcolor = (1,0,0)) elif r>0: p+= line([(0,0),(xpt,ypt)], rgbcolor = (0,0,1)) p+= arc([0,0], 0.2*abs(r), sector=(0,theta), rgbcolor = (1,0,0)) min_x = -abs(xpt)-0.5 max_x = abs(xpt)+0.5 min_y = -abs(ypt)-0.5 max_y = abs(ypt)+0.5 p.show(xmin = min_x, xmax = max_x, ymin = min_y, ymax = max_y, aspect_ratio=1) 
       
x = 
y = 
theta 
Draw Plots 

Click to the left again to hide and once more to show the dynamic interactive window

@interact def Polar(radius=input_box(2, label='r ='), theta=input_box(1/3*pi, label='theta ='), do_plot = ("Draw Plots", True)): r = radius; t = theta (xpt,ypt) = (r*cos(t),r*sin(t)) pretty_print(html('$$(x,y) = %s$$'%latex((xpt,ypt)))) if do_plot: p= point((xpt,ypt), rgbcolor = (0,0,1), size=40) if r == 0: p.show(xmin = -1, xmax = 1, ymin = -1, ymax = 1, aspect_ratio=1) else: if r < 0: p+= line([(0,0),(-xpt,-ypt)], color='black') p+= line([(0,0),(xpt,ypt)], rgbcolor = (0,0,1), linestyle='--') p+= arc([0,0], 0.2*abs(r), sector=(0,theta), rgbcolor = (1,0,0)) elif r>0: p+= line([(0,0),(xpt,ypt)], rgbcolor = (0,0,1)) p+= arc([0,0], 0.2*abs(r), sector=(0,theta), rgbcolor = (1,0,0)) min_x = -abs(xpt)-0.5 max_x = abs(xpt)+0.5 min_y = -abs(ypt)-0.5 max_y = abs(ypt)+0.5 p.show(xmin = min_x, xmax = max_x, ymin = min_y, ymax = max_y, aspect_ratio=1) 
       
r = 
theta = 
Draw Plots 

Click to the left again to hide and once more to show the dynamic interactive window