Obtaining the point at a certain percentage along a line segment

女生的网名这么多〃 提交于 2020-02-25 07:37:10

问题


I have a method that returns the mid point of a line segment as defined by two points;

public static PointF GetMidPoint(PointF p1, PointF p2)
{
    return new PointF((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
}

Which works exactly as expected.

However, I now need to find an arbitrary percentage along the segment (say, 35%), which in principle should(?) be as easy as;

public static PointF GetArbitaryPoint(PointF p1, PointF p2, double percentage)
{
    return new PointF((p1.X + p2.X) / percentage, (p1.Y + p2.Y) / percentage);
}

But that doesn't seem to be the case, I think it's a c# issue - as soon as you introduce a decimal point (whether it be a Point or a PointF) it throws it's toys out of the pram and returns bizarre results.

So I suppose the question is how would I modify the above to get it do what I need?

Thanks in advance.


回答1:


You are using the wrong formula.

The weighted middle between two points is p1 + (p2-p1) * percentage, which just happens to work out for 0.5 as p1 + (p2-p1)*0.5 = (p1 + p2) * 0.5



来源:https://stackoverflow.com/questions/27237169/obtaining-the-point-at-a-certain-percentage-along-a-line-segment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!