What is wrong with my equation for an ellipse?

131 Views Asked by At

I'm drawing an ellipse on the screen by generating lots of random y-values for every x-value (this creates bristles of a paintbrush I can later use to draw strokes).

Since wikipedia says the equation of an ellipse at the origin is:

$$\frac{x^2}{a^2} + \frac{y^2}{b^2} = 1$$

I used basic algebra to solve for

$$y = \pm\sqrt{b^2 \left( 1 - \frac{x^2}{a^2}\right)}$$

The problem is the resulting random points are not an ellipse at all, it's a stepped inverted mess like this:

enter image description here

Can anyone tell me why my equation isn't working please?


Below is my Javascript code for Paper.js if it helps:

for (let x = -width/2; x < width/2; x += bristleSize)
    for (let bristleCounter = 1; bristleCounter < numBristlesPerX; bristleCounter++)
        bristles.push(getRandomPointInEllipseAtX(x, width, height));

function getRandomPointInEllipseAtX(x, width, height)
{
    const halfWidth = width / 2;
    const halfHeight = height / 2;
    const heightAtX = 2 * Math.sqrt(Math.abs(halfHeight^2 * (1 - (x^2 / halfWidth^2))));
    const y = randomPN() * heightAtX; // -1 to 1 * height
    return new p.Point(x, y);
}
1

There are 1 best solutions below

0
On

The strongest guess is that the randomPN() function generates a continuous random number in $[-1,1]$, while it should generate random numbers in $\{-1,1\}$. Try to use a command like 2*floor(randomPN())+1.