# obtener puntos en un arco u óvalo en Android

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?

Gracias de antemano!

EDICIÓN # 1:

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

Esto es lo que tengo hasta ahora:

``````            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
``````

preguntado el 27 de agosto de 11 a las 21:08

## 2 Respuestas

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
}
``````

Respondido 28 ago 11, 10:08

Android draws ovals with rectangles, does this mean that a is height of the rectangle, and b is the width? +1 - javamonkey79

@javamonkey half the height and half the width. - IronMensan

@javamonkey Sorry about that, a is half the width and b is half the height - see edit - IronMensan

I am very appreciative of your help, however, I pasted in what you put above and it still does not work. The computed x value is not crazy large, but it is still not right. - javamonkey79

The final value must be added to the xCenter, this solves it. Thanks. - javamonkey79

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.

Respondido 28 ago 11, 01:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.