본문 바로가기
알고리즘

벡터(스크롤)을 스칼라 값(회전)으로 변경하는 예시

by hansoo.labs 한수댁 2013. 7. 29.

벡터(스크롤)을 스칼라 값(회전)으로 변경하는 예시
- 안드로이드 PieChart 예시에서.

/**
/**
 * Helper method for translating (x,y) scroll vectors into scalar rotation of the pie.
 *
 * @param dx The x component of the current scroll vector.
 * @param dy The y component of the current scroll vector.
 * @param x  The x position of the current touch, relative to the pie center.
 * @param y  The y position of the current touch, relative to the pie center.
 * @return The scalar representing the change in angular position for this scroll.
 */
private static float vectorToScalarScroll(float dx, float dy, float x, float y) {
    // get the length of the vector
    float l = (float) Math.sqrt(dx * dx + dy * dy);

    // decide if the scalar should be negative or positive by finding
    // the dot product of the vector perpendicular to (x,y). 
    float crossX = -y;
    float crossY = x;

    float dot = (crossX * dx + crossY * dy);
    float sign = Math.signum(dot);

    return l * sign;
}



댓글0