Example usage for android.util FloatMath cos

List of usage examples for android.util FloatMath cos

Introduction

In this page you can find the example usage for android.util FloatMath cos.

Prototype

public static float cos(float angle) 

Source Link

Document

Returns the closest float approximation of the cosine of the argument.

Usage

From source file:com.jiahuan.svgmapview.core.helper.map.SVGParser.java

private static void drawArc(Path p, float lastX, float lastY, float x, float y, float rx, float ry, float theta,
        int largeArc, int sweepArc) {
    // Log.d("drawArc", "from (" + lastX + "," + lastY + ") to (" + x + ","+
    // y + ") r=(" + rx + "," + ry +
    // ") theta=" + theta + " flags="+ largeArc + "," + sweepArc);

    // http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes

    if (rx == 0 || ry == 0) {
        p.lineTo(x, y);//www . j  a  v  a 2s  . c om
        return;
    }

    if (x == lastX && y == lastY) {
        return; // nothing to draw
    }

    rx = Math.abs(rx);
    ry = Math.abs(ry);

    final float thrad = theta * (float) Math.PI / 180;
    final float st = FloatMath.sin(thrad);
    final float ct = FloatMath.cos(thrad);

    final float xc = (lastX - x) / 2;
    final float yc = (lastY - y) / 2;
    final float x1t = ct * xc + st * yc;
    final float y1t = -st * xc + ct * yc;

    final float x1ts = x1t * x1t;
    final float y1ts = y1t * y1t;
    float rxs = rx * rx;
    float rys = ry * ry;

    float lambda = (x1ts / rxs + y1ts / rys) * 1.001f; // add 0.1% to be
    // sure that no out
    // of range occurs
    // due to
    // limited precision
    if (lambda > 1) {
        float lambdasr = FloatMath.sqrt(lambda);
        rx *= lambdasr;
        ry *= lambdasr;
        rxs = rx * rx;
        rys = ry * ry;
    }

    final float R = FloatMath.sqrt((rxs * rys - rxs * y1ts - rys * x1ts) / (rxs * y1ts + rys * x1ts))
            * ((largeArc == sweepArc) ? -1 : 1);
    final float cxt = R * rx * y1t / ry;
    final float cyt = -R * ry * x1t / rx;
    final float cx = ct * cxt - st * cyt + (lastX + x) / 2;
    final float cy = st * cxt + ct * cyt + (lastY + y) / 2;

    final float th1 = angle(1, 0, (x1t - cxt) / rx, (y1t - cyt) / ry);
    float dth = angle((x1t - cxt) / rx, (y1t - cyt) / ry, (-x1t - cxt) / rx, (-y1t - cyt) / ry);

    if (sweepArc == 0 && dth > 0) {
        dth -= 360;
    } else if (sweepArc != 0 && dth < 0) {
        dth += 360;
    }

    // draw
    if ((theta % 360) == 0) {
        // no rotate and translate need
        arcRectf.set(cx - rx, cy - ry, cx + rx, cy + ry);
        p.arcTo(arcRectf, th1, dth);
    } else {
        // this is the hard and slow part :-)
        arcRectf.set(-rx, -ry, rx, ry);

        arcMatrix.reset();
        arcMatrix.postRotate(theta);
        arcMatrix.postTranslate(cx, cy);
        arcMatrix.invert(arcMatrix2);

        p.transform(arcMatrix2);
        p.arcTo(arcRectf, th1, dth);
        p.transform(arcMatrix);
    }
}