我有一个方法返回由两点定义的线段的中点;
public static PointF GetMidPoint(PointF p1, PointF p2)
{
return new PointF((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
}
Run Code Online (Sandbox Code Playgroud)
其工作原理完全符合预期。
然而,我现在需要沿着该段找到一个任意百分比(例如 35%),原则上应该(?)很简单;
public static PointF GetArbitaryPoint(PointF p1, PointF p2, double percentage)
{
return new PointF((p1.X + p2.X) / percentage, (p1.Y + p2.Y) / percentage);
}
Run Code Online (Sandbox Code Playgroud)
但情况似乎并非如此,我认为这是 ac# 问题 - 一旦引入小数点(无论是 Point 还是 PointF),它就会把玩具从婴儿车中扔出来并返回奇怪的结果。
所以我想问题是如何修改上面的内容以使其满足我的需要?
提前致谢。
您使用了错误的公式。
两点之间的加权中位数是p1 + (p2-p1) * percentage,恰好可以计算出 0.5:p1 + (p2-p1)*0.5 = (p1 + p2) * 0.5