相对于线段转换坐标 [英] translate coordinates relative to line segment

查看:106
本文介绍了相对于线段转换坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个函数(使用Javascript),它接受一个线段的端点和一个附加点,并返回该点相对于起点的坐标。所以,基于这张图片:





<$ p $函数vertical_coords(x1,y1,x2,y2,xp,yp)

返回 [xp',yp'] 其中xp'是沿着垂直于线段的线的距离(xp,yp),yp'是距离从(x1,y1)到垂直线与线段相交点。



到目前为止我尝试过的方法是:

 函数rotateRad(cx,cy,x,y,radians){
var cos = Math.cos(弧度),
sin =数学.sin(弧度),
nx =(cos *(x-cx))+(sin *(y -cy))+ cx,
ny = (sin *(x-cx))+ cy;
return [nx,ny];
}

[xp',yp'] = rotateRad(x1,y1,xp,yp,Math.atan2(y2-y1,x2-x1));

我没有写这个函数;从 https://stackoverflow.com/a/17411276/1368860 获得它




  1. yp'我使用这里的答案找到相交点:垂直于从给定点开始

  2. 使用以下等式计算(xp,yp)和直线之间的距离: b
    $ b

    不是最优雅的解决方案,但它似乎可行。



    结果代码 https://jsfiddle.net/qke0m4mb/

     函数的n vertical_coords(x1,y1,x2,y2,xp,yp){
    var dx = x2 - x1,
    dy = y2 - y1;

    //找到交点
    var k =(dy *(xp-x1) - dx *(yp-y1))/(dy * dy + dx * dx);
    var x4 = xp - k * dy;
    var y4 = yp + k * dx; (y4-y1)*(y4-y1)+(x4-x1)*(x4-x1));(b4 b1 b

    var ypt = Math.sqrt
    var xpt = distance(x1,y1,x2,y2,xp,yp);
    return [xpt,ypt];
    }

    //点距线的距离
    函数距离(x1,y1,x2,y2,xp,yp){
    var dx = x2 - x1 ;
    var dy = y2 - y1;

    return Math.abs(dy * xp - dx * yp + x2 * y1 - y2 * x1)/ Math.sqrt(dy * dy + dx * dx);
    }


    I need to write a function (in Javascript) that accepts the endpoints of a line segment and an additional point and returns coordinates for the point relative to the start point. So, based on this picture:

    function perpendicular_coords(x1,y1, x2,y2, xp,yp)
    

    returns [xp',yp'] where xp' is the distance from (xp,yp) along a line perpendicular to the line segment, and yp' is the distance from (x1,y1) to the point where that perpendicular line intersects the line segment.

    What I've tried so far:

    function rotateRad(cx, cy, x, y, radians) {
        var cos = Math.cos(radians),
            sin = Math.sin(radians),
            nx = (cos * (x - cx)) + (sin * (y - cy)) + cx,
            ny = (cos * (y - cy)) - (sin * (x - cx)) + cy;
        return [nx, ny];
    }
    
    [xp', yp'] = rotateRad(x1,y1, xp, yp, Math.atan2(y2-y1,x2-x1));
    

    I didn't write the function; got it from https://stackoverflow.com/a/17411276/1368860

    解决方案

    I took a different approach, by combining two functions:

    1. To get yp' I find the intersection point using the answer from here: Perpendicular on a line from a given point
    2. To get xp' I calculate the distance between (xp, yp) and the line, using the equation from here: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line

    Not the most elegant solution, but it seems to work.

    Resulting code https://jsfiddle.net/qke0m4mb/

    function perpendicular_coords(x1, y1, x2, y2, xp, yp) {
      var dx = x2 - x1,
          dy = y2 - y1;
    
      //find intersection point    
      var k = (dy * (xp-x1) - dx * (yp-y1)) / (dy*dy + dx*dx);
      var x4 = xp - k * dy;
      var y4 = yp + k * dx;
    
      var ypt = Math.sqrt((y4-y1)*(y4-y1)+(x4-x1)*(x4-x1));
      var xpt = distance(x1, y1, x2, y2, xp, yp);
      return [xpt, ypt];
    }
    
    // Distance of point from line
    function distance(x1, y1, x2, y2, xp, yp) {
      var dx = x2 - x1;
      var dy = y2 - y1;
    
      return Math.abs(dy*xp - dx*yp + x2*y1 - y2*x1) / Math.sqrt(dy*dy + dx*dx);
    }
    

    这篇关于相对于线段转换坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆