开发者

get points on an arc or oval in Android

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

get points on an arc or oval in Android

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 anyth开发者_开发技巧ing 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
}


I dont know of any calls that let you get points of the drawn object.

I would write a function to loop for how ever many lines need to be drawn and and just have a bit of math to find your xStart and xEnd coordinates of the line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜