我在理解点的偏移量方法在foreach循环中如何修改现有点数组时遇到麻烦。我可以通过手动索引每个数组实体来做到这一点,但是我强烈怀疑那不是应该怎么做。
*编辑要清楚。将偏移点存储在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*/
相关分类