示例#1
0
文件: Triangle.java 项目: JaiKB/APCS
  /**
   * Calculates and returns an altitude of the triangle
   *
   * @return An altitude of the triangle
   */
  public Line getAltitude() {
    Line base = new Line(pointB, pointC);
    double altitudeSlope = base.getPerpSlope();
    // D is a point on BC such that AD perp BC
    double ax = pointA.getX();
    double bx = pointB.getX();
    double cx = pointC.getX();
    double ay = pointA.getY();
    double by = pointB.getY();
    double cy = pointC.getY();
    // Vertical case:
    if (Double.isInfinite(base.getSlope())) return new Line(pointA, new Point(ax, by));
    // There has to be an easier way...
    // This sorcery brought to you by Wolfram Alpha
    double dx =
        (ax * bx / (by - cy)
                - ax * cx / (by - cy)
                - ay * cy / (by - cy)
                + ay * by / (by - cy)
                + cx * (by - cy) / (bx - cx)
                - cy)
            / ((by - cy) / (bx - cx) + bx / (by - cy) - cx / (by - cy));
    double dy = base.getSlope() * (dx - cx) + cy;

    return new Line(pointA, new Point(dx, dy));
  }
  public static Point getIntersetingPoint(Point start1, Point end1, Point start2, Point end2) {
    Point intersection = null;

    // Sort Points by occurence on X-Axis
    if (start1.getX() > end1.getX()) {
      swap(start1, end1);
    }
    if (start2.getX() > end2.getX()) {
      swap(start2, end2);
    }
    if (start1.getX() > start2.getX()) {
      swap(start1, start2);
      swap(end1, end2);
    }

    // Create the Line Segments
    Line l1 = new Line(start1, end1);
    Line l2 = new Line(start2, end2);

    // if slopes are same then it could be parallel lines
    if (l1.getSlope() == l2.getSlope()) {
      // if the intercepts are same and start2 is in between Start1 and end2, then start 2 is the
      // intersecting point
      if (l1.getyIntercept() == l2.getyIntercept() && isBetween(start1, start2, end2)) {
        return start2;
      }
      return null;
    }

    // Else the line slopes are different

    double x = (l2.getyIntercept() - l1.getyIntercept()) / (l1.getSlope() - l2.getSlope());
    double y = x * l1.getSlope() + l1.getyIntercept();
    intersection = new Point(x, y);

    if (isBetween(start1, intersection, end1) && isBetween(start2, intersection, end2)) {
      return intersection;
    }
    return null;
  }
示例#3
0
 public boolean parallelTo(Line l2) {
   if (this.getSlope() == l2.getSlope()) return true;
   else return false;
 }
示例#4
0
 public boolean perpendicularTo(Line l2) {
   if (this.getSlope() == (-1 / l2.getSlope())) return true;
   else return false;
 }
示例#5
0
 public boolean equals(Line line2) {
   if (this.getSlope() == line2.getSlope()
       && this.getC() / this.getB() == line2.getC() / line2.getB()) return true;
   else return false;
 }