C# 如何为方法指定两个PointF点?

C# 如何为方法指定两个PointF点?,c#,winforms,C#,Winforms,这是ja72的方法: public static List<PointF> DistributePoints(PointF pt1, PointF pt4, int number_of_points) { List<PointF> result = new List<PointF>(); float x_min = Math.Min(pt1.X, pt4.X), x_max = Math.Max(

这是ja72的方法:

public static List<PointF> DistributePoints(PointF pt1, PointF pt4, int number_of_points)
        {
            List<PointF> result = new List<PointF>();
            float x_min = Math.Min(pt1.X, pt4.X), x_max = Math.Max(pt1.X, pt4.X);
            float y_min = Math.Min(pt1.Y, pt4.Y), y_max = Math.Max(pt1.Y, pt4.Y);
            if (number_of_points < 2) throw new ArgumentException("Need Two Points At Least");
            for (int i = 0; i < number_of_points; i++)
            {
                float scale = (float)i / (number_of_points - 1);
                float x = x_min + (x_max - x_min) * scale, y = y_min + (y_max - y_min) * scale;
                result.Add(new PointF(x, y));
            }
            return result;
        }
现在这是一篇文章。 下一个应该从原始列表索引2和索引3的云中获取,并将它们发送到您的方法,然后再返回20个点

现在,extendedPoints应该如下所示:

index 0 : x = 150 y = 200
index 1 : x = 152 y = 210
index 2 : x = 155 y = 220
.
.
.
.
.
index 21 : x = 160 y = 250
index 22 : x = 165 y = 255 ( this index 21 is the original index 2 of clouds )
index 23 : x = 166 y = 260
.
.
.
.
.
index 42 : x = 200 y = 300 ( this is the index 42 should be the original index 3 of clouds )
换句话说,我需要保持云的所有点坐标在原始云列表中的顺序相同,并在每个点之间添加新的20个点

最后,云应该以与以前相同的顺序包含所有原始点,但在每两个点之间添加新的20个点

问题是,您的方法只在云层上执行一次,而不是37次。
最后,每次点混合时,pt1和pt4应按相同顺序添加。

您需要在
点F的构造函数中提供
X
Y


该代码有一个微妙的bug。端点的处理方式与内部点不同(内部点经过
Min()
Max()
过程)。此外,对于循环计数器,使用
双精度
也不是一个好主意。将下面的代码视为代码的更干净版本:

public static List<PointF> DistributePoints(PointF pt1, PointF pt4, int number_of_points)
{
    List<PointF> result=new List<PointF>();
    float x_min=Math.Min(pt1.X, pt4.X), x_max=Math.Max(pt1.X, pt4.X);
    float y_min=Math.Min(pt1.Y, pt4.Y), y_max=Math.Max(pt1.Y, pt4.Y);
    if(number_of_points<2) throw new ArgumentException("Need Two Points At Least");
    for(int i=0; i<number_of_points; i++)
    {
        float scale=(float)i/(number_of_points-1);
        float x=x_min+(x_max-x_min)*scale, y=y_min+(y_max-y_min)*scale;
        result.Add(new PointF(x, y));
    }
    return result;
}

点应具有
x
y
坐标。您正在尝试创建只有
x
或只有
y
的点。您的意图是什么?考虑将方法重命名为<代码>分发点<代码>,因为它沿着<代码> PT1 < /COD>和<代码> PT4之间的代码区分<代码> n>代码>点。
循环变量。无需先转换为
双精度
,然后再转换为
浮点
,只需将所有数学运算保留在
浮点
。ja72我尝试了你的代码,但它只会返回20分一次。第二个问题,你把pt1和pt4放错了地方。我不需要按原来的顺序保存它们。始终先是pt1,然后是pt4。在您的代码结果中返回20分,第一个是pt4,最后一个是pt1。@user3117033请解释。你说的一次只回20分是什么意思?你期待什么。也许你需要在帖子中提供一个预期结果的例子(就像我在回答中所说的那样)。ja72看看我的问题上面我使用它的方式。我正在做列表云上的循环并调用你的方法。云包含37个点的索引。最后,列表扩展点应该包含37*20个点。首先,在您的方法中,它从云中获取两个点,并在这两个点之间返回20个点。下一步,它应该从云中获取下两个点,然后再次在它们之间创建20个点,并为云中的所有索引创建相同的点,即37.ja72。我将使用您的方法更新我的问题,以及我如何使用它。请尝试
clouds.AddRange(DistributePoints(…)
将distribute返回的点添加到运行列表
clouds
index 0 : x = 150 y = 200
index 1 : x = 152 y = 210
index 2 : x = 155 y = 220
.
.
.
.
.
index 21 : x = 160 y = 250
index 22 : x = 165 y = 255 ( this index 21 is the original index 2 of clouds )
index 23 : x = 166 y = 260
.
.
.
.
.
index 42 : x = 200 y = 300 ( this is the index 42 should be the original index 3 of clouds )
extendedPoints = ExtendPoints(new PointF(clouds[i].X, clouds[i].Y), new PointF(clouds[i + 1].X, clouds[i + 1].Y),20);
public static List<PointF> DistributePoints(PointF pt1, PointF pt4, int number_of_points)
{
    List<PointF> result=new List<PointF>();
    float x_min=Math.Min(pt1.X, pt4.X), x_max=Math.Max(pt1.X, pt4.X);
    float y_min=Math.Min(pt1.Y, pt4.Y), y_max=Math.Max(pt1.Y, pt4.Y);
    if(number_of_points<2) throw new ArgumentException("Need Two Points At Least");
    for(int i=0; i<number_of_points; i++)
    {
        float scale=(float)i/(number_of_points-1);
        float x=x_min+(x_max-x_min)*scale, y=y_min+(y_max-y_min)*scale;
        result.Add(new PointF(x, y));
    }
    return result;
}
{
    var res=DistributePoints(new PointF(100, 20), new PointF(10, 200), 11);
    // res = 
    // ( 10.0,  20.0)
    // ( 19.0,  38.0)
    // ( 28.0,  56.0)
    // ..
    // ( 91.0, 182.0)
    // (100.0, 200.0)
}