0
\$\begingroup\$

How to find the percentage value from A to B when I know the X value in between these two points?

I created this picture to kind of illustrate what I mean better.

enter image description here

I wrote this function which gets the percentage, but how do I also include the center offset?

private float GetPercentage(Vector2 point, float radiusOffset, float centerOffset)
{
    float radius = Math.Min(circle.rect.width / 2f, circle.rect.height / 2f);

    radius *= radiusOffset;

    return 1f - (Math.Max(Math.Abs(point.x), Math.Abs(point.y)) / radius);
}

The radius offset is from 0-1, 0 being the 0,0 position and 1 full radius. The center offset is from 1-0, 1 being the 0,0 position and 0 being the radius border.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I’m assuming you mean you want the percentage of X between B and A, where at 0.0, X=B and at 1.0, X=A. The formula should be this: (pseudo code, I don’t know unity’s math functions)

float r = dist(X, center);
return (r - innerRadius) / (outerRadius - innerRadius);

You can calculate innerRadius and outerRadius with the radius and center offsets. Your description is little unclear so figure out how to do that yourself.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .