沿线段以一定百分比获取点 [英] Obtaining the point at a certain percentage along a line segment
问题描述
我有一个方法可以返回由两个点定义的线段的中点;
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);
}
完全按照预期工作.
但是,我现在需要沿着该细分受众群找到一个任意百分比(例如35%),原则上,该百分比应该像(?)一样容易;
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);
}
但是事实并非如此,我认为这是ac#问题-一旦您引入了小数点(无论是Point还是PointF),它就会把玩具丢出婴儿车并返回奇异值结果.
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?
提前谢谢.
推荐答案
您使用了错误的公式.
两点之间的加权中间值是p1 + (p2-p1) * percentage
,恰好是p1 + (p2-p1)*0.5 = (p1 + p2) * 0.5
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
这篇关于沿线段以一定百分比获取点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!