/**
  * Calculate parameter for a point, and indicate how close the point is to the curve
  *
  * @param pt : point to find parameter for
  * @return FPoint2; x contains the parameter, y is its distance from the curve
  */
 private FPoint2 calcParameterAndDistance(FPoint2 pt) {
   final FPoint2 ept = new FPoint2();
   toCurveSpace(pt, ept);
   double t = toExt(ept.y);
   FPoint2 as = calcPointInArmSpace(t);
   return new FPoint2(t, Math.abs(ept.x - as.x));
 }
 /**
  * Determine where point is relative to arm
  *
  * @param x :
  * @param y : point to test
  * @return an integer, which is 0 if it's on the arm, 1 if it's to the right of the arm, -1 if
  *     it's to the left of the arm
  */
 public int testPoint(double x, double y) {
   final FPoint2 ept = new FPoint2(), eps = new FPoint2();
   eps.setLocation(x, y);
   toCurveSpace(eps, ept);
   FPoint2 as = calcPointInArmSpace(toExt(ept.y));
   int out = 0;
   double diff = ept.x - as.x;
   if (diff > 0) {
     out = 1;
   } else if (diff < 0) {
     out = -1;
   }
   if (flipped()) {
     out = -out;
   }
   return out;
 }
 /**
  * Calculate the parameter associated with a point
  *
  * @param pt : point to find parameter for
  * @return best-fit parameter for this point
  */
 public double calcParameter(FPoint2 pt) {
   final FPoint2 ept = new FPoint2();
   toCurveSpace(pt, ept);
   return toExt(ept.y);
 }
  /**
   * Find intersections between two hyperbolas
   *
   * @param a Hyperbola
   * @param b Hyperbola
   * @param iPts where to store intersection points; null to construct
   */
  public static DArray findIntersections(Hyperbola a, Hyperbola b, DArray iPts) {
    final boolean db = false;

    if (iPts == null) iPts = new DArray();
    iPts.clear();
    final DArray jPts = new DArray();
    // a.initClipIfNec();
    //    b.initClipIfNec();
    if (db) {
      System.out.println(
          "finding intersections between:\n" + a.toString(true) + "\n" + b.toString(true));
    }

    PlaneCurve.findIntersect(a.getCurve(), b.getCurve(), jPts);

    // filter out false intersections
    if (db) {
      System.out.println("prefilter # intersections= " + jPts.size());
    }

    FPoint2 ptc = new FPoint2();

    for (int i = 0; i < jPts.size(); i++) {
      FPoint2 pt = jPts.getFPoint2(i);

      // make sure calculated intersect point is actually on both arms
      {
        FPoint2 data = a.calcParameterAndDistance(pt);
        if (data.y > .001) continue;
        data = b.calcParameterAndDistance(pt);
        if (data.y > .001) continue;
      }

      if (!a.isLine()) {
        // put in curve space to verify it's to the right of the y axis
        a.toCurveSpace(pt, ptc);
        if (db) {
          System.out.println("Filter " + i + " in curveA= " + ptc);
        }
        if (ptc.x <= 0) {
          if (db) {
            System.out.println(" < 0");
          }
          continue;
        }
      }
      if (!b.isLine()) {
        b.toCurveSpace(pt, ptc);
        if (db) {
          System.out.println("        in curveB= " + ptc);
        }
        if (ptc.x <= 0) {
          if (db) {
            System.out.println(" < 0");
          }
          continue;
        }
      }
      if (db) {
        System.out.println(" adding " + pt);
      }
      iPts.add(pt);
    }
    return iPts;
  }