我不太明白如何在foreach循环中使用点的偏移方法来修改现有的点数组。我可以通过手动索引每个数组实体来完成,但我强烈怀疑这不是正确的方法。
*编辑:明确一下,将偏移点存储在MyPoints数组中的最佳方法是什么?
请参见下面的代码。我使用http://rextester.com/在线C#编译器测试了代码。
*编辑:明确一下,将偏移点存储在MyPoints数组中的最佳方法是什么?
请参见下面的代码。我使用http://rextester.com/在线C#编译器测试了代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Drawing;
namespace Rextester
{
public class Program
{
static int ii = 0;
public static void Main(string[] args)
{
Point[] MyPoints =
{
new Point(0,0),
new Point(10,10),
new Point(20,0)
};
foreach(Point p in MyPoints)
{
p.Offset(5,2); //this works but does not store the new points
} //in MyPoints array when the foreach loop
//exits, which is what I want.
Console.WriteLine("p1x = {0} \t p1y = {1}", MyPoints[0].X, MyPoints[0].Y);
Console.WriteLine("p2x = {0} \t p2y = {1}", MyPoints[1].X, MyPoints[1].Y);
Console.WriteLine("p3x = {0} \t p3y = {1} \n", MyPoints[2].X, MyPoints[2].Y);
foreach(Point p in MyPoints)
{
MyPoints[ii].Offset(5,2);
ii++;
}
Console.WriteLine("p1x = {0} \t p1y = {1}", MyPoints[0].X, MyPoints[0].Y);
Console.WriteLine("p2x = {0} \t p2y = {1}", MyPoints[1].X, MyPoints[1].Y);
Console.WriteLine("p3x = {0} \t p3y = {1}", MyPoints[2].X, MyPoints[2].Y);
}
}
}
//This yields the following
/*
p1x = 0 p1y = 0
p2x = 10 p2y = 10
p3x = 20 p3y = 0
p1x = 5 p1y = 2
p2x = 15 p2y = 12
p3x = 25 p3y = 2*/