获得在Android的一个弧形或椭圆形点 [英] get points on an arc or oval in Android

查看:147
本文介绍了获得在Android的一个弧形或椭圆形点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造一些像大转盘折的价格是正确的。有点像这样的:

我已经得到了左侧的椭圆形,右弧与顶部和底部的职位做。当我失去了是如何创建的内部线路,而不诉诸一堆复杂的数学运算。我想,如果我能得到我的形状点,不知何故,我可以很容易使我行这种方式。不过,我没有找到在Android SDK的任何东西,让我这个。

有没有什么办法让绘制的对象的点的Andr​​oid?如果没有,是否有一个简单的解决方案,我没有看到?

在此先感谢!

编辑#1:

通过下面我后采取了重击在数学,我似乎无法得到它的工作:(

这是我到目前为止有:

 浮起=(leftOval.bottom  -  leftOval.top)/ 2;
            浮B =(leftOval.right  -  leftOval.left)/ 2;
            浮X1 =(浮点)getXForOval(A,B,顶部+ 50);
            浮Y1 =畅销+ 50;
            浮X2 = X1 + 50;
            浮Y2 = Y1;

            Log.d(的coords,计算:+ getXForOval(A,B,顶+ 50)+);

            Log.d(的coords,leftOval.top:+ leftOval.top +);
            Log.d(的coords,leftOval.bottom:+ leftOval.bottom +);

            Log.d(的coords,leftOval.right:+ leftOval.right +);
            Log.d(的coords,leftOval.left:+ leftOval.left +);

            Log.d(COORDS,一:+ A +);
            Log.d(的coords,B:+ B +);
            Log.d(的coords,×1:+ X1 +);
            Log.d(COORDS,Y1:+ Y1 +);
            Log.d(的coords,×2:+ 2 +);
            Log.d(COORDS,Y2:+ Y2 +);

            canvas.drawLine(X1,Y1,X2,Y2,漆);

    私人双人getXForOval(浮起,浮动B,浮动Y){
        //开方(一^ 2 *(1  -  Y ^ 2 / B ^ 2))

        // @formatter:关闭
        返回的Math.sqrt(
                        Math.abs(
                            Math.pow(一,2)*(1  - 
                                                    (Math.pow(γ,2)/ Math.pow(B,2))
                                                )
                                )
                        );

        // @formatter:上
    }
 

不过,X值是未来的出路大。我究竟做错了什么?

  08-27 18:16:56.574:DEBUG / COORDS(2785):计算:2743.647207641682
08-27 18:16:56.584:DEBUG / COORDS(2785):leftOval.top:180.0
08-27 18:16:56.584:DEBUG / COORDS(2785):leftOval.bottom:780.0
08-27 18:16:56.584:DEBUG / COORDS(2785):leftOval.right:185.0
08-27 18:16:56.584:DEBUG / COORDS(2785):leftOval.left:135.0
08-27 18:16:56.584:DEBUG / COORDS(2785):一:300.0
08-27 18:16:56.584:DEBUG / COORDS(2785):B:25.0
08-27 18:16:56.584:DEBUG / COORDS(2785):X1:2743.6472
08-27 18:16:56.584:DEBUG / COORDS(2785):Y1:230.0
08-27 18:16:56.584:DEBUG / COORDS(2785):X2:2793.6472
08-27 18:16:56.584:DEBUG / COORDS(2785):Y2:230.0
 

解决方案

数学是不是非常复杂的。的公式为椭圆为x ^ 2 /一^ 2 + Y ^ 2 / B ^ 2 = 1,其中a和b是在长轴和短轴的长度,它是恒定的。你需要找到x对于一个给定的Y,它是开方(一^ 2 *(1 - Y ^ 2 / B ^ 2))。这将让你在x,y从左侧椭圆的中心偏移和线的长度是不变的。使用正弦函数,以动画的y值,它应该很好看。

编辑:

比较遗憾的是,我的评论有关a和b应该提到,你有他们逆转。此外,您必须使用y坐标相对于椭圆形的中心。

本身乘以数量比使用Math.pow当你平方更快。

  //一个应该是宽度的一半
        浮起=(leftOval.right  -  leftOval.left)/ 2;
        //和b的一半高度
        浮B =(leftOval.bottom  -  leftOval.top)/ 2;

        浮yCenter =(leftOval.top + leftOval.bottom)/ 2;
        浮X中心值=(leftOval.right + leftOval.left)/ 2;

        浮X1 =(浮点)getXForOval(A,B,顶部+ 150,X中心值,yCenter);
        浮Y1 =顶部+ 150;
        浮X2 = X1 + 50;
        浮Y2 = Y1;

        Log.d(COORDS
                计算:+ getXForOval(A,B,顶+ 50,X中心值,yCenter)
                        +);

        Log.d(的coords,leftOval.top:+ leftOval.top +);
        Log.d(的coords,leftOval.bottom:+ leftOval.bottom +);

        Log.d(的coords,leftOval.right:+ leftOval.right +);
        Log.d(的coords,leftOval.left:+ leftOval.left +);

        Log.d(COORDS,一:+ A +);
        Log.d(的coords,B:+ B +);
        Log.d(的coords,×1:+ X1 +);
        Log.d(COORDS,Y1:+ Y1 +);
        Log.d(的coords,×2:+ 2 +);
        Log.d(COORDS,Y2:+ Y2 +);

        canvas.drawLine(X1,Y1,X2,Y2,漆);
    }
}

私人双人getXForOval(浮起,浮动B,浮Y,浮X中心值,
        浮动yCenter){
    //开方(一^ 2 *(1  -  Y ^ 2 / B ^ 2))

    //计算基于相对于中心值
    浮yOffset = Y  -  yCenter;

    // @formatter:关闭
    返回的Math.sqrt(
                    Math.abs(
                        A * A *(1  - 
                                                ((yOffset * yOffset)/(B * b))的
                                            )
                            )
                    )+ X中心值;
   // @formatter:上
}
 

I'm trying to create something like the "Big Wheel" off of the price is right. Kinda like this:

I've got the left oval, the right arc and the top and bottom posts done. Where I am lost at is how to create the inner lines without resorting to a bunch of complicated math. I figure that if I can get the points on my shapes somehow I can fairly easily make my lines this way. However, I am not finding anything in the Android SDK that gives me this.

Is there any way to get the points of drawn objects in Android? If not, is there a simpler solution that I'm not seeing?

Thanks in advance!

EDIT #1:

With the post below I took a whack at the math and I can't seem to get it to work :(

This is what I have so far:

            float a = (leftOval.bottom - leftOval.top) / 2;
            float b = (leftOval.right - leftOval.left) / 2;
            float x1 = (float) getXForOval(a, b, top + 50);
            float y1 = top + 50;
            float x2 = x1 + 50;
            float y2 = y1;

            Log.d("coords", "compute: " + getXForOval(a, b, top + 50) + "");

            Log.d("coords", "leftOval.top: " + leftOval.top + "");
            Log.d("coords", "leftOval.bottom: " + leftOval.bottom + "");

            Log.d("coords", "leftOval.right: " + leftOval.right + "");
            Log.d("coords", "leftOval.left: " + leftOval.left + "");

            Log.d("coords", "a: " + a + "");
            Log.d("coords", "b: " + b + "");
            Log.d("coords", "x1: " + x1 + "");
            Log.d("coords", "y1: " + y1 + "");
            Log.d("coords", "x2: " + x2 + "");
            Log.d("coords", "y2: " + y2 + "");

            canvas.drawLine(x1, y1, x2, y2, paint);

    private double getXForOval(float a, float b, float y) {
        // sqrt ( a^2 * (1 - y^2 / b^2) )

        // @formatter:off
        return Math.sqrt(
                        Math.abs(
                            Math.pow(a, 2) *    (   1 - 
                                                    ( Math.pow(y, 2) / Math.pow(b, 2) )
                                                )
                                )
                        );

        // @formatter:on
    }

But the X value is coming out way large. What am I doing wrong?

08-27 18:16:56.574: DEBUG/coords(2785): compute: 2743.647207641682
08-27 18:16:56.584: DEBUG/coords(2785): leftOval.top: 180.0
08-27 18:16:56.584: DEBUG/coords(2785): leftOval.bottom: 780.0
08-27 18:16:56.584: DEBUG/coords(2785): leftOval.right: 185.0
08-27 18:16:56.584: DEBUG/coords(2785): leftOval.left: 135.0
08-27 18:16:56.584: DEBUG/coords(2785): a: 300.0
08-27 18:16:56.584: DEBUG/coords(2785): b: 25.0
08-27 18:16:56.584: DEBUG/coords(2785): x1: 2743.6472
08-27 18:16:56.584: DEBUG/coords(2785): y1: 230.0
08-27 18:16:56.584: DEBUG/coords(2785): x2: 2793.6472
08-27 18:16:56.584: DEBUG/coords(2785): y2: 230.0

解决方案

The math isn't terribly complex. The formula for an ellipse is x^2 / a^2 + y^2 / b^2 = 1 where a and b are the lengths of the major and minor axes, which are constant. You need to find x for a given y which is sqrt ( a^2 * (1 - y^2 / b^2) ) . This will get you the x,y offset from the center of the left oval and the length of the line is constant. Use the sine function to animate your y values and it should look good.

Edit:

Sorry about that, my comment about a and b should have mentioned that you had them reversed. Also, you have to use the y coordinate relative to the center of the oval.

Multiplying a number by itself is faster than using Math.pow when you are squaring it.

        // a should be half the width
        float a = (leftOval.right - leftOval.left) / 2;
        // and b half the height
        float b = (leftOval.bottom - leftOval.top) / 2;

        float yCenter = (leftOval.top + leftOval.bottom) / 2;
        float xCenter = (leftOval.right + leftOval.left) / 2;

        float x1 = (float) getXForOval(a, b, top + 150, xCenter, yCenter);
        float y1 = top + 150;
        float x2 = x1 + 50;
        float y2 = y1;

        Log.d("coords",
                "compute: " + getXForOval(a, b, top + 50, xCenter, yCenter)
                        + "");

        Log.d("coords", "leftOval.top: " + leftOval.top + "");
        Log.d("coords", "leftOval.bottom: " + leftOval.bottom + "");

        Log.d("coords", "leftOval.right: " + leftOval.right + "");
        Log.d("coords", "leftOval.left: " + leftOval.left + "");

        Log.d("coords", "a: " + a + "");
        Log.d("coords", "b: " + b + "");
        Log.d("coords", "x1: " + x1 + "");
        Log.d("coords", "y1: " + y1 + "");
        Log.d("coords", "x2: " + x2 + "");
        Log.d("coords", "y2: " + y2 + "");

        canvas.drawLine(x1, y1, x2, y2, paint);
    }
}

private double getXForOval(float a, float b, float y, float xCenter,
        float yCenter) {
    // sqrt ( a^2 * (1 - y^2 / b^2) )

    // calculation based on values relative to the center
    float yOffset = y - yCenter;

    // @formatter:off 
    return Math.sqrt( 
                    Math.abs( 
                        a * a *    (   1 -  
                                                ( (yOffset * yOffset) / (b * b) ) 
                                            ) 
                            ) 
                    ) + xCenter; 
   // @formatter:on
}

这篇关于获得在Android的一个弧形或椭圆形点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆