【问题标题】:How do i assign two PointF points to the method?如何为该方法分配两个 PointF 点?
【发布时间】:2013-12-31 04:05:03
【问题描述】:

这是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;
        }

这就是我使用它的方式:

for (int i = 0; i < clouds.Count - 1; i += 2)
            {
                extendedPoints = DistributePoints(new PointF(clouds[i].X, clouds[i].Y), new PointF(clouds[i + 1].X, clouds[i + 1].Y), 20);
            }
            clouds = extendedPoints;
            return clouds;

clouds 是 PointF 列表 扩展点也是 PointF 列表。

在这种情况下,现在在云中我有 37 个索引(点)。

例如,假设在索引 0 的云中,我有:x = 150 y = 200 和索引 1:x = 160 y = 250 所以我使用你的 ja72 方法,现在扩展点包含 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

现在这是一个迭代。 下一个应该从原始 List 索引 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,因为它们的顺序相同。

【问题讨论】:

  • 点应该有xy 坐标。您正在尝试创建只有x 或只有y 的点。你的意图是什么?
  • 考虑将方法重命名为DistributePoints,因为它沿pt1pt4 之间的线分布N 点。
  • 另外,将double 作为for() 循环变量也不是一个好主意。
  • 无需转换为double,然后转换为float,只需将所有数学运算保留在float

标签: c# winforms


【解决方案1】:

代码有一个微妙的错误。端点的处理方式与内部点不同(通过Min()Max() 过程)。此外,将double 用于循环计数器也不是一个好主意。将下面的代码视为代码的更简洁版本:

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)
}

【讨论】:

  • ja72 我试过你的代码,但它只会返回 20 分一次。第二个问题,您以错误的方式放置了 pt1 和 pt4。我需要按照原来的顺序保留它们。总是先pt1然后pt4。在您的代码结果中返回 20 个点,第一个是 pt4,最后一个是 pt1。
  • @user3117033 请解释一下。你是什​​么意思只返回20点一次?你期待什么。也许您需要在预期结果的帖子中提供一个示例(就像我在回答中所说的那样)。
  • ja72 在我的问题中以我使用它的方式看上面。我在列表云上做 FOR LOOP 并调用你的方法。云包含 37 个点索引。最后 List extendedPoint 应该包含 37*20 个点。您的方法中的第一次迭代它从云中获取两个点并在这两个点之间返回 20 个点。下一次迭代它应该从云中获取接下来的两个点,然后再次在它们之间创建 20 个点,并且对于云中的所有索引都相同,即 37。
  • ja72 我会用你的方法和我如何使用它来更新我的问题。
  • 尝试clouds.AddRange(DistributePoints(...));将distribute返回的点添加到运行列表clouds中。
【解决方案2】:

PointF的构造函数中需要提供XYhttp://msdn.microsoft.com/en-us/library/system.drawing.pointf.pointf(v=vs.110).aspx

extendedPoints = ExtendPoints(new PointF(clouds[i].X, clouds[i].Y), new PointF(clouds[i + 1].X, clouds[i + 1].Y),20);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多