List of usage examples for java.lang Math atan2
@HotSpotIntrinsicCandidate public static double atan2(double y, double x)
From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java
/** * This is used for the reverse-arrow of a non-directed edge * get a transform to place the arrow shape on the passed edge at the * point where it intersects the passed shape * @param edgeShape// w ww . j a v a 2 s .c o m * @param vertexShape * @return */ protected AffineTransform getReverseArrowTransform(Line2D edgeShape, Shape vertexShape) { float dx = (float) (edgeShape.getX1() - edgeShape.getX2()); float dy = (float) (edgeShape.getY1() - edgeShape.getY2()); // iterate over the line until the edge shape will place the // arrowhead closer than 'arrowGap' to the vertex shape boundary while ((dx * dx + dy * dy) > arrow_placement_tolerance) { try { edgeShape = getFirstOutsideSegment(edgeShape, vertexShape); } catch (IllegalArgumentException e) { System.err.println(e.toString()); return null; } dx = (float) (edgeShape.getX1() - edgeShape.getX2()); dy = (float) (edgeShape.getY1() - edgeShape.getY2()); } // calculate the angle for the arrowhead double atheta = Math.atan2(dx, dy) - Math.PI / 2; AffineTransform at = AffineTransform.getTranslateInstance(edgeShape.getX1(), edgeShape.getY1()); at.rotate(-atheta); return at; }
From source file:org.apache.calcite.runtime.SqlFunctions.java
/** SQL <code>ATAN2</code> operator applied to long values. */ public static double atan2(long b0, long b1) { return Math.atan2(b0, b1); }
From source file:org.apache.calcite.runtime.SqlFunctions.java
/** SQL <code>ATAN2</code> operator applied to long/BigDecimal values. */ public static double atan2(long b0, BigDecimal b1) { return Math.atan2(b0, b1.doubleValue()); }
From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java
@Test public void atan2NegativeNegative() { try {/*w ww .j a v a 2s .c om*/ Expression expression = getExpressionWithFunctionContext("atan2(-10,-10)"); assertEquals(ExpressionType.DOUBLE, expression.getExpressionType()); assertEquals(Math.atan2(-10, -10), expression.evaluateNumerical(), 1e-15); } catch (ExpressionException e) { assertNotNull(e.getMessage()); } }
From source file:org.apache.calcite.runtime.SqlFunctions.java
/** SQL <code>ATAN2</code> operator applied to BigDecimal values. */ public static double atan2(BigDecimal b0, BigDecimal b1) { return Math.atan2(b0.doubleValue(), b1.doubleValue()); }
From source file:geogebra.kernel.EquationSolver.java
public int solveQuartic(double eqn[], double res[]) { if (Math.abs(eqn[4]) < 0) return solveCubic(eqn, res); double a = eqn[3] / eqn[4], b = eqn[2] / eqn[4], c = eqn[1] / eqn[4], d = eqn[0] / eqn[4]; /* /* w w w. j a va2 s.c om*/ * 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; } else { 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); 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; } else { /* 2 real roots if (*x0 > *x1) SWAPD (*x0, *x1);*/ } return 2; }
From source file:org.apache.calcite.runtime.SqlFunctions.java
/** SQL <code>ATAN2</code> operator applied to double values. */ public static double atan2(double b0, double b1) { return Math.atan2(b0, b1); }
From source file:statechum.analysis.learning.Visualiser.java
protected static PluggableRenderer constructRenderer(Graph g, final LayoutOptions options) { final LayoutOptions graphLayoutOptions = options != null ? options : new LayoutOptions(); PluggableRenderer r = new PluggableRenderer() { /**/* ww w . j a v a 2 s .c o m*/ * 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>. */ @Override protected void drawSimpleEdge(Graphics2D g2d, Edge e, int x1, int y1, int x2, int y2) { final Vertex v1 = (Vertex) e.getEndpoints().getFirst(); final Vertex v2 = (Vertex) e.getEndpoints().getSecond(); boolean isLoop = v1.equals(v2); final Shape s2 = vertexShapeFunction.getShape(v2); Shape edgeShape = edgeShapeFunction.getShape(e); final double dx = x2 - x1; final double dy = y2 - y1; 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); } String label = edgeStringer.getLabel(e); assert (label != null); Component labelComponent = prepareRenderer(graphLabelRenderer, label, isPicked(e), e); Dimension d = labelComponent.getPreferredSize(); Rectangle2D EdgeShapeBoundaries = edgeShape.getBounds2D(); AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); double yMin = 0, yMax = 0; double thetaRadians = 0; 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. int edgeIndex = ParallelEdgeIndexSingleton.getInstance().getIndex(e); Rectangle2D s2Bounds = s2.getBounds2D(); double scaleBy = 1 + (graphLayoutOptions.scaleLines - 1) * 1. / 3.; double translation = s2Bounds.getHeight() * (1. / 4. + edgeIndex / 4.); xform.translate(0, -scaleBy * translation); xform.scale(scaleBy * s2Bounds.getWidth(), scaleBy * s2Bounds.getHeight()); yMin = scaleBy * (EdgeShapeBoundaries.getMinY() * s2Bounds.getHeight()) - translation; yMax = scaleBy * (EdgeShapeBoundaries.getMaxY() * s2Bounds.getHeight()) - translation; } else { // this is a normal edge. Rotate it to the angle between // vertex endpoints, then scale it to the distance between // the vertices thetaRadians = Math.atan2(dy, dx); double dist = Math.sqrt(dx * dx + dy * dy); xform.rotate(thetaRadians); xform.scale(dist, 1.0); yMin = EdgeShapeBoundaries.getMinY(); yMax = EdgeShapeBoundaries.getMaxY(); } edgeShape = xform.createTransformedShape(edgeShape); // Debug code /* if (!isLoop) { g2d.setPaint(new Color( 250, 250, 0)); AffineTransform rect = AffineTransform.getTranslateInstance(x1, y1+yMin); rect.rotate(thetaRadians); g2d.fill(rect.createTransformedShape( new Rectangle(0,0,(int)Math.sqrt(dx*dx + dy*dy),(int)(yMax-yMin)))); } else { g2d.setPaint(new Color( 100, 250, 0)); AffineTransform rect = AffineTransform.getTranslateInstance(x1-s2.getBounds2D().getWidth()/2, y1+yMin); rect.rotate(thetaRadians); g2d.fill(rect.createTransformedShape( new Rectangle(0,0,(int)s2.getBounds2D().getWidth(),(int)(yMax-yMin)))); }*/ edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle); if (edgeHit == true) { Paint oldPaint = g2d.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) { g2d.setPaint(fill_paint); g2d.fill(edgeShape); } Paint draw_paint = edgePaintFunction.getDrawPaint(e); if (draw_paint != null) { g2d.setPaint(draw_paint); g2d.draw(edgeShape); } double scalex = g2d.getTransform().getScaleX(); double scaley = g2d.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 g2d.fill(arrow); } assert !(e instanceof UndirectedEdge); } // For difference visualisation only boolean labelBelow = false; if (graphLayoutOptions.showDIFF && (draw_paint == null || draw_paint instanceof Color && ((Color) draw_paint).equals(Color.BLACK))) labelBelow = true; // Now draw the label. double xLabel = 0, yLabel = 0, xa = 0, ya = 0, rotation = thetaRadians; if (isLoop) { double displacementY = labelBelow ? -yMin + d.height : -yMin + d.height, displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa - displacementX * Math.cos(thetaRadians); yLabel = ya - displacementX * Math.sin(thetaRadians); } else if (dx < 0) { double displacementY = labelBelow ? yMax - d.height : (-yMax - d.height), displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa + displacementX * Math.cos(thetaRadians); yLabel = ya + displacementX * Math.sin(thetaRadians); rotation = thetaRadians + Math.PI; } else { double displacementY = labelBelow ? yMax : -yMax, displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa - displacementX * Math.cos(thetaRadians); yLabel = ya - displacementX * Math.sin(thetaRadians); } AffineTransform old = g2d.getTransform(); AffineTransform labelTransform = new AffineTransform(); // Debug code: //g2d.drawLine((int)(x1+dx/2), (int)(y1+dy/2), (int)(xa), (int)(ya));g2d.drawLine((int)(xa), (int)(ya), (int)(xLabel), (int)(yLabel)); labelTransform.translate(xLabel, yLabel); labelTransform.rotate(rotation); g2d.setTransform(labelTransform); rendererPane.paintComponent(g2d, labelComponent, screenDevice, 0, 0, d.width, d.height, true); g2d.setTransform(old); // restore old paint g2d.setPaint(oldPaint); } // if edgeHit == true } }; r = labelEdges(g, r, graphLayoutOptions); r = labelVertices(r, g, graphLayoutOptions); r.setVertexIncludePredicate(new Predicate() { @Override public boolean evaluate(Object object) { if (!graphLayoutOptions.showIgnored && graphLayoutOptions.ignoredStates != null && graphLayoutOptions.ignoredStates.contains(object.toString())) return false; if (graphLayoutOptions.showNegatives) return true; else return DeterministicDirectedSparseGraph.isAccept((Vertex) object); } }); return r; }
From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java
@Test public void atan2NegativePositive() { try {//from www. j a v a 2s. c om Expression expression = getExpressionWithFunctionContext("atan2(-10,10)"); assertEquals(ExpressionType.DOUBLE, expression.getExpressionType()); assertEquals(Math.atan2(-10, 10), expression.evaluateNumerical(), 1e-15); } catch (ExpressionException e) { assertNotNull(e.getMessage()); } }
From source file:au.org.ala.biocache.web.OccurrenceController.java
private Double distInMetres(Double lat1, Double lon1, Double lat2, Double lon2) { Double R = 6371000d; // km Double dLat = Math.toRadians(lat2 - lat1); Double dLon = Math.toRadians(lon2 - lon1); Double lat1Rad = Math.toRadians(lat1); Double lat2Rad = Math.toRadians(lat2); Double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1Rad) * Math.cos(lat2Rad); Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; }