【问题标题】:How to find intersection point of opposite lines of two lines如何找到两条线的相对线的交点
【发布时间】:2018-12-25 12:23:58
【问题描述】:

我正在尝试找到两条线的相对线的交点:

var ax=0.00, ay=0.50 ;
var bx=1.00, by=1.00 ;
var cx=2.00, cy=0.25 ;

但我对找到一条线的对立面感到很困惑。

这是一个jsfiddle,它的点在 0.0-1.0 之间转换

那么如何找到那个路口呢?

【问题讨论】:

    标签: javascript intersection trigonometry line-intersection


    【解决方案1】:

    我冒昧地清除了您的代码并添加了一些对象,以便更轻松地回忆数据。新增:PointLine 对象和关联的方法draw()

    为此,您首先需要计算中点。这很简单,用点(a,b)(c,d) 作为你的线,中间点是( (a+c)/2, (b+d)/2 )。这将是你对面线的起点。

    从那里,您可以通过获取线的梯度 (grad = (d-b)/(a-c)) 并计算出 -1/grad(因为垂直线具有相反的梯度)来计算相反的梯度。这是我定义的opposite() 函数。

    从这里,你有两条相反的线,你只需要找到交叉点。你有两条线的两个方程(因为你知道一条线是y = g*x + r,其中g是梯度,r是原点的y值),所以你可以很容易地找出(x,y)的值在两条线上都是一样的。如果你不能,请继续maths stackexchange site 并在那里发布那个问题。

    function Point(x,y) {
      this.x = x;
      this.y = y;
    }
    Point.prototype.draw = function(color="blue") {
      var diff = 0.0125 ;
      (new Line(this.x-diff, this.y-diff, this.x-diff, this.y+diff)).draw("normal", color);
      (new Line(this.x-diff, this.y+diff, this.x+diff, this.y+diff)).draw("normal", color);
      (new Line(this.x+diff, this.y+diff, this.x+diff, this.y-diff)).draw("normal", color);
      (new Line(this.x+diff, this.y-diff, this.x-diff, this.y-diff)).draw("normal", color);
    }
    
    function Line(x1, y1, x2, y2) {
      this.point1 = new Point(x1, y1);
      this.point2 = new Point(x2, y2);
    }
    
    Line.prototype.draw = function(style="normal", color="black") {
      var c = document.getElementById("myCanvas");
      var ctx = c.getContext("2d");
      ctx.save();
      if (style == "dashed-back") {
      	ctx.setLineDash([5,3]); }
     	ctx.strokeStyle = color ;
      ctx.beginPath();
      ctx.moveTo(xp(this.point1.x), yp(this.point1.y));
      ctx.lineTo(xp(this.point2.x), yp(this.point2.y));
      ctx.stroke();
      ctx.restore();
      return this;
    }
    Line.prototype.intersect = function(otherLine) {
    	var grad1 = (this.point2.y - this.point1.y)/(this.point2.x - this.point1.x);
      var grad2 = (otherLine.point2.y - otherLine.point1.y)/(otherLine.point2.x - otherLine.point1.x);
      var remainder1 = this.point1.y - this.point1.x * grad1;
      var remainder2 = otherLine.point1.y - otherLine.point1.x * grad2;
      var x = (remainder2 - remainder1)/(grad1 - grad2);
      var y = grad1 * x + remainder1;
      return new Point(x, y);
    }
    Line.prototype.opposite = function(style = "normal", color = "Black") {
    	var c = document.getElementById("myCanvas");
      var ctx = c.getContext("2d");
      ctx.save();
      var midway = new Point((this.point1.x + this.point2.x)/2, (this.point1.y + this.point2.y)/2);
      var invgrad = -1 * 1/(this.point2.y - this.point1.y)/(this.point2.x - this.point1.x);
      return new Line(midway.x - 100, midway.y - 100*invgrad, midway.x+100, midway.y + 100 * invgrad);
    }
    
    // Normalize points for normal plot
    function xp (x) { return x*300+50 ; }
    function yp (y) { return 450-(y*300) ; }
    
    // Write a text
    function text (str,x,y,size=12,color="black") {
      var c = document.getElementById("myCanvas");
      var ctx = c.getContext("2d");
      ctx.save();
      ctx.font = size+"px Arial" ;
      ctx.fillStyle = color;
      ctx.fillText(str,xp(x),yp(y));
      ctx.restore();
    }
    
    // Init guides
    function init () {
      new Line(0, 0, 0, 1).draw("dashed-back", "#888");
      new Line(0, 1, 3, 1).draw("dashed-back", "#888");
      new Line(3, 1, 3, 0).draw("dashed-back", "#888");
      new Line(1, 0, 1, 1).draw("dashed-back", "#888");
      new Line(2, 0, 2, 1).draw("dashed-back", "#888");
      text("1",-0.05,0.95)
      text("0",-0.05,-0.05)
      text("1",1,-0.05)
      text("2",2,-0.05)
      text("3",3,-0.05)
    }
    
    init();
    
    var ax=0.00, ay=0.50 ;
    var bx=1.00, by=1.00 ;
    var cx=2.00, cy=0.25 ;
    var dx=3.00, dy=0.75 ;
    
    new Point(ax,ay).draw("red");
    new Point(bx,by).draw("red");
    new Point(cx,cy).draw("red");
    new Point(dx,dy).draw("red");
    
    var line1 = new Line(ax, ay, bx, by).draw("normal", "blue");
    var line2 = new Line(bx, by, cx, cy).draw("normal", "blue");
    var line3 = new Line(cx, cy, dx, dy).draw("normal", "blue");
    line2.opposite().draw("normal", "red");
    line1.opposite().draw("normal", "orange");
    line1.opposite().intersect(line2.opposite()).draw("normal", "purple");
    <canvas id="myCanvas" width="1000" height="600">

    警告:我的代码中有一个很大的错误 - 相对线的梯度被错误计算为 -1 * grad 而不是 -1 / grad

    【讨论】:

    • @JonasWilms 它的琐碎性是我没有把它放在答案中的两个原因之一 - 另一个是它在完整的 LaTeX 支持下更具可读性,这是 SO 所没有的但数学 SE 可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多