# Hit Ellipse

Hello again,

I’ve a little problem with a code. A year ago I posted this topic: Hit Button [ Help me! ]

Simeon helped me with a code with a class called Button. This class contains a function contains(point).

I need a function to detect touch on an ellipse (round not square).

I was not able to figure out how to do this, so I’m not too good with math. I think this is about math.

Something to calculate the round, maybe pi, cos, sin, etc.

Given how fat our fingers are, perhaps exact accuracy isn’t necessary. Why not just test within a rectangle chosen to fit the ellipse as closely as possible, especially if the button is almost rectangular?

If you have a centered ellipse, with `w` width, `h` height, `x` and `y` distance from the center, the formula:

``````(x * x) / (w * w) + (y  * y) / (h * h) <= 1
``````

should work.
NB: Not tested, I just remember that from somewhere. Forgive me if I am incorrect.

Amost correct: w and h are the half width and height

Here’s an example I wrote long ago. Change the shape of the ellipse with the parameters and then drag your finger across the ellipse.

``````
function setup()
parameter.integer("a",1,400,200)
parameter.integer("b",1,400,100)
end

function draw()
background(40, 40, 50)

xc=250    -- center of ellipse x axis
yc=400    -- center of ellipse y axis

fill(255)
ellipse(xc,yc,a*2,b*2)    -- draw ellipse

x=CurrentTouch.x    -- touched x value
y=CurrentTouch.y    -- touched y value

-- determine if touched value is inside ellipse
fill(255,0,0)
if (y-yc)^2/b^2+(x-xc)^2/a^2 <= 1 then
text("inside",xc,yc)
end
end

``````

@dave1707 - Your code works perfect. Thanks!