コード例 #1
0
  /**
   * Test program for Hyperbola class
   *
   * @param args String[]
   */
  public static void main(String[] args) {

    final double[] pts = { //
      100, 0, -100, 0, 75, 20,

      //  120, 30, -100, -10, 70, 50,
    };

    for (int i = 0; i < pts.length; i += 6) {
      try {
        Hyperbola h =
            new Hyperbola(
                new FPoint2(pts[i + 0], pts[i + 1]),
                new FPoint2(pts[i + 2], pts[i + 3]),
                new FPoint2(pts[i + 4], pts[i + 5]));
        System.out.println("Constructed:\n" + h);

        for (double t = -50; t <= 50; t += 10) {

          FPoint2 pt = h.calcPoint(t);

          FPoint2 pt2 = new FPoint2(pt.x, pt.y + 5);

          double tClosest = h.closestPointTo(pt2);

          System.out.println("t=" + Tools.f(t) + " pt=" + pt + " closest=" + tClosest);

          if (t == -20) {
            for (double t2 = tClosest - .1; t2 <= tClosest + .1; t2 += .01) {
              FPoint2 pt3 = h.calcPoint(t2);
              Streams.out.println("t2=" + t2 + " dist=" + pt3.distance(pt2));
            }
          }
        }

      } catch (TBError e) {
        System.out.println(e.toString());
      }
    }
  }
コード例 #2
0
  /**
   * 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;
  }