Reputation: 29
Given the vectors p,q and number of points n I want to approximate shape of the ellipse with these n points as best as possible. To do this I use parametric equation of ellipse and change radius and angle in a double for loop:
n = 10000
points = []
p = [300, 0]
q = [0, 200]
root = int(math.sqrt(n))
for a in range(root):
for b in range(root):
x = 400 + (a/root)*(p[0] - q[0])*math.cos(2*math.pi*b/root)
y = 300 - (a/root)*(p[1] - q[1])*math.sin(2*math.pi*b/root)
points.append([x, y])
for w in points:
pygame.draw.circle(screen, (200, 50, 75), (int(w[0]), int(w[1])), 1)
Here I'm using pygame to draw but it doesn't really matter. Given these parameters my ellipse looks like this:
with n = 100000. It looks like this:
Given the nature of the ellipse there are more points in the middle using basic parametrization of the radious and angle. Because of that I need very large n to get good picture. How can I change parameterization to make points better spread on the whole area?
Upvotes: 1
Views: 446
Reputation: 729
Perhaps create the points uniformly and then filter out the ones outside the ellipse?
import numpy as np
from math import sqrt
import matplotlib.pyplot as plt
def filter_points(h, k, a, b, x, y):
mask = ((x-h)**2)/(a**2) + ((y-k)**2)/(b**2) <= 1
return x[mask], y[mask]
h = 0
k = 0
a = 4
b = 5
N = 10000
n = int(sqrt(N))
X, Y = np.meshgrid(
np.linspace(-a,a,n),
np.linspace(-b,b,n)
)
X, Y = filter_points(h, k, a, b, X[:], Y[:])
points = np.asarray([X, Y]).T
plt.figure
plt.plot(X,Y,'.')
Upvotes: 1