Example usage for java.lang Math toDegrees

List of usage examples for java.lang Math toDegrees

Introduction

In this page you can find the example usage for java.lang Math toDegrees.

Prototype

public static double toDegrees(double angrad) 

Source Link

Document

Converts an angle measured in radians to an approximately equivalent angle measured in degrees.

Usage

From source file:ExtendedGeneralPath.java

/**
 * This constructs an unrotated Arc2D from the SVG specification of an
 * Elliptical arc.  To get the final arc you need to apply a rotation
 * transform such as://w ww  .  j  av  a 2s . com
 *
 * AffineTransform.getRotateInstance
 *     (angle, arc.getX()+arc.getWidth()/2, arc.getY()+arc.getHeight()/2);
 */
public static Arc2D computeArc(double x0, double y0, double rx, double ry, double angle, boolean largeArcFlag,
        boolean sweepFlag, double x, double y) {
    //
    // Elliptical arc implementation based on the SVG specification notes
    //

    // Compute the half distance between the current and the final point
    double dx2 = (x0 - x) / 2.0;
    double dy2 = (y0 - y) / 2.0;
    // Convert angle from degrees to radians
    angle = Math.toRadians(angle % 360.0);
    double cosAngle = Math.cos(angle);
    double sinAngle = Math.sin(angle);

    //
    // Step 1 : Compute (x1, y1)
    //
    double x1 = (cosAngle * dx2 + sinAngle * dy2);
    double y1 = (-sinAngle * dx2 + cosAngle * dy2);
    // Ensure radii are large enough
    rx = Math.abs(rx);
    ry = Math.abs(ry);
    double Prx = rx * rx;
    double Pry = ry * ry;
    double Px1 = x1 * x1;
    double Py1 = y1 * y1;
    // check that radii are large enough
    double radiiCheck = Px1 / Prx + Py1 / Pry;
    if (radiiCheck > 1) {
        rx = Math.sqrt(radiiCheck) * rx;
        ry = Math.sqrt(radiiCheck) * ry;
        Prx = rx * rx;
        Pry = ry * ry;
    }

    //
    // Step 2 : Compute (cx1, cy1)
    //
    double sign = (largeArcFlag == sweepFlag) ? -1 : 1;
    double sq = ((Prx * Pry) - (Prx * Py1) - (Pry * Px1)) / ((Prx * Py1) + (Pry * Px1));
    sq = (sq < 0) ? 0 : sq;
    double coef = (sign * Math.sqrt(sq));
    double cx1 = coef * ((rx * y1) / ry);
    double cy1 = coef * -((ry * x1) / rx);

    //
    // Step 3 : Compute (cx, cy) from (cx1, cy1)
    //
    double sx2 = (x0 + x) / 2.0;
    double sy2 = (y0 + y) / 2.0;
    double cx = sx2 + (cosAngle * cx1 - sinAngle * cy1);
    double cy = sy2 + (sinAngle * cx1 + cosAngle * cy1);

    //
    // Step 4 : Compute the angleStart (angle1) and the angleExtent (dangle)
    //
    double ux = (x1 - cx1) / rx;
    double uy = (y1 - cy1) / ry;
    double vx = (-x1 - cx1) / rx;
    double vy = (-y1 - cy1) / ry;
    double p, n;
    // Compute the angle start
    n = Math.sqrt((ux * ux) + (uy * uy));
    p = ux; // (1 * ux) + (0 * uy)
    sign = (uy < 0) ? -1.0 : 1.0;
    double angleStart = Math.toDegrees(sign * Math.acos(p / n));

    // Compute the angle extent
    n = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
    p = ux * vx + uy * vy;
    sign = (ux * vy - uy * vx < 0) ? -1.0 : 1.0;
    double angleExtent = Math.toDegrees(sign * Math.acos(p / n));
    if (!sweepFlag && angleExtent > 0) {
        angleExtent -= 360f;
    } else if (sweepFlag && angleExtent < 0) {
        angleExtent += 360f;
    }
    angleExtent %= 360f;
    angleStart %= 360f;

    //
    // We can now build the resulting Arc2D in double precision
    //
    Arc2D.Double arc = new Arc2D.Double();
    arc.x = cx - rx;
    arc.y = cy - ry;
    arc.width = rx * 2.0;
    arc.height = ry * 2.0;
    arc.start = -angleStart;
    arc.extent = -angleExtent;

    return arc;
}

From source file:com.taobao.weex.ui.animation.TransformParser.java

public static Map<Property<View, Float>, Float> parseTransForm(@Nullable String rawTransform, final int width,
        final int height, final int viewportW) {
    if (!TextUtils.isEmpty(rawTransform)) {
        FunctionParser<Property<View, Float>, Float> parser = new FunctionParser<>(rawTransform,
                new FunctionParser.Mapper<Property<View, Float>, Float>() {
                    @Override/*  w ww  .  j  a va2s  . c om*/
                    public Map<Property<View, Float>, Float> map(String functionName, List<String> raw) {
                        if (raw != null && !raw.isEmpty()) {
                            if (wxToAndroidMap.containsKey(functionName)) {
                                return convertParam(width, height, viewportW, wxToAndroidMap.get(functionName),
                                        raw);
                            }
                        }
                        return new HashMap<>();
                    }

                    private Map<Property<View, Float>, Float> convertParam(int width, int height, int viewportW,
                            @NonNull List<Property<View, Float>> propertyList, @NonNull List<String> rawValue) {

                        Map<Property<View, Float>, Float> result = WXDataStructureUtil
                                .newHashMapWithExpectedSize(propertyList.size());
                        List<Float> convertedList = new ArrayList<>(propertyList.size());
                        if (propertyList.contains(View.ROTATION) || propertyList.contains(View.ROTATION_X)
                                || propertyList.contains(View.ROTATION_Y)) {
                            convertedList.addAll(parseRotationZ(rawValue));
                        } else if (propertyList.contains(View.TRANSLATION_X)
                                || propertyList.contains(View.TRANSLATION_Y)) {
                            convertedList
                                    .addAll(parseTranslation(propertyList, width, height, rawValue, viewportW));
                        } else if (propertyList.contains(View.SCALE_X) || propertyList.contains(View.SCALE_Y)) {
                            convertedList.addAll(parseScale(propertyList.size(), rawValue));
                        } else if (propertyList.contains(CameraDistanceProperty.getInstance())) {
                            convertedList.add(parseCameraDistance(rawValue));
                        }
                        if (propertyList.size() == convertedList.size()) {
                            for (int i = 0; i < propertyList.size(); i++) {
                                result.put(propertyList.get(i), convertedList.get(i));
                            }
                        }
                        return result;
                    }

                    private List<Float> parseScale(int size, @NonNull List<String> rawValue) {
                        List<Float> convertedList = new ArrayList<>(rawValue.size() * 2);
                        List<Float> rawFloat = new ArrayList<>(rawValue.size());
                        for (String item : rawValue) {
                            rawFloat.add(WXUtils.fastGetFloat(item));
                        }
                        convertedList.addAll(rawFloat);
                        if (size != 1 && rawValue.size() == 1) {
                            convertedList.addAll(rawFloat);
                        }
                        return convertedList;
                    }

                    private @NonNull List<Float> parseRotationZ(@NonNull List<String> rawValue) {
                        List<Float> convertedList = new ArrayList<>(1);
                        int suffix;
                        for (String raw : rawValue) {
                            if ((suffix = raw.lastIndexOf(DEG)) != -1) {
                                convertedList.add(WXUtils.fastGetFloat(raw.substring(0, suffix)));
                            } else {
                                convertedList.add((float) Math.toDegrees(Double.parseDouble(raw)));
                            }
                        }
                        return convertedList;
                    }

                    /**
                     * As "translate(50%, 25%)" or "translate(25px, 30px)" both are valid,
                     * parsing translate is complicated than other method.
                     * Add your waste time here if you try to optimize this method like {@link #parseScale(int, List)}
                     * Time: 0.5h
                     */
                    private List<Float> parseTranslation(List<Property<View, Float>> propertyList, int width,
                            int height, @NonNull List<String> rawValue, int viewportW) {
                        List<Float> convertedList = new ArrayList<>(2);
                        String first = rawValue.get(0);
                        if (propertyList.size() == 1) {
                            parseSingleTranslation(propertyList, width, height, convertedList, first,
                                    viewportW);
                        } else {
                            parseDoubleTranslation(width, height, rawValue, convertedList, first, viewportW);
                        }
                        return convertedList;
                    }

                    private void parseSingleTranslation(List<Property<View, Float>> propertyList, int width,
                            int height, List<Float> convertedList, String first, int viewportW) {
                        if (propertyList.contains(View.TRANSLATION_X)) {
                            convertedList.add(parsePercentOrPx(first, width, viewportW));
                        } else if (propertyList.contains(View.TRANSLATION_Y)) {
                            convertedList.add(parsePercentOrPx(first, height, viewportW));
                        }
                    }

                    private void parseDoubleTranslation(int width, int height, @NonNull List<String> rawValue,
                            List<Float> convertedList, String first, int viewportW) {
                        String second;
                        if (rawValue.size() == 1) {
                            second = first;
                        } else {
                            second = rawValue.get(1);
                        }
                        convertedList.add(parsePercentOrPx(first, width, viewportW));
                        convertedList.add(parsePercentOrPx(second, height, viewportW));
                    }

                    private Float parseCameraDistance(List<String> rawValue) {
                        float ret = Float.MAX_VALUE;
                        if (rawValue.size() == 1) {
                            float value = WXViewUtils.getRealPxByWidth(WXUtils.getFloat(rawValue.get(0)),
                                    viewportW);
                            float scale = WXEnvironment.getApplication().getResources()
                                    .getDisplayMetrics().density;
                            if (!Float.isNaN(value) && value > 0) {
                                ret = value * scale;
                            }
                        }
                        return ret;
                    }
                });
        return parser.parse();
    }
    return new LinkedHashMap<>();
}

From source file:com.piusvelte.mosaic.android.MosaicMap.java

private double getOffsetLatitude(double lat, double offset) {
    return lat + Math.toDegrees(offset / Mosaic.EARTH_RADIUS);
}

From source file:com.piusvelte.mosaic.android.MosaicMap.java

private double getOffsetLongitude(double lat, double lng, double offset) {
    return lng + Math.toDegrees(offset / (Mosaic.EARTH_RADIUS * Math.cos(Math.toRadians(lat))));
}

From source file:com.example.casthelloworld.MainActivity.java

@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
        return;//from  www  .  j a  v  a 2  s.co  m
    }

    SensorManager.getRotationMatrixFromVector(mRotationMatrix, sensorEvent.values);

    SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
            mRotationMatrix);

    SensorManager.getOrientation(mRotationMatrix, orientationVals);

    orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);

    float Roll = orientationVals[2];

    if (Roll < -85 && Roll > -95 && lastVal != 0.0f) {
        // Log.d("Middle: ", "" + Roll);
        lastVal = 0.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -95 && Roll > -105 && lastVal != -0.2f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.2f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -85 && Roll < -75 && lastVal != 0.2f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.2f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -105 && Roll > -115 && lastVal != -0.4f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.4f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -75 && Roll < -65 && lastVal != 0.4f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.4f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -115 && Roll > -125 && lastVal != -0.6f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.6f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -65 && Roll < -55 && lastVal != 0.6f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.6f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -125 && Roll > -135 && lastVal != -0.8f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.8f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -55 && Roll < -45 && lastVal != 0.8f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.8f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -135 && lastVal != -1.0f && lastVal != 1.0f) {
        // Log.d("Left", "" + Roll);
        lastVal = -1.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -45 && lastVal != 1.0f && lastVal != -1.0f) {
        // Log.d("Right", "" + Roll);
        lastVal = 1.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    }

}

From source file:ShapeTest.java

public Shape makeShape(Point2D[] p) {
    double centerX = (p[0].getX() + p[1].getX()) / 2;
    double centerY = (p[0].getY() + p[1].getY()) / 2;
    double width = Math.abs(p[1].getX() - p[0].getX());
    double height = Math.abs(p[1].getY() - p[0].getY());

    double skewedStartAngle = Math
            .toDegrees(Math.atan2(-(p[2].getY() - centerY) * width, (p[2].getX() - centerX) * height));
    double skewedEndAngle = Math
            .toDegrees(Math.atan2(-(p[3].getY() - centerY) * width, (p[3].getX() - centerX) * height));
    double skewedAngleDifference = skewedEndAngle - skewedStartAngle;
    if (skewedStartAngle < 0)
        skewedStartAngle += 360;/*from w  w w .  ja v a 2 s . c o m*/
    if (skewedAngleDifference < 0)
        skewedAngleDifference += 360;

    Arc2D s = new Arc2D.Double(0, 0, 0, 0, skewedStartAngle, skewedAngleDifference, Arc2D.OPEN);
    s.setFrameFromDiagonal(p[0], p[1]);

    GeneralPath g = new GeneralPath();
    g.append(s, false);
    Rectangle2D r = new Rectangle2D.Double();
    r.setFrameFromDiagonal(p[0], p[1]);
    g.append(r, false);
    Point2D center = new Point2D.Double(centerX, centerY);
    g.append(new Line2D.Double(center, p[2]), false);
    g.append(new Line2D.Double(center, p[3]), false);
    return g;
}

From source file:org.gearvrf.controls.util.VRSamplesTouchPadGesturesDetector.java

private SwipeDirection getSwipeDirection(float x1, float y1, float x2, float y2) {

    Double angle = Math.toDegrees(Math.atan2(y1 - y2, x2 - x1));

    if (angle > 45 && angle <= 135) {
        return SwipeDirection.Up;
    } else if (angle >= 135 && angle < 180 || angle < -135 && angle > -180) {
        return SwipeDirection.Ignore; // left to right
    } else if (angle < -45 && angle >= -135) {
        return SwipeDirection.Down;
    } else if (angle > -45 && angle <= 45) {
        return SwipeDirection.Ignore; // right to left
    }//  w  w w  .j  a v a  2 s  .  c o m

    return SwipeDirection.Ignore;
}

From source file:com.bc.jexp.impl.DefaultNamespace.java

private void registerDefaultFunctions() {
    registerFunction(new AbstractFunction.D("sin", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.sin(args[0].evalD(env));
        }/*from  w w  w  . j av  a 2 s  .  c o m*/
    });
    registerFunction(new AbstractFunction.D("cos", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.cos(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("tan", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.tan(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("asin", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.asin(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("acos", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.acos(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("atan", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.atan(args[0].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("atan2", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.atan2(args[0].evalD(env), args[1].evalD(env));
        }
    });
    registerFunction(new AbstractFunction.D("log", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.log(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("log10", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.log10(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("exp", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.exp(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("exp10", 1) {

        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return FastMath.pow(10.0, args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("sqr", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            double v = args[0].evalD(env);
            return v * v;
        }
    });

    registerFunction(new AbstractFunction.D("sqrt", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.sqrt(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("pow", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return FastMath.pow(args[0].evalD(env), args[1].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.I("min", 2) {

        public int evalI(final EvalEnv env, final Term[] args) {
            return Math.min(args[0].evalI(env), args[1].evalI(env));
        }
    });

    registerFunction(new AbstractFunction.D("min", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.min(args[0].evalD(env), args[1].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.I("max", 2) {

        public int evalI(final EvalEnv env, final Term[] args) {
            return Math.max(args[0].evalI(env), args[1].evalI(env));
        }
    });

    registerFunction(new AbstractFunction.D("max", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.max(args[0].evalD(env), args[1].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("floor", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.floor(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("round", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.round(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("ceil", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.ceil(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("rint", 1) {
        public double evalD(EvalEnv env, Term[] args) throws EvalException {
            return Math.rint(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.I("sign", 1) {

        public int evalI(final EvalEnv env, final Term[] args) {
            return ExtMath.sign(args[0].evalI(env));
        }
    });

    registerFunction(new AbstractFunction.D("sign", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return ExtMath.sign(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.I("abs", 1) {

        public int evalI(final EvalEnv env, final Term[] args) {
            return Math.abs(args[0].evalI(env));
        }
    });

    registerFunction(new AbstractFunction.D("abs", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.abs(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("deg", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.toDegrees(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("rad", 1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            return Math.toRadians(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("ampl", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            final double a = args[0].evalD(env);
            final double b = args[1].evalD(env);
            return Math.sqrt(a * a + b * b);
        }
    });

    registerFunction(new AbstractFunction.D("phase", 2) {

        public double evalD(final EvalEnv env, final Term[] args) {
            final double a = args[0].evalD(env);
            final double b = args[1].evalD(env);
            return Math.atan2(b, a);
        }
    });

    registerFunction(new AbstractFunction.B("feq", 2) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            final double x1 = args[0].evalD(env);
            final double x2 = args[1].evalD(env);
            return ExtMath.feq(x1, x2, EPS);
        }
    });

    registerFunction(new AbstractFunction.B("feq", 3) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            final double x1 = args[0].evalD(env);
            final double x2 = args[1].evalD(env);
            final double eps = args[2].evalD(env);
            return ExtMath.feq(x1, x2, eps);
        }
    });

    registerFunction(new AbstractFunction.B("fneq", 2) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            final double x1 = args[0].evalD(env);
            final double x2 = args[1].evalD(env);
            return ExtMath.fneq(x1, x2, EPS);
        }
    });

    registerFunction(new AbstractFunction.B("fneq", 3) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            final double x1 = args[0].evalD(env);
            final double x2 = args[1].evalD(env);
            final double eps = args[2].evalD(env);
            return ExtMath.fneq(x1, x2, eps);
        }
    });

    registerFunction(new AbstractFunction.B("inf", 1) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            return Double.isInfinite(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.B("nan", 1) {
        public boolean evalB(EvalEnv env, Term[] args) throws EvalException {
            return Double.isNaN(args[0].evalD(env));
        }
    });

    registerFunction(new AbstractFunction.D("distance", -1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            double sqrSum = 0.0;
            final int n = args.length / 2;
            for (int i = 0; i < n; i++) {
                final double v = args[i + n].evalD(env) - args[i].evalD(env);
                sqrSum += v * v;
            }
            return Math.sqrt(sqrSum);
        }
    });

    registerFunction(new AbstractFunction.D("distance_deriv", -1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            double sqrSum = 0.0;
            final int n = args.length / 2;
            for (int i = 0; i < n - 1; i++) {
                final double v1 = args[i + 1].evalD(env) - args[i].evalD(env);
                final double v2 = args[i + n + 1].evalD(env) - args[i + n].evalD(env);
                sqrSum += (v1 - v2) * (v1 - v2);
            }
            return Math.sqrt(sqrSum);
        }
    });

    registerFunction(new AbstractFunction.D("distance_integ", -1) {

        public double evalD(final EvalEnv env, final Term[] args) {
            double sqrSum = 0.0;
            double v1Sum = 0.0;
            double v2Sum = 0.0;
            final int n = args.length / 2;
            for (int i = 0; i < n; i++) {
                v1Sum += args[i].evalD(env);
                v2Sum += args[i + n].evalD(env);
                sqrSum += (v2Sum - v1Sum) * (v2Sum - v1Sum);
            }
            return Math.sqrt(sqrSum);
        }
    });

    registerFunction(new AbstractFunction.B("inrange", -1) {

        public boolean evalB(final EvalEnv env, final Term[] args) {
            final int n1 = args.length / 3;
            final int n2 = n1 + args.length / 3;
            for (int i = 0; i < n1; i++) {
                final double v = args[i].evalD(env);
                final double v1 = args[i + n1].evalD(env);
                final double v2 = args[i + n2].evalD(env);
                if (v < v1 || v > v2) {
                    return false;
                }
            }
            return true;
        }
    });

    registerFunction(new AbstractFunction.B("inrange_deriv", -1) {

        public boolean evalB(final EvalEnv env, final Term[] args) {
            final int n1 = args.length / 3;
            final int n2 = 2 * n1;
            for (int i = 0; i < n1 - 1; i++) {
                final double v = args[i + 1].evalD(env) - args[i].evalD(env);
                final double v1 = args[i + n1 + 1].evalD(env) - args[i + n1].evalD(env);
                final double v2 = args[i + n2 + 1].evalD(env) - args[i + n2].evalD(env);
                if (v < v1 || v > v2) {
                    return false;
                }
            }
            return true;
        }
    });

    registerFunction(new AbstractFunction.B("inrange_integ", -1) {

        public boolean evalB(final EvalEnv env, final Term[] args) {
            final int n1 = args.length / 3;
            final int n2 = 2 * n1;
            double vSum = 0.0;
            double v1Sum = 0.0;
            double v2Sum = 0.0;
            for (int i = 0; i < n1; i++) {
                vSum += args[i].evalD(env);
                v1Sum += args[i + n1].evalD(env);
                v2Sum += args[i + n2].evalD(env);
                if (vSum < v1Sum || vSum > v2Sum) {
                    return false;
                }
            }
            return true;
        }
    });

}

From source file:com.nextgis.maplibui.fragment.CompassFragment.java

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDownX = event.getX();// w w  w. ja v a2  s . c o  m
        mDownY = event.getY();
        return true;
    case MotionEvent.ACTION_MOVE:
        float upX = event.getX();
        float upY = event.getY();

        double downR = Math.atan2(v.getHeight() / 2 - mDownY, mDownX - v.getWidth() / 2);
        int angle1 = (int) Math.toDegrees(downR);

        double upR = Math.atan2(v.getHeight() / 2 - upY, upX - v.getWidth() / 2);
        int angle2 = (int) Math.toDegrees(upR);

        this.rotateCompass(angle1 - angle2);

        if (mIsVibrationOn) {
            mVibrator.vibrate(5);
        }

        // update starting point for next move event
        mDownX = upX;
        mDownY = upY;

        return true;
    }
    return false;
}

From source file:io.github.data4all.handler.TagSuggestionHandler.java

/**
 * Get a bounding box of the location./*from  w w  w.  ja v a  2s  . co  m*/
 * 
 * @param lat
 *            latitude of the location
 * @param lon
 *            longitude of the location
 * @param radius
 *            distance to bounds from location
 * @return a boundingBox
 */
private static double[] getBoundingBox(double lat, double lon, double radius) {
    final double result[] = new double[4];
    result[0] = lat - Math.toDegrees(radius / (double) EARTH_RADIUS); // s
    result[1] = lon - Math.toDegrees(radius / (double) EARTH_RADIUS / Math.cos(Math.toRadians(lat))); // w
    result[2] = lat + Math.toDegrees(radius / (double) EARTH_RADIUS); // n
    result[3] = lon + Math.toDegrees(radius / (double) EARTH_RADIUS / Math.cos(Math.toRadians(lat))); // e
    return result;
}