Example usage for java.lang Math atan2

List of usage examples for java.lang Math atan2

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double atan2(double y, double x) 

Source Link

Document

Returns the angle theta from the conversion of rectangular coordinates ( x ,  y ) to polar coordinates (r, theta).

Usage

From source file:org.shaman.terrain.sketch.SketchTerrain_old.java

protected void drawLineBresenham(int x0, int y0, int x1, int y1, int thickness, ColorRGBA color,
        ImagePainter img) {//from   w w w. j a va2  s.co  m
    final double theta = Math.atan2(y1 - y0, x1 - x0);
    thickness = (int) Math.round(thickness * Math.max(Math.abs(Math.cos(theta)), Math.abs(Math.sin(theta))));

    final int offset = thickness / 2;
    final int extra = thickness % 2;

    // implementation of Bresenham's algorithm from Wikipedia.
    int Dx = x1 - x0;
    int Dy = y1 - y0;
    final boolean steep = (Math.abs(Dy) >= Math.abs(Dx));
    if (steep) {
        int tmp;
        // SWAP(x0, y0);
        tmp = x0;
        x0 = y0;
        y0 = tmp;
        // SWAP(x1, y1);
        tmp = x1;
        x1 = y1;
        y1 = tmp;

        // recompute Dx, Dy after swap
        Dx = x1 - x0;
        Dy = y1 - y0;
    }
    int xstep = 1;
    if (Dx < 0) {
        xstep = -1;
        Dx = -Dx;
    }
    int ystep = 1;
    if (Dy < 0) {
        ystep = -1;
        Dy = -Dy;
    }
    final int TwoDy = 2 * Dy;
    final int TwoDyTwoDx = TwoDy - 2 * Dx; // 2*Dy - 2*Dx
    int E = TwoDy - Dx; // 2*Dy - Dx
    int y = y0;
    int xDraw, yDraw;
    for (int x = x0; x != x1; x += xstep) {
        if (steep) {
            xDraw = y;
            yDraw = x;
        } else {
            xDraw = x;
            yDraw = y;
        }
        // plot
        if (xDraw >= 0 && xDraw < width && yDraw >= 0 && yDraw < height) {
            if (thickness == 1) {
                img.paintPixel(xDraw, yDraw, color, ImagePainter.BlendMode.SET);
            } else if (thickness > 1) {
                for (int yy = yDraw - offset; yy < yDraw + offset + extra; yy++) {
                    for (int xx = xDraw - offset; xx < xDraw + offset + extra; xx++) {
                        if (xx >= 0 && yy >= 0 && xx < width && yy < height) {
                            img.paintPixel(xx, yy, color, ImagePainter.BlendMode.SET);
                        }
                    }
                }
            }
        }

        // next
        if (E > 0) {
            E += TwoDyTwoDx; // E += 2*Dy - 2*Dx;
            y += ystep;
        } else {
            E += TwoDy; // E += 2*Dy;
        }
    }
}

From source file:eu.trentorise.smartcampus.mobility.util.GamificationHelper.java

private static double harvesineDistance(double lat1, double lon1, double lat2, double lon2) {
    lat1 = Math.toRadians(lat1);//from   w w  w .ja  v  a  2 s. co m
    lon1 = Math.toRadians(lon1);
    lat2 = Math.toRadians(lat2);
    lon2 = Math.toRadians(lon2);

    double dlon = lon2 - lon1;
    double dlat = lat2 - lat1;

    double a = Math.pow((Math.sin(dlat / 2)), 2)
            + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / 2), 2);

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return EARTH_RADIUS * c;
}

From source file:info.bartowski.easteregg.MLand.java

@Override
public void onDraw(Canvas c) {
    super.onDraw(c);

    if (SHOW_TOUCHES) {
        for (Player p : mPlayers) {
            if (p.mTouchX > 0) {
                mTouchPaint.setColor(0x80FFFFFF & p.color);
                mPlayerTracePaint.setColor(0x80FFFFFF & p.color);
                float x1 = p.mTouchX;
                float y1 = p.mTouchY;
                c.drawCircle(x1, y1, 100, mTouchPaint);
                float x2 = p.getX() + p.getPivotX();
                float y2 = p.getY() + p.getPivotY();
                float angle = PI_2 - (float) Math.atan2(x2 - x1, y2 - y1);
                x1 += 100 * Math.cos(angle);
                y1 += 100 * Math.sin(angle);
                c.drawLine(x1, y1, x2, y2, mPlayerTracePaint);
            }/*from  w ww  . jav a2 s .c  om*/
        }
    }

    if (!DEBUG_DRAW)
        return;

    final Paint pt = new Paint();
    pt.setColor(0xFFFFFFFF);
    for (Player p : mPlayers) {
        final int L = p.corners.length;
        final int N = L / 2;
        for (int i = 0; i < N; i++) {
            final int x = (int) p.corners[i * 2];
            final int y = (int) p.corners[i * 2 + 1];
            c.drawCircle(x, y, 4, pt);
            c.drawLine(x, y, p.corners[(i * 2 + 2) % L], p.corners[(i * 2 + 3) % L], pt);
        }
    }

    pt.setStyle(Paint.Style.STROKE);
    pt.setStrokeWidth(getResources().getDisplayMetrics().density);

    final int M = getChildCount();
    pt.setColor(0x8000FF00);
    for (int i = 0; i < M; i++) {
        final View v = getChildAt(i);
        if (v instanceof Player)
            continue;
        if (!(v instanceof GameView))
            continue;
        if (v instanceof Pop) {
            final Pop pop = (Pop) v;
            c.drawCircle(pop.cx, pop.cy, pop.r, pt);
        } else {
            final Rect r = new Rect();
            v.getHitRect(r);
            c.drawRect(r, pt);
        }
    }

    pt.setColor(Color.BLACK);
    final StringBuilder sb = new StringBuilder("obstacles: ");
    for (Obstacle ob : mObstaclesInPlay) {
        sb.append(ob.hitRect.toShortString());
        sb.append(" ");
    }
    pt.setTextSize(20f);
    c.drawText(sb.toString(), 20, 100, pt);
}

From source file:gdsc.utils.Cell_Outliner.java

/**
 * For the given point, compute the angle between pixels and the centre.
 * //  w  w  w  . ja  v a  2 s. co  m
 * @param ip
 * @param cx
 * @param cy
 * @param pointBounds
 */
private FloatProcessor createAngleProcessor(ImageProcessor ip, int cx, int cy, Rectangle pointBounds) {
    // Find the bounds to process
    int minx = pointBounds.x;
    int miny = pointBounds.y;
    int maxx = minx + pointBounds.width;
    int maxy = miny + pointBounds.height;

    FloatProcessor angle = new FloatProcessor(pointBounds.width, pointBounds.height);
    for (int y = miny, index = 0; y < maxy; y++) {
        for (int x = minx; x < maxx; x++, index++) {
            int dx = cx - x;
            int dy = cy - y;
            float a = (float) (Math.atan2(dy, dx) * 180.0 / Math.PI);
            angle.setf(index, a + 180f); // Convert to 0-360 domain
        }
    }
    return angle;
}

From source file:com.tumblr.cardboard.Tumblr3DActivity.java

private boolean isLookingAtObject(float[] initVec, float[] objPositionVec, int texIndex) {
    // Convert object space to camera space. Use the headView from onNewFrame.
    Matrix.multiplyMM(mModelView, 0, mHeadView, 0, mModelRect[texIndex], 0);
    Matrix.multiplyMV(objPositionVec, 0, mModelView, 0, initVec, 0);

    float pitch = (float) Math.atan2(objPositionVec[1], -objPositionVec[2]);
    float yaw = (float) Math.atan2(objPositionVec[0], -objPositionVec[2]);

    Log.v(TAG, "Object position: X: " + objPositionVec[0] + "  Y: " + objPositionVec[1] + " Z: "
            + objPositionVec[2]);//from ww  w.ja v  a2 s .c  o m
    Log.v(TAG, "Object Pitch: " + pitch + "  Yaw: " + yaw);

    return (Math.abs(pitch) < PITCH_LIMIT) && (Math.abs(yaw) < YAW_LIMIT);
}

From source file:com.example.aaron.test.MyGLSurfaceView.java

@Override
public boolean onTouchEvent(MotionEvent e) {

    float x = e.getX();
    float y = e.getY();

    int cc = 0;/*from   ww w  . j  ava  2  s. c o  m*/

    /*
            float xGL=(width1/2-x)/(float)(height1/1.85);
            float yGL=( height1/2+30-y)/(float)(height1/1.85);
    */

    float xGL = (width1 / 2 - x) / (float) (height1 / 2);
    float yGL = (height1 / 2 - y) / (float) (height1 / 2);

    workspace = -(width1 - 115) / (height1 * 2) + .15f - mRenderer.slider;

    mRenderer.tempFun(xGL, yGL);

    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        mRenderer.tToggle = 1;

        for (int i = 0; i < maxBots; i++) {
            if (tList[i].getOn() == 1) {
                cc = cc + 1;
                if (Math.abs(tList[i].getX() * getScale() + mRenderer.dragX * mRenderer.scale - xGL) < .1f
                        && Math.abs(
                                tList[i].getY() * getScale() + mRenderer.dragY * mRenderer.scale - yGL) < .1f) {
                    if (state[i] == 0) {
                        state[i] = 1;
                    } else {
                        state[i] = 0;
                    }
                    v.vibrate(50);
                }
            }
        }

        if (antispam > 1) {

            if (xGL < .85 && xGL > .65 && yGL > -.95 && yGL < -.75) {
                mRenderer.scale = mRenderer.scale + .5f;
                mRenderer.textList.get(1).setText("Scale: " + truncateDecimal(mRenderer.scale, 1) + "x");
                mRenderer.textList.get(2).setText(truncateDecimal(2 / mRenderer.scale, 2) + " ft");
                v.vibrate(75);
                break;
            }
            if (xGL < 1.2f && xGL > .95f && yGL > -.85f && yGL < -.75 && mRenderer.scale > .5f) {
                mRenderer.scale = mRenderer.scale - .5f;
                mRenderer.textList.get(1).setText("Scale: " + truncateDecimal(mRenderer.scale, 1) + "x");
                mRenderer.textList.get(2).setText(truncateDecimal(2 / mRenderer.scale, 2) + " ft");
                v.vibrate(75);
                break;
            }

            if (mRenderer.commit.blocked == false && xGL < mRenderer.commit.left - mRenderer.slider
                    && xGL > mRenderer.commit.right - mRenderer.slider && yGL > mRenderer.commit.down
                    && yGL < mRenderer.commit.up)
                if (mRenderer.commit.active == true) {
                    mRenderer.commit.active = false;
                    newAction = true;
                    v.vibrate(50);
                } else {
                    mRenderer.commit.active = true;
                    if (mRenderer.customFormation.active) {
                        mRenderer.justifyRelativePositions();
                        mRenderer.resetScaleAndRotation();
                        mRenderer.formation.send = true;
                    }
                    newAction = true;
                    v.vibrate(50);
                }

            //Turn on voronoi toggle
            if (buttonPush(xGL, yGL, mRenderer.vorToggle))
                if (mRenderer.getvToggle() == 1) {
                    mRenderer.setvToggle(0);
                    v.vibrate(500);
                } else {
                    mRenderer.setvToggle(1);
                    v.vibrate(500);
                }
            vFlag = mRenderer.getvToggle();

            //Turn on Free Draw Toggle
            if (buttonPush(xGL, yGL, mRenderer.freeDrawToggle))
                if (mRenderer.getfToggle() == 1) {
                    mRenderer.setfToggle(0);
                    fFlag = mRenderer.getfToggle();
                } else {
                    mRenderer.setfToggle(1);
                    mRenderer.eraseFreeLine();
                    freeDrawCount = 0;
                    previousy = 0;
                    previousx = 0;
                    connectable = 0;
                    firstPointFreeDraw[0] = -1000;
                    firstPointFreeDraw[1] = -1000;
                    fFlag = mRenderer.getfToggle();
                }

            //Toggable swarm for Path Follower
            /*if (mRenderer.getfToggle()==1 && xGL<mRenderer.swarmToggle.left- mRenderer.slider&& xGL>mRenderer.swarmToggle.right-mRenderer.slider&& yGL > mRenderer.swarmToggle.down && yGL < mRenderer.swarmToggle.up)
                if (mRenderer.swarmToggle.active == true) {
                    mRenderer.swarmToggle.active =  false;
                    v.vibrate(50);
                } else {
                    mRenderer.swarmToggle.active =  true;
                    v.vibrate(50);
                }
            */ //THIS FEATURE CAN BE ADDED LATER IF WE WANT

            //Turn on Way Point Toggle
            if (buttonPush(xGL, yGL, mRenderer.wayPointToggle))
                if (mRenderer.getpToggle() == 1) {
                    mRenderer.setpToggle(0);
                    pFlag2 = 0;
                    mRenderer.setpToggle2(pFlag2);
                    mRenderer.tToggle = 1;
                    pX = 0;
                    pY = 0;
                } else {
                    mRenderer.tToggle = 0;
                    mRenderer.setpToggle(1);
                    mRenderer.voronoiDeploymentToggle.active = false;
                    pX = 0;
                    pY = 0;
                }
            pFlag = mRenderer.getpToggle();

            //Turn on AndroneToggle
            if (buttonPush(xGL, yGL, mRenderer.ardronePrefToggle))
                if (mRenderer.getAPToggle() == 1) {
                    mRenderer.setAPToggle(0);
                    mRenderer.SINFO_FLAG = true;
                } else {
                    mRenderer.setAPToggle(1);
                    mRenderer.SINFO_FLAG = false;
                }

            if (buttonPush(xGL, yGL, mRenderer.gaussianTrackToggle)) {
                if (mRenderer.gaussianTrackToggle.active == true) {
                    mRenderer.gaussianTrackToggle.active = false;

                } else {
                    mRenderer.gaussianTrackToggle.active = true;

                }
                v.vibrate(50);
            }

            if (buttonPush(xGL, yGL, mRenderer.formation)) {
                if (mRenderer.formation.active == true) {
                    mRenderer.formation.active = false;
                    mRenderer.formation.send = false;
                } else {
                    mRenderer.formation.active = true;
                    mRenderer.formation.send = true;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.formationToggleTransform)) {
                if (mRenderer.formationToggleTransform.active == true) {
                    mRenderer.formationToggleTransform.active = false;
                } else {
                    mRenderer.formationToggleTransform.active = true;
                    mRenderer.customFormation.active = false;
                    mRenderer.commit.blocked = true;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.customFormation)) {
                if (mRenderer.customFormation.active == true) {
                    mRenderer.customFormation.active = false;
                    mRenderer.commit.blocked = true;
                } else {
                    mRenderer.customFormation.active = true;
                    mRenderer.resetFormation();
                    mRenderer.refreshCenter();
                    mRenderer.formationToggleTransform.active = false;
                    mRenderer.commit.blocked = false;
                    mRenderer.commit.active = false;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.ardroneAddToggle)) {
                dummyFlag = 1;
            }

            if (buttonPush(xGL, yGL, mRenderer.addToggle)) {
                mRenderer.formation.val1 = mRenderer.formation.val1 + 1;
                mRenderer.refreshFormationtext();
                mRenderer.formation.send = true;
            }

            if (buttonPush(xGL, yGL, mRenderer.subtractToggle)) {
                mRenderer.formation.val1 = mRenderer.formation.val1 - 1;
                mRenderer.refreshFormationtext();
                mRenderer.formation.send = true;
            }

            if (buttonPush(xGL, yGL, mRenderer.freeBoundarytoggle)) {
                if (mRenderer.freeBoundarytoggle.active == true) {
                    mRenderer.freeBoundarytoggle.active = false;
                    freeBoundaryFlag = true;
                    closedBoundaryFlag = false;
                } else {
                    mRenderer.freeBoundarytoggle.active = true;
                    closedBoundaryFlag = true;
                    freeBoundaryFlag = false;
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.obstacleLineToggle)) {
                System.out.println("obstacle Line Pushed");

                addObstacleFlag = true;
                mRenderer.makeObstacle();
                mRenderer.fToggle = 0;
                fFlag = mRenderer.fToggle;
            }

            //IF GAUSS TOGGLE SELECTED
            if (buttonPush(xGL, yGL, mRenderer.gaussToggle))
                if (mRenderer.getgToggle() == 1) {
                    mRenderer.setgToggle(0);
                    gFlag2 = 0;
                    mRenderer.setgToggle2(gFlag2);
                } else {
                    mRenderer.setgToggle(1);
                }
            gFlag = mRenderer.getgToggle();

            //IF GAUSSPATH TOGGLE SELECTED
            /*if (xGL<mRenderer.temptoggle.left- mRenderer.slider&& xGL>mRenderer.temptoggle.right-mRenderer.slider&& yGL > mRenderer.temptoggle.down && yGL < mRenderer.temptoggle.up)
                if (mRenderer.getgpToggle() == 1) {
                    mRenderer.setgpToggle(0);
                    mRenderer.eraseGaussLine();
                    v.vibrate(50);
                } else {
                    mRenderer.setgpToggle(1);
                    mRenderer.makeGaussPoints();
                    v.vibrate(50);
                }*/
            gpFlag = mRenderer.getgpToggle();

            //Toggle for voronoi deployment
            if (buttonPush(xGL, yGL, mRenderer.voronoiDeploymentToggle))
                if (mRenderer.voronoiDeploymentToggle.active == true) {
                    mRenderer.voronoiDeploymentToggle.active = false;
                    newAction = true;
                } else {
                    newAction = true;
                    mRenderer.voronoiDeploymentToggle.active = true;
                    mRenderer.setpToggle(0);
                    mRenderer.setpToggle2(0);
                }

            if (buttonPush(xGL, yGL, mRenderer.dragToggle)) {
                addedTurtleNumber = 0;
                addedTurtleX = .1f;
                addedTurtleY = .1f;
                for (int j = 41; j < 44; j++) {
                    if (tList[j].getOn() != 1) {
                        addedTurtleNumber = j;
                    }
                }
                if (addedTurtleNumber == 0) {
                    addTurtleFlag = false;
                } else {
                    addTurtleFlag = true;

                }
            }

            if (buttonPush(xGL, yGL, mRenderer.addTurtle)) {
                addedTurtleNumber = 0;
                addedTurtleX = .1f;
                addedTurtleY = .1f;
                for (int j = 1; j < 11; j++) {
                    if (tList[j].getOn() != 1) {
                        addedTurtleNumber = j;
                    }
                }
                if (addedTurtleNumber == 0) {
                    addTurtleFlag = false;
                } else {
                    addTurtleFlag = true;

                }
            }

            if (buttonPush(xGL, yGL, mRenderer.dragRobotToggle)) {
                if (mRenderer.dragRobotToggle.active == false) {
                    mRenderer.dragRobotToggle.active = true;
                } else {
                    mRenderer.dragRobotToggle.active = false;
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.gaussianPathToggle)) {
                if (mRenderer.gaussianPathToggle.active == true) {
                    mRenderer.gaussianPathToggle.active = false;
                    mRenderer.gaussPathPlay.active = false;
                } else {
                    mRenderer.gaussianPathToggle.active = true;
                    if (mRenderer.getfToggle() == 1) {//
                        mRenderer.gaussPathPlay.active = true;
                        mRenderer.updateGauss(
                                mRenderer.pathArray.pose[0].x * mRenderer.scale
                                        + mRenderer.dragX * mRenderer.scale,
                                mRenderer.pathArray.pose[0].y * mRenderer.scale
                                        + mRenderer.dragY * mRenderer.scale,
                                0);
                        gaussianPathArrayIteration = 1;
                        deltaX = (mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x)
                                / (float) Math.sqrt(Math
                                        .pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x, 2)
                                        + Math.pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y,
                                                2));
                        deltaY = (mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y)
                                / (float) Math.sqrt(Math
                                        .pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x, 2)
                                        + Math.pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y,
                                                2));

                    }
                }
            }

            if (xGL < mRenderer.clearAll.left - mRenderer.slider
                    && xGL > mRenderer.clearAll.right - mRenderer.slider && yGL > mRenderer.clearAll.down
                    && yGL < mRenderer.clearAll.up) {
                if (mRenderer.clearAll.active == true) {
                    mRenderer.clearAll.active = false;
                    v.vibrate(50);
                } else {
                    mRenderer.clearAll.active = true;
                    v.vibrate(50);
                }
            }

            if (xGL < mRenderer.resetToggle.left - mRenderer.slider
                    && xGL > mRenderer.resetToggle.right - mRenderer.slider && yGL > mRenderer.resetToggle.down
                    && yGL < mRenderer.resetToggle.up) {
                if (mRenderer.resetToggle.active == true) {
                    mRenderer.resetToggle.active = false;
                    v.vibrate(50);
                } else {
                    mRenderer.resetToggle.active = true;
                    v.vibrate(50);
                }
            }

            /*                    if (xGL<-(width1-90)/height1+.05f && xGL>-(width1-90)/height1 && yGL >-(height1-10)/(height1)-mRenderer.slider  && yGL < -(height1-10)/(height1)+05f-mRenderer.slider ){
                    
            }*/

            //Clear button
            if (xGL < mRenderer.clear.left - mRenderer.slider && xGL > mRenderer.clear.right - mRenderer.slider
                    && yGL > mRenderer.clear.down && yGL < mRenderer.clear.up) {
                if (mRenderer.getgToggle() == 1) {
                    gInd = 0;
                    mRenderer.clearGauss();

                }
                if (mRenderer.dragToggle.active == true) {
                    obsticle.on = 0;
                }

                deleteTurtleNumber = -1;
                for (int i = 0; i < maxBots; i++) {
                    if (tList[i].getState() == 1) {
                        deleteTurtleNumber = i;
                    }
                }
                if (deleteTurtleNumber != -1) {
                    deleteTurtleFlag = true;
                } else {
                    deleteTurtleFlag = false;
                }

                v.vibrate(75);
                mRenderer.clear.active = true;

            }

        }

        if (pFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL - mRenderer.dragX * mRenderer.scale;
            pY = yGL - mRenderer.dragY * mRenderer.scale;
            pFlag2 = 1;
            mRenderer.setpToggle2(pFlag2);
            mRenderer.setWayPointValues(pX, pY);
        }

        if (gFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            //pX=xGL;
            //pY=yGL;
            gFlag2 = 1;
            mRenderer.setgToggle2(gFlag2);

            //System.out.println("GAUSSIAN INDEX: " +gInd);
            //mRenderer.setWayPointValues(xGL, yGL);
            //mRenderer.setGaussValues(xGL, yGL, gInd);
            //mRenderer.setGaussScale(1f);
            if (gInd < 99) {
                //mRenderer.addGaussStuff(xGL, yGL, 1f,gInd);
                //TEMP FIX
                mRenderer.addGaussStuff(xGL, yGL, 1f, 0);
            }
            gInd++;
        }

        if (mRenderer.gToggle == 0 && mRenderer.fToggle == 0 && mRenderer.directionalDrag.active == true
                && (mRenderer.directionalDrag.active == true
                        && xGL < mRenderer.directionalDrag.left - mRenderer.slider
                        && xGL > mRenderer.directionalDrag.right - mRenderer.slider
                        && yGL > mRenderer.directionalDrag.down && yGL < mRenderer.directionalDrag.up)) {
            v.vibrate(50);
            if (xGL > mRenderer.directionalDrag.left
                    + (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX - .01f;
            }
            if (xGL < mRenderer.directionalDrag.left
                    + 2 * (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX + .01f;
            }

            if (yGL > mRenderer.directionalDrag.up
                    + (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY - .01f;
            }
            if (yGL < mRenderer.directionalDrag.up
                    + 2 * (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY + .01f;
            }
        }

    case MotionEvent.ACTION_POINTER_DOWN:
        //System.out.println("GAUSS 0");
        if (e.getActionIndex() == 1) {

            if (gFlag == 1) {
                float gaussX = e.getX(1);

                float gaussY = e.getY(1);

                float gauss_xGL = (width1 / 2 - gaussX) / (float) (height1 / 1.85);
                float gauss_yGL = (height1 / 2 + 30 - gaussY) / (float) (height1 / 1.85);

                float gauss_dx = gauss_xGL - xGL;
                float gauss_dy = gauss_yGL - yGL;

                float dgauss = (float) Math.sqrt(Math.pow(gauss_dx, 2) + Math.pow(gauss_dy, 2));

                gaussScale = dgauss / .2f;
                //System.out.println("SCALE");
                //mRenderer.addGaussStuff(xGL, yGL, gaussScale,gInd-1);
                //mRenderer.setGaussScale(gInd-1, gaussScale);
                //TEMP FIX
                mRenderer.setGaussScale(0, gaussScale);
            }
        }

    case MotionEvent.ACTION_MOVE:

        mRenderer.tToggle = 1;
        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        if (mRenderer.dragRobotToggle.active) {
            for (int i = 0; i < maxBots; i++) {
                if (tList[i].getOn() == 1) {
                    if (Math.abs(tList[i].getX() * getScale() + mRenderer.dragX * mRenderer.scale - xGL) < .1f
                            && Math.abs(tList[i].getY() * getScale() + mRenderer.dragY * mRenderer.scale
                                    - yGL) < .1f) {
                        addedTurtleNumber = i;
                        addedTurtleX = xGL / getScale() - mRenderer.dragX;
                        addedTurtleY = yGL / getScale() - mRenderer.dragY;
                        addTurtleFlag = true;
                        v.vibrate(50);
                    }
                }
            }

        }
        //pX+(float)Math.cos(formation.val4)*.3f+dragX*scale, pY+(float)Math.sin(formation.val4)*.3f+dragY*scale, 0);
        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(mRenderer.formation.x * mRenderer.scale
                    + (float) Math.cos(mRenderer.formation.val4) * .3f * mRenderer.scale
                    + mRenderer.dragX * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale
                            + (float) Math.sin(mRenderer.formation.val4) * .3f * mRenderer.scale
                            + mRenderer.dragY * mRenderer.scale - yGL) < .15f) {
                mRenderer.formation.val4 = (float) Math.atan2(
                        yGL - (mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale),
                        xGL - (mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale));
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.rotation_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.rotation_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(
                    mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale
                            - yGL) < .15f) {
                pX = xGL - mRenderer.scale * mRenderer.dragX;
                pY = yGL - mRenderer.scale * mRenderer.dragY;
                mRenderer.setWayPointValues(pX, pY);
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.center_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.center_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale
                    + mRenderer.formation.val3 * .2f * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale
                            - yGL) < .15f) {
                mRenderer.formation.val3 = (xGL
                        - (mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale)) / .2f
                        / mRenderer.scale;
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.scale_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.scale_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.customFormation.active) {
            for (int i = 0; i < mRenderer.formation.val1; i++) {
                if (target(convertToScreenPosition(mRenderer.formation_locations.x[i], true), xGL,
                        convertToScreenPosition(mRenderer.formation_locations.y[i], false), yGL, .15f)) {
                    //mRenderer.formation.x=mRenderer.formation.x*mRenderer.formation.val1-mRenderer.formation_locations.x[i];
                    //mRenderer.formation.y=mRenderer.formation.y*mRenderer.formation.val1-mRenderer.formation_locations.y[i];

                    mRenderer.formation_locations.x[i] = convertToGlobalPosition(xGL, true);
                    mRenderer.formation_locations.y[i] = convertToGlobalPosition(yGL, false);

                    //mRenderer.formation.x=mRenderer.formation.x+mRenderer.formation_locations.x[i];
                    //mRenderer.formation.x=mRenderer.formation.x/mRenderer.formation.val1;

                    //mRenderer.formation.y=mRenderer.formation.y+mRenderer.formation_locations.y[i];
                    //mRenderer.formation.y=mRenderer.formation.y/mRenderer.formation.val1;
                    mRenderer.refreshCenter();
                    break;
                }
            }
        }

        if (mRenderer.dragToggle.active == true && xGL > workspace && xGL < mapLeft && yGL < mapTop
                && yGL > mapBottom) {
            obsticle.x = xGL / getScale() - mRenderer.dragX;
            obsticle.y = yGL / getScale() - mRenderer.dragY;
            obsticle.Aw = 1;
            obsticle.on = 1;
            newObstacle = true;
        }

        if (mRenderer.gToggle == 0 && mRenderer.fToggle == 0 && mRenderer.directionalDrag.active == true
                && (mRenderer.directionalDrag.active == true
                        && xGL < mRenderer.directionalDrag.left - mRenderer.slider
                        && xGL > mRenderer.directionalDrag.right - mRenderer.slider
                        && yGL > mRenderer.directionalDrag.down && yGL < mRenderer.directionalDrag.up)) {
            v.vibrate(50);
            if (xGL > mRenderer.directionalDrag.left
                    + (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX - .01f;
                System.out.println("LEFT");

            }
            if (xGL < mRenderer.directionalDrag.left
                    + 2 * (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX + .01f;
                System.out.println("RIGHT");
            }

            if (yGL > mRenderer.directionalDrag.up
                    + (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY - .01f;
                System.out.println("UP");
            }
            if (yGL < mRenderer.directionalDrag.up
                    + 2 * (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY + .01f;
                System.out.println("DOWN");
            }
        }

        if (fFlag == 1 && (Math.abs(xGL - previousx) > .03f || Math.abs(yGL - previousy) > .03f)
                && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            if (previousx != 0 && previousy != 0) {
                if (firstPointFreeDraw[0] == -1000) {
                    firstPointFreeDraw[0] = previousx;
                    firstPointFreeDraw[1] = previousy;
                } else if (xGL > firstPointFreeDraw[0] + .1f || xGL < firstPointFreeDraw[0] - .1f
                        || yGL > firstPointFreeDraw[1] + .1f || yGL < firstPointFreeDraw[1] - .1f) {
                    connectable = 1;
                } else if (connectable == 1) {
                    fFlag = 0;
                    setFreeDrawCoordinates(firstPointFreeDraw[0], firstPointFreeDraw[1], previousx, previousy,
                            true);
                    v.vibrate(50);
                }

                if (fFlag == 1) {
                    setFreeDrawCoordinates(xGL - mRenderer.dragX * mRenderer.scale,
                            yGL - mRenderer.dragY * mRenderer.scale,
                            previousx - mRenderer.dragX * mRenderer.scale,
                            previousy - mRenderer.dragY * mRenderer.scale, false);
                }

            }

            previousx = xGL;
            previousy = yGL;
        }

        // reverse direction of rotation above the mid-line
        if (y > getHeight() / 2) {
            dx = dx * -1;
        }

        // reverse direction of rotation to left of the mid-line
        if (x < getWidth() / 2) {
            dy = dy * -1;
        }

        if (yGL > -.15f && yGL < .15f && xGL > -(width1 - 115) / (height1 * 2) - mRenderer.slider
                && xGL < -(width1 - 115) / (height1 * 2) - mRenderer.slider + .15f) {
            mRenderer.slider = -(width1 - 115) / (height1 * 2) - xGL;
            if (mRenderer.slider < .1f) {
                mRenderer.slider = 0;
            } else if (mRenderer.slider > -.1f + (width1 - 115) / (height1 * 2)) {
                mRenderer.slider = (width1 - 115) / (height1 * 2);
            }
        }

        mRenderer.setAngle(mRenderer.getAngle() + ((dx + dy) * TOUCH_SCALE_FACTOR)); // = 180.0f / 320
        requestRender();

        if (pFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL - mRenderer.scale * mRenderer.dragX;
            pY = yGL - mRenderer.scale * mRenderer.dragY;
            pFlag2 = 1;
            mRenderer.setpToggle2(pFlag2);
            mRenderer.setWayPointValues(pX, pY);
        }

        //moving gauss
        if (gFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL;
            pY = yGL;
            gFlag2 = 1;
            mRenderer.setpToggle2(gFlag2);
            //TEMP FIX
            mRenderer.updateGauss(xGL, yGL, 0);
            //mRenderer.updateGauss(xGL, yGL, gInd-1);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar.right - .04f - mRenderer.slider && yGL > mRenderer.dial.down
                && yGL < mRenderer.dial.up) {
            mRenderer.dial1 = -(xGL - mRenderer.bar.left - mRenderer.slider) / .6f;
            if (mRenderer.dial1 < 0.05) {
                mRenderer.dial1 = 0;
            } else if (mRenderer.dial1 > 1) {
                mRenderer.dial1 = 1;
            } else if (mRenderer.dial1 < .15) {
                mRenderer.dial1 = .1f;
            } else if (mRenderer.dial1 < .25) {
                mRenderer.dial1 = .2f;
            } else if (mRenderer.dial1 < .35) {
                mRenderer.dial1 = .3f;
            } else if (mRenderer.dial1 < .45) {
                mRenderer.dial1 = .4f;
            } else if (mRenderer.dial1 < .55) {
                mRenderer.dial1 = .5f;
            } else if (mRenderer.dial1 < .65) {
                mRenderer.dial1 = .6f;
            } else if (mRenderer.dial1 < .75) {
                mRenderer.dial1 = .7f;
            } else if (mRenderer.dial1 < .85) {
                mRenderer.dial1 = .8f;
            } else if (mRenderer.dial1 < .95) {
                mRenderer.dial1 = .9f;
            } else {
                mRenderer.dial1 = 1;
            }
            mRenderer.textListGaussian.get(1).setText(" " + truncateDecimal(mRenderer.dial1, 1));
            mRenderer.dial.active = true;
            v.vibrate(50);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar_2.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar_2.right - .04f - mRenderer.slider && yGL > mRenderer.dial_2.down
                && yGL < mRenderer.dial_2.up) {
            mRenderer.dial2 = -(xGL - mRenderer.bar_2.left - mRenderer.slider) / .6f;
            if (mRenderer.dial2 < 0.05) {
                mRenderer.dial2 = 0;
            } else if (mRenderer.dial2 > 1) {
                mRenderer.dial2 = 1;
            } else if (mRenderer.dial2 < .15) {
                mRenderer.dial2 = .1f;
            } else if (mRenderer.dial2 < .25) {
                mRenderer.dial2 = .2f;
            } else if (mRenderer.dial2 < .35) {
                mRenderer.dial2 = .3f;
            } else if (mRenderer.dial2 < .45) {
                mRenderer.dial2 = .4f;
            } else if (mRenderer.dial2 < .55) {
                mRenderer.dial2 = .5f;
            } else if (mRenderer.dial2 < .65) {
                mRenderer.dial2 = .6f;
            } else if (mRenderer.dial2 < .75) {
                mRenderer.dial2 = .7f;
            } else if (mRenderer.dial2 < .85) {
                mRenderer.dial2 = .8f;
            } else if (mRenderer.dial2 < .95) {
                mRenderer.dial2 = .9f;
            } else {
                mRenderer.dial2 = 1;
            }
            mRenderer.textListGaussian.get(3).setText(" " + truncateDecimal(mRenderer.dial2, 1));
            mRenderer.dial_2.active = true;
            v.vibrate(50);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar_3.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar_3.right - .04f - mRenderer.slider && yGL > mRenderer.dial_3.down
                && yGL < mRenderer.dial_3.up) {
            mRenderer.dial3 = -(xGL - mRenderer.bar_3.left - mRenderer.slider) / .6f;
            if (mRenderer.dial3 < 0.05) {
                mRenderer.dial3 = 0;
            } else if (mRenderer.dial3 > 1) {
                mRenderer.dial3 = 1;
            } else if (mRenderer.dial3 < .15) {
                mRenderer.dial3 = .1f;
            } else if (mRenderer.dial3 < .25) {
                mRenderer.dial3 = .2f;
            } else if (mRenderer.dial3 < .35) {
                mRenderer.dial3 = .3f;
            } else if (mRenderer.dial3 < .45) {
                mRenderer.dial3 = .4f;
            } else if (mRenderer.dial3 < .55) {
                mRenderer.dial3 = .5f;
            } else if (mRenderer.dial3 < .65) {
                mRenderer.dial3 = .6f;
            } else if (mRenderer.dial3 < .75) {
                mRenderer.dial3 = .7f;
            } else if (mRenderer.dial3 < .85) {
                mRenderer.dial3 = .8f;
            } else if (mRenderer.dial3 < .95) {
                mRenderer.dial3 = .9f;
            } else {
                mRenderer.dial3 = 1;
            }
            mRenderer.textListGaussian.get(5).setText(" " + truncateDecimal(mRenderer.dial3, 1));
            mRenderer.dial_3.active = true;
            v.vibrate(50);
        }

    case MotionEvent.ACTION_UP:

        mRenderer.tToggle = 1;
        //mRenderer.clear.active = false;

    }

    mPreviousX = x;
    mPreviousY = y;

    antispam = 0;
    return true;
}

From source file:com.sidekickApp.AppState.java

public static Double haversine(Double lat1, Double lon1, Double lat2, Double lon2) {
    int R = 6371;
    Double dLat = Math.toRadians(lat2 - lat1);
    Double dLon = Math.toRadians(lon2 - lon1);

    Double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2)
            * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));

    Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    Double d = R * c * 1000;//from  w ww. j a  v  a 2s .  com

    return d;
}

From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java

private void handleMove(MotionEvent event) {
    int activeTarget = -1;
    final int historySize = event.getHistorySize();
    ArrayList<TargetDrawable> targets = mTargetDrawables;
    int ntargets = targets.size();
    float x = 0.0f;
    float y = 0.0f;
    int actionIndex = event.findPointerIndex(mPointerId);

    if (actionIndex == -1) {
        return; // no data for this pointer
    }//ww  w . java 2  s.  c o m

    for (int k = 0; k < historySize + 1; k++) {
        float eventX = k < historySize ? event.getHistoricalX(actionIndex, k) : event.getX(actionIndex);
        float eventY = k < historySize ? event.getHistoricalY(actionIndex, k) : event.getY(actionIndex);
        // tx and ty are relative to wave center
        float tx = eventX - mWaveCenterX;
        float ty = eventY - mWaveCenterY;
        float touchRadius = (float) Math.hypot(tx, ty);
        final float scale = touchRadius > mOuterRadius ? mOuterRadius / touchRadius : 1.0f;
        float limitX = tx * scale;
        float limitY = ty * scale;
        double angleRad = Math.atan2(-ty, tx);

        if (!mDragging) {
            trySwitchToFirstTouchState(eventX, eventY);
        }

        if (mDragging) {
            // For multiple targets, snap to the one that matches
            final float snapRadius = mRingScaleFactor * mOuterRadius - mSnapMargin;
            final float snapDistance2 = snapRadius * snapRadius;
            // Find first target in range
            for (int i = 0; i < ntargets; i++) {
                TargetDrawable target = targets.get(i);

                double targetMinRad = (i - 0.5) * 2 * Math.PI / ntargets;
                double targetMaxRad = (i + 0.5) * 2 * Math.PI / ntargets;
                if (target.isEnabled()) {
                    boolean angleMatches = (angleRad > targetMinRad && angleRad <= targetMaxRad)
                            || (angleRad + 2 * Math.PI > targetMinRad
                                    && angleRad + 2 * Math.PI <= targetMaxRad);
                    if (angleMatches && (dist2(tx, ty) > snapDistance2)) {
                        activeTarget = i;
                    }
                }
            }
        }
        x = limitX;
        y = limitY;
    }

    if (!mDragging) {
        return;
    }

    if (activeTarget != -1) {
        switchToState(STATE_SNAP, x, y);
        updateGlowPosition(x, y);
    } else {
        switchToState(STATE_TRACKING, x, y);
        updateGlowPosition(x, y);
    }

    if (mActiveTarget != activeTarget) {
        // Defocus the old target
        if (mActiveTarget != -1) {
            TargetDrawable target = targets.get(mActiveTarget);
            target.setState(TargetDrawable.STATE_INACTIVE);
        }
        // Focus the new target
        if (activeTarget != -1) {
            TargetDrawable target = targets.get(activeTarget);
            target.setState(TargetDrawable.STATE_FOCUSED);
            final AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
                    .getSystemService(Context.ACCESSIBILITY_SERVICE);
            if (accessibilityManager.isEnabled()) {
                String targetContentDescription = getTargetDescription(activeTarget);
                announceForAccessibility(targetContentDescription);
            }
        }
    }
    mActiveTarget = activeTarget;
}

From source file:geogebra.common.kernel.EquationSolver.java

public int solveQuartic(double eqn[], double res[], double eps) {

    if (Math.abs(eqn[4]) < 0)
        return solveCubic(eqn, res, Kernel.STANDARD_PRECISION);

    double a = eqn[3] / eqn[4], b = eqn[2] / eqn[4], c = eqn[1] / eqn[4], d = eqn[0] / eqn[4];

    /*/*from w w  w . j a v a  2s .  co m*/
     * This code is based on a simplification of the algorithm from
     * zsolve_quartic.c for real roots
     */
    double u[] = new double[3], v[] = new double[3], zarr[] = new double[4];
    double aa, pp, qq, rr, rc, sc, tc, mt;
    double w1r, w1i, w2r, w2i, w3r;
    double v1, v2, arg, theta;
    double disc, h;
    int k1 = 0, k2 = 0;

    int roots = 0;

    /*
     * Deal easily with the cases where the quartic is degenerate. The
     * ordering of solutions is done explicitly.
     */
    if (0 == b && 0 == c) {
        if (0 == d) {
            if (a > 0) {
                res[roots++] = -a;
                res[roots++] = 0.0;
                res[roots++] = 0.0;
                res[roots++] = 0.0;
            } else {
                res[roots++] = 0.0;
                res[roots++] = 0.0;
                res[roots++] = 0.0;
                res[roots++] = -a;
            }
            return 4;
        } else if (0 == a) {
            if (d > 0) {
                return 0;
            }

            res[roots++] = Math.sqrt(Math.sqrt(-d));
            res[roots] = -res[roots - 1];
            roots++;
            return 2;

        }
    }

    if (0.0 == c && 0.0 == d) {
        res[roots++] = 0.0;
        res[roots++] = 0.0;
        double[] res2 = new double[3];
        res2[2] = 1.0;
        res2[1] = a;
        res2[0] = b;
        int n = solveQuadratic(res2, res2, eps);
        res[roots++] = res2[0];
        res[roots++] = res2[1];
        // if (gsl_poly_solve_quadratic(1.0,a,b,x2,x3)==0) {
        if (n == 0) {
            mt = 3;
        } else {
            mt = 1;
        }
    } else {
        /*
         * For non-degenerate solutions, proceed by constructing and solving
         * the resolvent cubic
         */
        aa = a * a;
        pp = b - (3.0 / 8.0) * aa;
        qq = c - (1.0 / 2.0) * a * (b - (1.0 / 4.0) * aa);
        rr = d - (1.0 / 4.0) * (a * c - (1.0 / 4.0) * aa * (b - (3.0 / 16.0) * aa));
        rc = (1.0 / 2.0) * pp;
        sc = (1.0 / 4.0) * ((1.0 / 4.0) * pp * pp - rr);
        tc = -((1.0 / 8.0) * qq * (1.0 / 8.0) * qq);

        /*
         * This code solves the resolvent cubic in a convenient fashion for
         * this implementation of the quartic. If there are three real
         * roots, then they are placed directly into u[]. If two are
         * complex, then the real root is put into u[0] and the real and
         * imaginary part of the complex roots are placed into u[1] and
         * u[2], respectively. Additionally, this calculates the
         * discriminant of the cubic and puts it into the variable disc.
         */
        {
            double qcub = (rc * rc - 3 * sc);
            double rcub = (2 * rc * rc * rc - 9 * rc * sc + 27 * tc);

            double Q = qcub / 9;
            double R = rcub / 54;

            double Q3 = Q * Q * Q;
            double R2 = R * R;

            double CR2 = 729 * rcub * rcub;
            double CQ3 = 2916 * qcub * qcub * qcub;

            disc = (CR2 - CQ3) / 2125764.0;

            if (0 == R && 0 == Q) {
                u[0] = -rc / 3;
                u[1] = -rc / 3;
                u[2] = -rc / 3;
            } else if (CR2 == CQ3) {
                double sqrtQ = Math.sqrt(Q);
                if (R > 0) {
                    u[0] = -2 * sqrtQ - rc / 3;
                    u[1] = sqrtQ - rc / 3;
                    u[2] = sqrtQ - rc / 3;
                } else {
                    u[0] = -sqrtQ - rc / 3;
                    u[1] = -sqrtQ - rc / 3;
                    u[2] = 2 * sqrtQ - rc / 3;
                }
            } else if (CR2 < CQ3) {
                double sqrtQ = Math.sqrt(Q);
                double sqrtQ3 = sqrtQ * sqrtQ * sqrtQ;
                theta = Math.acos(R / sqrtQ3);
                if (R / sqrtQ3 >= 1.0)
                    theta = 0.0;
                {
                    double norm = -2 * sqrtQ;

                    u[0] = norm * Math.cos(theta / 3) - rc / 3;
                    u[1] = norm * Math.cos((theta + 2.0 * Math.PI) / 3) - rc / 3;
                    u[2] = norm * Math.cos((theta - 2.0 * Math.PI) / 3) - rc / 3;
                }
            } else {
                double sgnR = (R >= 0 ? 1 : -1);
                double modR = Math.abs(R);
                double sqrt_disc = Math.sqrt(R2 - Q3);
                double A = -sgnR * Math.pow(modR + sqrt_disc, 1.0 / 3.0);
                double B = Q / A;
                double mod_diffAB = Math.abs(A - B);

                u[0] = A + B - rc / 3;
                u[1] = -0.5 * (A + B) - rc / 3;
                u[2] = -(Math.sqrt(3.0) / 2.0) * mod_diffAB;
            }
        }
        /* End of solution to resolvent cubic */

        /*
         * Combine the square roots of the roots of the cubic resolvent
         * appropriately. Also, calculate 'mt' which designates the nature
         * of the roots: mt=1 : 4 real roots (disc == 0) mt=2 : 0 real roots
         * (disc < 0) mt=3 : 2 real roots (disc > 0)
         */

        if (0.0 == disc)
            u[2] = u[1];

        if (0 >= disc) {
            mt = 2;

            /*
             * One would think that we could return 0 here and exit, since
             * mt=2. However, this assignment is temporary and changes to
             * mt=1 under certain conditions below.
             */

            v[0] = Math.abs(u[0]);
            v[1] = Math.abs(u[1]);
            v[2] = Math.abs(u[2]);

            v1 = Math.max(Math.max(v[0], v[1]), v[2]);
            /* Work out which two roots have the largest moduli */
            k1 = 0;
            k2 = 0;
            if (v1 == v[0]) {
                k1 = 0;
                v2 = Math.max(v[1], v[2]);
            } else if (v1 == v[1]) {
                k1 = 1;
                v2 = Math.max(v[0], v[2]);
            } else {
                k1 = 2;
                v2 = Math.max(v[0], v[1]);
            }

            if (v2 == v[0]) {
                k2 = 0;
            } else if (v2 == v[1]) {
                k2 = 1;
            } else {
                k2 = 2;
            }

            if (0.0 <= u[k1]) {
                w1r = Math.sqrt(u[k1]);
                w1i = 0.0;
            } else {
                w1r = 0.0;
                w1i = Math.sqrt(-u[k1]);
            }
            if (0.0 <= u[k2]) {
                w2r = Math.sqrt(u[k2]);
                w2i = 0.0;
            } else {
                w2r = 0.0;
                w2i = Math.sqrt(-u[k2]);
            }
        } else {
            mt = 3;

            if (0.0 == u[1] && 0.0 == u[2]) {
                arg = 0.0;
            } else {
                arg = Math.sqrt(Math.sqrt(u[1] * u[1] + u[2] * u[2]));
            }
            theta = Math.atan2(u[2], u[1]);

            w1r = arg * Math.cos(theta / 2.0);
            w1i = arg * Math.sin(theta / 2.0);
            w2r = w1r;
            w2i = -w1i;
        }

        /* Solve the quadratic to obtain the roots to the quartic */
        w3r = qq / 8.0 * (w1i * w2i - w1r * w2r) / (w1i * w1i + w1r * w1r) / (w2i * w2i + w2r * w2r);
        h = a / 4.0;

        zarr[0] = w1r + w2r + w3r - h;
        zarr[1] = -w1r - w2r + w3r - h;
        zarr[2] = -w1r + w2r - w3r - h;
        zarr[3] = w1r - w2r - w3r - h;

        /* Arrange the roots into the variables z0, z1, z2, z3 */
        if (2 == mt) {
            if (u[k1] >= 0 && u[k2] >= 0) {
                mt = 1;
                res[roots++] = zarr[0];
                res[roots++] = zarr[1];
                res[roots++] = zarr[2];
                res[roots++] = zarr[3];
            } else {
                return 0;
            }
        } else {
            res[roots++] = zarr[0];
            res[roots++] = zarr[1];
        }
    }

    /* Sort the roots as usual */
    if (1 == mt) {
        /*
         * Roots are all real, sort them by the real part if (*x0 > *x1)
         * SWAPD (*x0, *x1); if (*x0 > *x2) SWAPD (*x0, *x2); if (*x0 > *x3)
         * SWAPD (*x0, *x3);
         * 
         * if (*x1 > *x2) SWAPD (*x1, *x2); if (*x2 > *x3) { SWAPD (*x2,
         * *x3); if (*x1 > *x2) SWAPD (*x1, *x2); }
         */
        return 4;
    }
    /*
     * 2 real roots if (*x0 > *x1) SWAPD (*x0, *x1);
     */

    return 2;
}

From source file:org.esa.nest.gpf.oceantools.WindFieldEstimationOp.java

/**
 * Estimate wind speed using CMOD5 model.
 *
 * @param nrcs      The normalized radar cross section.
 * @param direction The wind direction vector.
 * @param theta     The incidence angle in degree.
 * @return The wind speed in m/s./*  w w w .  j a  va 2  s  .c o m*/
 */
private static double estimateWindSpeed(final double nrcs, final double[] direction, final double theta) {

    final double fi = Math.atan2(direction[1], direction[0]) * org.esa.beam.util.math.MathUtils.RTOD;
    final double cosFI = Math.cos(fi * org.esa.beam.util.math.MathUtils.DTOR);

    // try wind speed from 0.1 m/s to 20 m/s with step size 0.1
    final double[] err = new double[200];
    err[0] = Math.abs(nrcs - CMOD5.compute(0.1, cosFI, theta));
    double errMin = err[0];
    int errMinIndex = 0;
    for (int i = 1; i < 200; i++) {
        final double v = (i + 1) * 0.1; // speed
        err[i] = Math.abs(nrcs - CMOD5.compute(v, cosFI, theta));
        if (err[i] < errMin) {
            errMin = err[i];
            errMinIndex = i;
        }
    }

    return (errMinIndex + 1) * 0.1;
}