简体   繁体   English

在曲线中,如何计算给定 X 的点的 Y position?

[英]In a curve, how do I calculate the Y position of a point for a given X?

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:我正在 iPhone 应用程序中创建一个界面控件,其中视图位于弧形中,对应于 X 触摸 position。我需要编写一个 function,给定 X 值,返回对应于红线的 Y 值下图:在此处输入图像描述

I will be defining points A, B, and radius r to control the amount of curve.我将定义点 A、B 和半径 r 来控制曲线的数量。 Point A will always be at 12 o'clock (greater Y value than point B). A 点将始终位于 12 点钟位置(Y 值大于 B 点)。 My math (especially with circles) is a little rusty… what formulas do I need to write this function?我的数学(尤其是圆圈)有点生疏……我需要什么公式来写这个 function? An example to accompany any formula(s) would be helpful.伴随任何公式的示例会有所帮助。 Thanks!谢谢!

Edit: Function should return ABS(Y)编辑:Function 应该返回 ABS(Y)

Assuming the center of the circle is (0,0).假设圆心为(0,0)。 (Writing a translation function to the iphone screen coordinates should be simple) (写个翻译function到iphone屏幕坐标应该很简单)

formula for a circle is x^2 + y^2 = r^2圆的公式是 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)
}

You can use the Pythagorean theorem to find the Y value.您可以使用毕达哥拉斯定理来找到 Y 值。 X 2 + Y 2 = r 2 . X 2 + Y 2 = r 2

Rearranging that, we get Y 2 = r 2 - X 2 .重新排列,我们得到 Y 2 = r 2 - X 2 Taking the square root of both sides we get Y = sqrt(r 2 - x 2 ).对两边取平方根,我们得到 Y = sqrt(r 2 - x 2 )。 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.因为我们知道我们关心的 Y 值是正的,所以我们不需要注意通常在求平方根时会有的 +/-。

That all assumes the center of the circle is at (0,0).这一切都假设圆心位于 (0,0)。 If it's not, you'll need to apply an offset.如果不是,则需要应用偏移量。

From there, it's just a matter of clamping the value -- ie, if you get a Y value smaller than your minimum Y value, then you set it to the minimum.从那里开始,这只是限制值的问题——即,如果您得到的 Y 值小于您的最小 Y 值,则将其设置为最小值。

The equation for a circle is (xa)^2 + (yb)^2 = r^2, where (a,b) are the coordinates of the center and r is the radius.圆的方程是 (xa)^2 + (yb)^2 = r^2,其中 (a,b) 是圆心坐标,r 是半径。 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假设中心位于 (0,0) 且半径为 1,则方程简化为 x^2 + y^2 = 1。要求解 y,方程变为

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所以,如果你的 x 值是 0.5,把它代入,得到

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).您只需要正值,因此您的坐标将为 (0.5, 0.866)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM