/** * Returns the oriented smallest angle between two vectors. * The computed angle will be in the range (-Pi, Pi]. * A positive result corresponds to a counterclockwise * (CCW) rotation * from v1 to v2; * a negative result corresponds to a clockwise (CW) rotation; * a zero result corresponds to no rotation. * * @param tip1 the tip of v1 * @param tail the tail of each vector * @param tip2 the tip of v2 * @return the angle between v1 and v2, relative to v1 */ public static double angleBetweenOriented(Coordinate tip1, Coordinate tail, Coordinate tip2) { double a1 = angle(tail, tip1); double a2 = angle(tail, tip2); double angDel = a2 - a1; // normalize, maintaining orientation if (angDel <= -Math.PI) return angDel + PI_TIMES_2; if (angDel > Math.PI) return angDel - PI_TIMES_2; return angDel; }
double ang0 = Angle.angle(basePt, seg0.p0);
/** * Computes the interior angle between two segments of a ring. The ring is * assumed to be oriented in a clockwise direction. The computed angle will be * in the range [0, 2Pi] * * @param p0 * a point of the ring * @param p1 * the next point of the ring * @param p2 * the next point of the ring * @return the interior angle based at <code>p1</code> */ public static double interiorAngle(Coordinate p0, Coordinate p1, Coordinate p2) { double anglePrev = Angle.angle(p1, p0); double angleNext = Angle.angle(p1, p2); return Math.abs(angleNext - anglePrev); }
/** * Returns the unoriented smallest angle between two vectors. * The computed angle will be in the range [0, Pi). * * @param tip1 the tip of one vector * @param tail the tail of each vector * @param tip2 the tip of the other vector * @return the angle between tail-tip1 and tail-tip2 */ public static double angleBetween(Coordinate tip1, Coordinate tail, Coordinate tip2) { double a1 = angle(tail, tip1); double a2 = angle(tail, tip2); return diff(a1, a2); }
public void testAngle() throws Exception { assertEquals(Angle.angle(new Coordinate(10,0)), 0.0, TOLERANCE); assertEquals(Angle.angle(new Coordinate(10,10)), Math.PI/4, TOLERANCE); assertEquals(Angle.angle(new Coordinate(0,10)), Math.PI/2, TOLERANCE); assertEquals(Angle.angle(new Coordinate(-10,10)), 0.75*Math.PI, TOLERANCE); assertEquals(Angle.angle(new Coordinate(-10,0)), Math.PI, TOLERANCE); assertEquals(Angle.angle(new Coordinate(-10,-0.1)), -3.131592986903128, TOLERANCE); assertEquals(Angle.angle(new Coordinate(-10,-10)), -0.75*Math.PI, TOLERANCE); }