所需的帮助沿直线计算新点:在vb.net或c#中 [英] Help needed Calculating New points along a straight line: in vb.net or c#

查看:91
本文介绍了所需的帮助沿直线计算新点:在vb.net或c#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助,计算一条直线上的新点:

我需要一些帮助来确定直线上的点位置:

我将尝试解释我在做什么:

我有两个点(x1,y1)和(x2,y2),并且在两个点之间有一条直线,由此我可以算出两个点之间的距离,可以说是50.
我想做的是按预定义的数量增加或减少行的长度,比如说10.
因此,如果我将线长增加10(从50增加到60),我想算出的是在增加的线的末尾是什么点(x3,y3).
希望这有道理.
我尝试使用该角度计算出新点,但是它无法正常工作,它似乎无法产生完美的直线,并且从点(x1,y1)到新点(x3,y3)的角度不是与从点(x1,y1到x2,y2)的角度相同.

这是我尝试过的:
:我算出角度
aAngle = Math.Atan((Y2-Y1)/(X2-X2))

:获取行的长度并增加10
nChngX = Math.Pow((X1-X2),2))
nChngY = Math.Pow((Y1 – Y2),2))
nChng = Math.Sqrt(nChngX + nChngY)+ 10

:计算出新的点(x3,y3)
X3 = CSng((Math.Cos(aAngle)* nChng))
Y3 = CSng(CSng((Math.Sin(aAngle)* nChng)))

如果有人可以在c#或vb.net中发布一些示例代码,将不胜感激.

Help needed Calculating New points along a straight line:

I need some help working out the point positions along a straight line:

I will try to explain what I am doing:

I have two points (x1, y1) and (x2,y2) and have a straight line between the 2 points, from this I can work out the distance between the 2 points lets say it is 50.
What I want to do is increase or decrease the length of the line by a predefined amount lets say 10.
So If I increase the line length by 10 (from 50 to 60), What I want to work out is what point(x3,y3) would be at the end of the increased line.
Hope this makes sense.
I have tried to work out the new point using the angle but it does not work correctly, it does not seems to produce a perfectly straight line and the angle from point (x1,y1) to the new (x3,y3) is not the same as the angle from point (x1,y1 to x2,y2).

This is what I have tried:
:I work out the angle
aAngle =Math.Atan((Y2 – Y1) / (X2 – X2))

:get the length of the line and increase by 10
nChngX = Math.Pow((X1 –X2), 2))
nChngY = Math.Pow((Y1 – Y2), 2))
nChng = Math.Sqrt(nChngX + nChngY) + 10

:work out the new points (x3,y3)
X3 = CSng((Math.Cos(aAngle) * nChng))
Y3 = CSng(CSng((Math.Sin(aAngle) * nChng)))

If someone could post some sample code, in either c# or vb.net that would be much appreciated.

推荐答案

由于最后两行:

Your code doesn''t work because due to the last two lines:

X3 = X1 + CSng(Math.Cos(aAngle) * nChng)
Y3 = Y1 + CSng(Math.Sin(aAngle) * nChng)



但是我认为您可以使用更简单有效的方法:

假设您有P1 =(x1,y1)和P2 =(x2,y2);现在我们定义V =(x2-x1,y2-y1)
这样您应该写:

P =(x,y)= P1 + k * V =(x1 + k *(x2-x1),y1 + k *(y2-y1))

更改k yo的值可以获得通过P1和P2的直线上所有可能的点:


  • 对于k = 0,您可以获得P = P1
  • 对于k = 1,您可以获得P = P2
  • 对于k在0和1之间,您可以从P1和P2
  • 的k <0,您可以获得k> p1的所有在P1之前"
  • 1您获得"P2之后"的所有点


  • However I think that you can use a more simple and effective approach:

    let''s say you have P1=(x1, y1) and P2=(x2, y2); now we define V=(x2-x1, y2-y1)
    this way you should write:

    P = (x, y) = P1 + k * V = (x1 + k*(x2-x1), y1 + k*(y2-y1))

    changing the value of k yo can obtain all the possible points on th straight line that go through P1 and P2:


    • for k = 0 you obtain P = P1
    • for k = 1 you obtain P = P2
    • for k between 0 and 1 you obtain all the points inside the segment from P1 and P2
    • for k < 0 you obtain all the points "before" P1
    • for k > 1 you obtain all the points "after" P2
    • l = Math.Sqrt(Math.Pow(X2 - X1, 2) + Math.Pow(Y2 - Y1, 2))
      k = (l + 10) / l
      X3 = X1 + k * (X2 - X1)
      Y3 = Y1 + k * (Y2 - Y1)


      这篇关于所需的帮助沿直线计算新点:在vb.net或c#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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