⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 line.java

📁 Java2入门经典第五章源码
💻 JAVA
字号:
class Line
{
  Point start;   // Start point of line
  Point end;     // End point of line

  // Create a line from two points
  Line(final Point start, final Point end)
  {
    this.start = new Point(start);
    this.end = new Point(end);
  }

  // Create a line from two coordinate pairs
  Line(double xStart, double yStart, double xEnd, double yEnd)
  {
    start = new Point(xStart, yStart);   // Create the start point
    end = new Point(xEnd, yEnd);         // Create the end point
  }

  // Calculate the length of a line
  double length()
  {
    return start.distance(end);  // Use the method from the Point class
  }

  // Convert a line to a string 
  public String toString()
  {
    return "(" + start+ "):(" + end + ")";    // As "(start):(end)"
  }                                           // that is, "(x1, y1):(x2, y2)"

  // Return a point as the intersection of two lines -- called from a Line object
  Point intersects(final Line line1)
  {
    Point localPoint = new Point(0, 0);

    double num = 
      (this.end.y - this.start.y)*(this.start.x - line1.start.x) -
      (this.end.x - this.start.x)*(this.start.y - line1.start.y);

    double denom = 
      (this.end.y - this.start.y)*(line1.end.x - line1.start.x) -
      (this.end.x - this.start.x)*(line1.end.y - line1.start.y);

    localPoint.x = line1.start.x + (line1.end.x - line1.start.x)*num/denom;
    localPoint.y = line1.start.y + (line1.end.y - line1.start.y)*num/denom;

    return localPoint;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -