Question

I'm creating an interface control in an iPhone app where views are positioned in an arc, corresponding to the X touch position. I need to write a function that, given an X value, returns the Y value that corresponds to the red line on the graph below: enter image description here

I will be defining points A, B, and radius r to control the amount of curve. Point A will always be at 12 o'clock (greater Y value than point B). My math (especially with circles) is a little rusty… what formulas do I need to write this function? An example to accompany any formula(s) would be helpful. Thanks!

Edit: Function should return ABS(Y)

Was it helpful?

Solution

Assuming the center of the circle is (0,0). (Writing a translation function to the iphone screen coordinates should be simple)

formula for a circle is x^2 + y^2 = r^2

if( x < -(B.x) ) { return minY }//  B.x is the x coord of b.
if( x > B.x ) { return minY }
else {
   return sqrt( r^2 - x^2)
}

OTHER TIPS

You can use the Pythagorean theorem to find the Y value. X2 + Y2 = r2.

Rearranging that, we get Y2 = r2 - X2. Taking the square root of both sides we get Y = sqrt(r2 - x2). Since we know the Y values we care about are positive, we don't need to pay attention to the +/- we'd normally have when taking a square root.

That all assumes the center of the circle is at (0,0). If it's not, you'll need to apply an offset.

From there, it's just a matter of clamping the value -- i.e., if you get a Y value smaller than your minimum Y value, then you set it to the minimum.

The equation for a circle is (x-a)^2 + (y-b)^2 = r^2, where (a,b) are the coordinates of the center and r is the radius. Assuming the center will be at (0,0) and the radius is 1, the equation is simplified to x^2 + y^2 = 1. To solve for y, the equation changes to

y^2 = 1 - x^2

or

y = ± SQRT(1-x^2) for {-1 <= x <= 1}

So, if your x value is 0.5, plug that in, and get

y = ± SQRT(1 - 0.5^2)
y = ± SQRT(1 - 0.25)
y = ± SQRT(0.75)
y = ± 0.866

You only need the positive value, so your coordinates would be (0.5, 0.866).

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