Last active
May 31, 2018 06:03
-
-
Save mpilosov/17f3e9372b612de135523519b2048172 to your computer and use it in GitHub Desktop.
One way to draw an ellipse in python (with two functions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from matplotlib import pyplot as plt | |
def drawellipse(center = [0,0], radius = 1, k=1, h=1, N=50): | |
assert len(center) == 2 # good practice to do things like this. | |
x0, y0 = center | |
x = np.linspace(x0-h*radius , x0+h*radius, N) | |
y = np.sqrt(radius**2 - ((x-x0)/h)**2 )*k # from the eq for an ellipse | |
plt.plot(x, -y+y0, c='blue') | |
plt.plot(x, y+y0, c='red') | |
plt.xlim([-50, 50]) # optional stuff here... | |
plt.ylim([-50,50]) | |
plt.axis('equal') | |
plt.show() | |
return None | |
def drawcircle(center = [0,0], radius = 1, N=50): | |
return drawellipse(center=center, radius=radius, k=1, h=1, N=N) | |
drawcircle([1,1], 2.0) | |
drawcircle([-1,2], 3.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment