简体   繁体   English

获取平面中直线的交点

[英]Get coords of the intersection of the line in the plane

I have a canvas with this params: width = 400 , height = 400 我有一个具有以下参数的画布: width = 400height = 400

and have a line passing through the point cursor[x1,y1] at an angle Q (in degree) 并有一条线以角度Q (度)穿过点cursor[x1,y1]

I need get all coords of the intersection of the line in the plane and write it to array. 我需要获取平面中直线的所有交点并将其写入数组。 Now i use this equation: y - y1 = k * (x - x1) 现在我使用以下等式: y - y1 = k * (x - x1)

to check all point I use this code: 检查所有点,我使用此代码:

var rad = Q * Math.PI/180; var rad = Q * Math.PI / 180;

for (ctrY = 0; ctrY < 400; ctrY += 1) {
    for (ctrX = 0; ctrX < 400; ctrX += 1) {

        if ( (ctrY - cursor.y) ===
              ~~(Math.tan(rad) * (ctrX - cursor.x)) ) {

            z.push([ctrX, ctrY]);
        }

    }
}

For example when 0 < Q < 90 and cursor[x1,y1] = [200,200] z.length = 0 and it's not correct. 例如,当0 <Q <90且cursor [x1,y1] = [200,200] z.length = 0时,这是不正确的。

Where i'm wrong? 我哪里错了? Maybe there is a more convenient algorithm? 也许有一个更方便的算法?

PS Sorry for my english PS对不起我的英语

I imagine an algorithm like this. 我想像这样的算法。 (I only consider the case when 0 < Q < 90). (我只考虑0 <Q <90的情况)。 First I will want to calculate the points where the line will intersect the Ox and Oy axes, considering the origin (0,0) point the upper left corner and if we imagine that the negative x and y values are respectively to the left and to the top of this point. 首先,我将要计算直线与Ox和Oy轴相交的点,考虑原点(0,0)点在左上角,并且如果我们假设负x和y值分别位于左侧和这一点的顶部。 Let x2 and y2 be the values where the line will intersect Ox and Oy. x2y2为直线与Ox和Oy相交的值。 We want to calculate these values. 我们要计算这些值。 We now have a system with 2 unknown variables (x2 and y2): Math.tan(rad) = (y1 -y2)/x1 and Math.tan(rad) = y1/(x1-x2) . 现在,我们有了一个系统,其中包含2个未知变量(x2和y2): Math.tan(rad) = (y1 -y2)/x1Math.tan(rad) = y1/(x1-x2) We can deduct these equations by drawing the line on the coordinate system and analyzing a bit. 我们可以通过在坐标系上画线并分析一点来推导这些方程式。 If we solve the system of equations we find something like: x2 = (x1*y1 -x1 * x1 * Math.tan(rad)/(2 * y1-x1)) and y2= y1- x1 * Math.tan(rad) (These need to be verified, I haven't double checked my calculus). 如果我们求解方程组,我们会发现类似x2 = (x1*y1 -x1 * x1 * Math.tan(rad)/(2 * y1-x1))y2= y1- x1 * Math.tan(rad) (这些需要验证,我没有仔细检查我的演算)。 A linear equation can be defined by the formula y = a*x + b and in our case a = x2 and b = y2 . 线性方程可由公式y = a*x + b ,在我们的情况下为a = x2b = y2 We can then calculate the points like this: 然后我们可以像这样计算点:

for (xIdx = 0; xIdx < 400; xIdx += 1) {
    var ctrX = xIdx;
    var ctrY = x2 * ctrX + y2 //todo: replace with the respective calculated variables x2 and y2(we could also define two functions in js) and proper rounding
    z.push([ctrX, ctrY]);
}

I'm not sure if I'm 100% accurate but I hope you understand my idea. 我不确定我是否100%准确,但希望您能理解我的想法。

Seems you need line rastering algorithm. 似乎您需要线栅格化算法。 Consider Bresenham algorithm . 考虑布雷森汉姆算法

在此处输入图片说明

You can also look at DDA algorithm 您也可以看一下DDA算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM