Question

what i want to do is to draw an unfilled halfcircle and draw a pointer on it when i touch it (on the very point i touched it). I have the knowledge to do almost everything of these, but what i need is a function that takes the center and the radius of the halfcircle and the x-coordinate of the point i'm touching and returns a y-coordinate, so that the point (x,y) is a point that belongs to the circle.

I have already tried this, but it leads to awkward results.

    private int f(int centerX, int centerY, int radius, int touchedX){ 
            int y = -1;
            if (touchedX>=centerX-radius && touchedX<=centerX+radius)
               y = (int) (centerY+Math.sin((double) (touchedX-centerX)*radius);
            return y;
   }

Sorry for my bad english and thank you

Was it helpful?

Solution

In cartesian coordinates a circle centered at (x0,y0) of radius r is defined by:

(x - x0)^2 + (y - y0)^2 = r^2

So, if you know x, you have two possible values for y:

y = y0 +/- sqrt( r^2 - (x - x0)^2 )

But this is assuming that the point you clicked is close enough to the circle. So you should compare the chosen y with an y on the circle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top