List of usage examples for java.lang Math atan2
@HotSpotIntrinsicCandidate public static double atan2(double y, double x)
From source file:com.android.screenspeak.contextmenu.RadialMenuView.java
private boolean getClosestTouchedWedge(float dX, float dY, float touchDistSq, TouchedMenuItem result) { if (touchDistSq <= mInnerRadiusSq) { // The user is touching the center dot. return false; }//from www . ja v a2 s . co m final RadialMenu menu = (mSubMenu != null) ? mSubMenu : mRootMenu; int menuSize = menu.size(); if (menuSize == 0) return false; final float offset = (mSubMenu != null) ? mSubMenuOffset : mRootMenuOffset; // Which wedge is the user touching? final double angle = Math.atan2(dX, dY); final double wedgeArc = (360.0 / menuSize); final double offsetArc = (wedgeArc / 2.0) - offset; double touchArc = (((180.0 - Math.toDegrees(angle)) + offsetArc) % 360); if (touchArc < 0) { touchArc += 360; } final int wedgeNum = (int) (touchArc / wedgeArc); if ((wedgeNum < 0) || (wedgeNum >= menuSize)) { LogUtils.log(this, Log.ERROR, "Invalid wedge index: %d", wedgeNum); return false; } result.item = menu.getItem(wedgeNum); result.isDirectTouch = (touchDistSq < mExtremeRadiusSq); return true; }
From source file:main.java.biogenesis.Organism.java
/** * Calculates the position of all organism points in the world, depending on * its rotation. It also calculates the bounding rectangle of the organism. * This method must be called from outside this class only when doing * manual drawing. //ww w . java 2s . co m * * @param force To avoid calculations, segments position are only calculated * if the organism's rotation has changed in the last frame. If it is necessary * to calculate them even when the rotation hasn't changed, assign true to this * parameter. */ public void calculateBounds(boolean force) { double left = java.lang.Double.MAX_VALUE, right = java.lang.Double.MIN_VALUE, top = java.lang.Double.MAX_VALUE, bottom = java.lang.Double.MIN_VALUE; double theta; for (int i = _segments - 1; i >= 0; i--) { /* Save calculation: if rotation hasn't changed and it is not forced, * don't calculate points again. */ if ((Math.abs(_lastTheta - _theta) > 0.00001) || force) { theta = _theta + Math.atan2(_startPointY[i], _startPointX[i]); x1[i] = (int) (_m1[i] * Math.cos(theta)); y1[i] = (int) (_m1[i] * Math.sin(theta)); theta = _theta + Math.atan2(_endPointY[i], _endPointX[i]); x2[i] = (int) (_m2[i] * Math.cos(theta)); y2[i] = (int) (_m2[i] * Math.sin(theta)); } // Finds the rectangle that comprises the organism left = Utils.min(left, x1[i] + _dCenterX, x2[i] + _dCenterX); right = Utils.max(right, x1[i] + _dCenterX, x2[i] + _dCenterX); top = Utils.min(top, y1[i] + _dCenterY, y2[i] + _dCenterY); bottom = Utils.max(bottom, y1[i] + _dCenterY, y2[i] + _dCenterY); } setBounds((int) left, (int) top, (int) (right - left + 1) + 1, (int) (bottom - top + 1) + 1); _lastTheta = _theta; }
From source file:com.googlecode.eyesfree.widget.RadialMenuView.java
private boolean getClosestTouchedWedge(float dX, float dY, float touchDistSq, TouchedMenuItem result) { if (touchDistSq <= mInnerRadiusSq) { // The user is touching the center dot. return false; }/*w ww .jav a 2s. c o m*/ final RadialMenu menu = (mSubMenu != null) ? mSubMenu : mRootMenu; final float offset = (mSubMenu != null) ? mSubMenuOffset : mRootMenuOffset; // Which wedge is the user touching? final double angle = Math.atan2(dX, dY); final double wedgeArc = (360.0 / menu.size()); final double offsetArc = (wedgeArc / 2.0) - offset; double touchArc = (((180.0 - Math.toDegrees(angle)) + offsetArc) % 360); if (touchArc < 0) { touchArc += 360; } final int wedgeNum = (int) (touchArc / wedgeArc); if ((wedgeNum < 0) || (wedgeNum > menu.size())) { LogUtils.log(this, Log.ERROR, "Invalid wedge index: %d", wedgeNum); return false; } result.item = menu.getItem(wedgeNum); result.isDirectTouch = (touchDistSq < mExtremeRadiusSq); return true; }
From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java
/** * Draws the edge <code>e</code>, whose endpoints are at <code>(x1,y1)</code> * and <code>(x2,y2)</code>, on the graphics context <code>g</code>. * The <code>Shape</code> provided by the <code>EdgeShapeFunction</code> instance * is scaled in the x-direction so that its width is equal to the distance between * <code>(x1,y1)</code> and <code>(x2,y2)</code>. */// ww w. ja v a 2 s . c o m protected void drawSimpleEdge(Graphics2D g, Edge e, int x1, int y1, int x2, int y2) { Pair endpoints = e.getEndpoints(); Vertex v1 = (Vertex) endpoints.getFirst(); Vertex v2 = (Vertex) endpoints.getSecond(); boolean isLoop = v1.equals(v2); Shape s2 = vertexShapeFunction.getShape(v2); Shape edgeShape = edgeShapeFunction.getShape(e); boolean edgeHit = true; boolean arrowHit = true; Rectangle deviceRectangle = null; if (screenDevice != null) { Dimension d = screenDevice.getSize(); if (d.width <= 0 || d.height <= 0) { d = screenDevice.getPreferredSize(); } deviceRectangle = new Rectangle(0, 0, d.width, d.height); } AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); if (isLoop) { // this is a self-loop. scale it is larger than the vertex // it decorates and translate it so that its nadir is // at the center of the vertex. Rectangle2D s2Bounds = s2.getBounds2D(); xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight()); xform.translate(0, -edgeShape.getBounds2D().getWidth() / 2); } else { // this is a normal edge. Rotate it to the angle between // vertex endpoints, then scale it to the distance between // the vertices float dx = x2 - x1; float dy = y2 - y1; float thetaRadians = (float) Math.atan2(dy, dx); xform.rotate(thetaRadians); float dist = (float) Math.sqrt(dx * dx + dy * dy); xform.scale(dist, 1.0); } edgeShape = xform.createTransformedShape(edgeShape); edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle); if (edgeHit == true) { Paint oldPaint = g.getPaint(); // get Paints for filling and drawing // (filling is done first so that drawing and label use same Paint) Paint fill_paint = edgePaintFunction.getFillPaint(e); if (fill_paint != null) { g.setPaint(fill_paint); g.fill(edgeShape); } Paint draw_paint = edgePaintFunction.getDrawPaint(e); if (draw_paint != null) { g.setPaint(draw_paint); g.draw(edgeShape); } float scalex = (float) g.getTransform().getScaleX(); float scaley = (float) g.getTransform().getScaleY(); // see if arrows are too small to bother drawing if (scalex < .3 || scaley < .3) return; if (edgeArrowPredicate.evaluate(e)) { Shape destVertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getSecond()); AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2); destVertexShape = xf.createTransformedShape(destVertexShape); arrowHit = viewTransformer.transform(destVertexShape).intersects(deviceRectangle); if (arrowHit) { AffineTransform at; if (edgeShape instanceof GeneralPath) at = getArrowTransform((GeneralPath) edgeShape, destVertexShape); else at = getArrowTransform(new GeneralPath(edgeShape), destVertexShape); if (at == null) return; Shape arrow = edgeArrowFunction.getArrow(e); arrow = at.createTransformedShape(arrow); // note that arrows implicitly use the edge's draw paint g.fill(arrow); } if (e instanceof UndirectedEdge) { Shape vertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getFirst()); xf = AffineTransform.getTranslateInstance(x1, y1); vertexShape = xf.createTransformedShape(vertexShape); arrowHit = viewTransformer.transform(vertexShape).intersects(deviceRectangle); if (arrowHit) { AffineTransform at; if (edgeShape instanceof GeneralPath) at = getReverseArrowTransform((GeneralPath) edgeShape, vertexShape, !isLoop); else at = getReverseArrowTransform(new GeneralPath(edgeShape), vertexShape, !isLoop); if (at == null) return; Shape arrow = edgeArrowFunction.getArrow(e); arrow = at.createTransformedShape(arrow); g.fill(arrow); } } } // use existing paint for text if no draw paint specified if (draw_paint == null) g.setPaint(oldPaint); String label = edgeStringer.getLabel(e); if (label != null) { labelEdge(g, e, label, x1, x2, y1, y2); } // restore old paint g.setPaint(oldPaint); } }
From source file:org.deegree.geometry.linearization.CurveLinearizer.java
private int calcNumPoints(Point p0, Point p1, Point p2, boolean isCircle, double error) { // shift the points down (to reduce the occurrence of floating point errors), independently on the x and y axes double minOrd0 = CurveLinearizer.findShiftOrd0(p0, p1, p2); double minOrd1 = CurveLinearizer.findShiftOrd1(p0, p1, p2); // if the points are already shifted, this does no harm! Point p0Shifted = new DefaultPoint(null, p0.getCoordinateSystem(), p0.getPrecision(), new double[] { p0.get0() - minOrd0, p0.get1() - minOrd1 }); Point p1Shifted = new DefaultPoint(null, p1.getCoordinateSystem(), p1.getPrecision(), new double[] { p1.get0() - minOrd0, p1.get1() - minOrd1 }); Point p2Shifted = new DefaultPoint(null, p2.getCoordinateSystem(), p2.getPrecision(), new double[] { p2.get0() - minOrd0, p2.get1() - minOrd1 }); Point center = calcCircleCenter(p0Shifted, p1Shifted, p2Shifted); double centerX = center.get0(); double centerY = center.get1(); double dx = p0Shifted.get0() - centerX; double dy = p0Shifted.get1() - centerY; double ex = p2Shifted.get0() - centerX; double ey = p2Shifted.get1() - centerY; double startAngle = Math.atan2(dy, dx); double endAngle = isCircle ? startAngle : Math.atan2(ey, ex); double radius = Math.sqrt(dx * dx + dy * dy); double angleStep = 2 * Math.acos(1 - error / radius); int numPoints; if (isCircle) { numPoints = (int) Math.ceil(2 * Math.PI / angleStep) + 1; } else {/*w w w .j a v a2s .c om*/ if (!isClockwise(p0Shifted, p1Shifted, p2Shifted)) { if (endAngle < startAngle) { endAngle += 2 * Math.PI; } numPoints = (int) Math.ceil((endAngle - startAngle) / angleStep) + 1; } else { if (startAngle < endAngle) { startAngle += 2 * Math.PI; } numPoints = (int) Math.ceil((startAngle - endAngle) / angleStep) + 1; } } return numPoints; }
From source file:org.dwfa.ace.graph.AceGraphRenderer.java
/** * Draws the edge <code>e</code>, whose endpoints are at * <code>(x1,y1)</code> and <code>(x2,y2)</code>, on the graphics context * <code>g</code>.//from w ww . j a v a 2 s.c o m * The <code>Shape</code> provided by the <code>EdgeShapeFunction</code> * instance * is scaled in the x-direction so that its width is equal to the distance * between <code>(x1,y1)</code> and <code>(x2,y2)</code>. */ protected void drawSimpleEdge(Graphics2D g, Edge e, int x1, int y1, int x2, int y2) { Pair endpoints = e.getEndpoints(); Vertex v1 = (Vertex) endpoints.getFirst(); Vertex v2 = (Vertex) endpoints.getSecond(); boolean isLoop = v1.equals(v2); Shape s2 = vertexShapeFunction.getShape(v2); Shape edgeShape = edgeShapeFunction.getShape(e); boolean edgeHit = true; boolean arrowHit = true; Rectangle deviceRectangle = null; if (screenDevice != null) { Dimension d = screenDevice.getSize(); if (d.width <= 0 || d.height <= 0) { d = screenDevice.getPreferredSize(); } deviceRectangle = new Rectangle(0, 0, d.width, d.height); } AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); if (isLoop) { // this is a self-loop. scale it is larger than the vertex // it decorates and translate it so that its nadir is // at the center of the vertex. Rectangle2D s2Bounds = s2.getBounds2D(); xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight()); xform.translate(0, -edgeShape.getBounds2D().getWidth() / 2); } else { // this is a normal edge. Rotate it to the angle between // vertex endpoints, then scale it to the distance between // the vertices float dx = x2 - x1; float dy = y2 - y1; float thetaRadians = (float) Math.atan2(dy, dx); xform.rotate(thetaRadians); float dist = (float) Math.sqrt(dx * dx + dy * dy); xform.scale(dist, 1.0); } edgeShape = xform.createTransformedShape(edgeShape); if (deviceRectangle == null) { edgeHit = false; } else { edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle); } if (edgeHit == true) { Paint oldPaint = g.getPaint(); // get Paints for filling and drawing // (filling is done first so that drawing and label use same Paint) Paint fill_paint = edgePaintFunction.getFillPaint(e); if (fill_paint != null) { g.setPaint(fill_paint); g.fill(edgeShape); } Paint draw_paint = edgePaintFunction.getDrawPaint(e); if (draw_paint != null) { g.setPaint(draw_paint); g.draw(edgeShape); } float scalex = (float) g.getTransform().getScaleX(); float scaley = (float) g.getTransform().getScaleY(); // see if arrows are too small to bother drawing if (scalex < .3 || scaley < .3) return; if (edgeArrowPredicate.evaluate(e)) { Shape destVertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getSecond()); AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2); destVertexShape = xf.createTransformedShape(destVertexShape); arrowHit = viewTransformer.transform(destVertexShape).intersects(deviceRectangle); if (arrowHit) { AffineTransform at; if (edgeShape instanceof GeneralPath) at = getArrowTransform((GeneralPath) edgeShape, destVertexShape); else at = getArrowTransform(new GeneralPath(edgeShape), destVertexShape); if (at == null) return; Shape arrow = edgeArrowFunction.getArrow(e); arrow = at.createTransformedShape(arrow); // note that arrows implicitly use the edge's draw paint g.fill(arrow); } if (e instanceof UndirectedEdge) { Shape vertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getFirst()); xf = AffineTransform.getTranslateInstance(x1, y1); vertexShape = xf.createTransformedShape(vertexShape); arrowHit = viewTransformer.transform(vertexShape).intersects(deviceRectangle); if (arrowHit) { AffineTransform at; if (edgeShape instanceof GeneralPath) at = getReverseArrowTransform((GeneralPath) edgeShape, vertexShape, !isLoop); else at = getReverseArrowTransform(new GeneralPath(edgeShape), vertexShape, !isLoop); if (at == null) return; Shape arrow = edgeArrowFunction.getArrow(e); arrow = at.createTransformedShape(arrow); g.fill(arrow); } } } // use existing paint for text if no draw paint specified if (draw_paint == null) g.setPaint(oldPaint); String label = edgeStringer.getLabel(e); if (label != null) { labelEdge(g, e, label, x1, x2, y1, y2); } // restore old paint g.setPaint(oldPaint); } }
From source file:at.alladin.rmbt.controlServer.OpenTestResource.java
/** * Calculate the rough distance in meters between two points * taken from http://stackoverflow.com/questions/120283/working-with-latitude-longitude-values-in-java * @param lat1 /*from w w w. j av a2 s . c om*/ * @param lng1 * @param lat2 * @param lng2 * @return */ private static double distFrom(double lat1, double lng1, double lat2, double lng2) { double earthRadius = 6371000; double dLat = Math.toRadians(lat2 - lat1); double dLng = Math.toRadians(lng2 - lng1); double sindLat = Math.sin(dLat / 2); double sindLng = Math.sin(dLng / 2); double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double dist = earthRadius * c; return dist; }
From source file:fr.inria.wimmics.prissma.selection.Matcher.java
/** * Compute geographic distance with the haversine formula. * Earth radius expressed in Km./*from w w w . j av a 2s.c o m*/ * @param lat1 * @param lng1 * @param lat2 * @param lng2 * @return distance in Km */ private double haversineDistance(double lat1, double lng1, double lat2, double lng2) { double earthRadius = 6371; // Km double dLat = Math.toRadians(lat2 - lat1); double dLng = Math.toRadians(lng2 - lng1); double sindLat = Math.sin(dLat / 2); double sindLng = Math.sin(dLng / 2); double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double dist = earthRadius * c; return dist; }
From source file:com.google.vrtoolkit.cardboard.samples.treasurehunt.MainActivity.java
/** * Check if user is looking at object by calculating where the object is in eye-space. * * @return true if the user is looking at the object. */// ww w . ja v a 2s . c om private boolean isLookingAtObject() { float[] initVec = { 0, 0, 0, 1.0f }; float[] objPositionVec = new float[4]; // Convert object space to camera space. Use the headView from onNewFrame. Matrix.multiplyMM(modelView, 0, headView, 0, modelCube, 0); Matrix.multiplyMV(objPositionVec, 0, modelView, 0, initVec, 0); float pitch = (float) Math.atan2(objPositionVec[1], -objPositionVec[2]); float yaw = (float) Math.atan2(objPositionVec[0], -objPositionVec[2]); return Math.abs(pitch) < PITCH_LIMIT && Math.abs(yaw) < YAW_LIMIT; }
From source file:com.klinker.deskclock.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 }//from w w w . j a v a 2s . co 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.sqrt(dist2(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 = 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); if (Build.VERSION.SDK_INT >= 16) { announceForAccessibility(targetContentDescription); } else { AccessibilityEvent acc_event = AccessibilityEvent .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); AccessibilityRecordCompat arc = new AccessibilityRecordCompat(acc_event); arc.setSource(this); acc_event.setClassName(this.getClass().getName()); acc_event.setPackageName(this.getContext().getPackageName()); acc_event.setEnabled(this.isEnabled()); acc_event.getText().add(targetContentDescription); accessibilityManager.sendAccessibilityEvent(acc_event); } } } } mActiveTarget = activeTarget; }