用线按顺序连接点
Connecting the dots in order with line
我真的需要一些帮助。我正在尝试创建一个类似于名为“connecting the dots”的游戏的程序,其中你有数字来自 (1...n+1) 的点,你需要连接他们用线。
所以我有一个面板,我从文件中读取了点的坐标。但我被困住了,因为我不知道如何将点与线连接起来。
My current outcome
总结一下我想做的事情:
- 你按点1,你按点2,他们连线,否则不连线。
- 并且你需要按照从1到n+1的顺序连接点。
我希望你能理解我。提前很多坦克!!
private void panel1_Paint(object sender, PaintEventArgs e)
{
List<String> pav1;
pav1 = new List<String>();
StreamReader datafile = new StreamReader("pav1.txt");
int[] X = new int[100];
int[] Y = new int[100];
int k = 0;
string line;
while (datafile.Peek() >= 0)
{
line = datafile.ReadLine();
X[k] = Int16.Parse(line);
line = datafile.ReadLine();
Y[k] = Int16.Parse(line);
k++;
}
datafile.Close();
Brush aBrush = (Brush)Brushes.Black;
for (int i = 0; i < k; i++)
{
e.Graphics.FillEllipse(aBrush, X[i], Y[i], 10, 10);
e.Graphics.DrawString((i + 1).ToString(), new Font("Arial", 10),
System.Drawing.Brushes.Gray, new Point(X[i] + 20, Y[i]));
}
}
使用Graphics.Draw()方法,不知道你为什么要用椭圆画法。你的循环应该看起来像
var myFont = new Font("Arial", 10);
for (int i = 0; i < k; i += 2)
{
var point1 = new Point(X[i], Y[i]);
var point2 = new Point(X[i + 1], Y[i + 1]);
e.Graphics.DrawLine(aBrush, point1, point2);
e.Graphics.DrawString((i + 1).ToString(), myFont, System.Drawing.Brushes.Gray, point1);
e.Graphics.DrawString((i + 2).ToString(), myFont, System.Drawing.Brushes.Gray, point2);
}
另外,点0, 0
是左上角。
首先,从panel_paint
方法中取出点,并添加额外的属性,如序数。所以,你应该像这样制作 class 而不是数组 X[] 和 Y[]:
public class Dot
{
public Point Coordinates { get; set; }
public int Ordinal { get; set; }
}
然后
List<Dot> Dots { get; set; }
为第一个和第二个选定的点制作两个道具
private Dot FirstDot { get; set; }
private Dot SecondDot { get; set; }
以与填充 X[]
和 Y[]
数组相同的方式填充该列表。
然后在面板上添加 OnMouseClick
处理程序,并在其中编写如下内容:
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
//check if user clicked on any of dots in list
var selectedDot = Dots.FirstOrDefault(dot => e.X < dot.Coordinates.X + 10 && e.X > dot.Coordinates.X
&& e.Y < dot.Coordinates.Y + 10 && e.Y > dot.Coordinates.Y);
//dot is found, add it to selected first or second dot property
if (selectedDot != null)
{
if (FirstDot == null)
FirstDot = selectedDot;
else if (SecondDot == null)
SecondDot = selectedDot;
}
}
现在,在您的绘画方法中,您应该检查是否设置了两个点,如果设置了,请检查它们是否并排,例如
if (FirstDot.Ordinal + 1 == SecondDot.Ordinal)
然后你可以使用
画线
e.Graphics.DrawLine(aBrush, FirstDot.Coordinates, SecondDot.Coordinates);
应该是这样。我希望您了解如何实施它。除了几张支票,应该就是了。
我真的需要一些帮助。我正在尝试创建一个类似于名为“connecting the dots”的游戏的程序,其中你有数字来自 (1...n+1) 的点,你需要连接他们用线。 所以我有一个面板,我从文件中读取了点的坐标。但我被困住了,因为我不知道如何将点与线连接起来。
My current outcome
总结一下我想做的事情:
- 你按点1,你按点2,他们连线,否则不连线。
- 并且你需要按照从1到n+1的顺序连接点。 我希望你能理解我。提前很多坦克!!
private void panel1_Paint(object sender, PaintEventArgs e)
{
List<String> pav1;
pav1 = new List<String>();
StreamReader datafile = new StreamReader("pav1.txt");
int[] X = new int[100];
int[] Y = new int[100];
int k = 0;
string line;
while (datafile.Peek() >= 0)
{
line = datafile.ReadLine();
X[k] = Int16.Parse(line);
line = datafile.ReadLine();
Y[k] = Int16.Parse(line);
k++;
}
datafile.Close();
Brush aBrush = (Brush)Brushes.Black;
for (int i = 0; i < k; i++)
{
e.Graphics.FillEllipse(aBrush, X[i], Y[i], 10, 10);
e.Graphics.DrawString((i + 1).ToString(), new Font("Arial", 10),
System.Drawing.Brushes.Gray, new Point(X[i] + 20, Y[i]));
}
}
使用Graphics.Draw()方法,不知道你为什么要用椭圆画法。你的循环应该看起来像
var myFont = new Font("Arial", 10);
for (int i = 0; i < k; i += 2)
{
var point1 = new Point(X[i], Y[i]);
var point2 = new Point(X[i + 1], Y[i + 1]);
e.Graphics.DrawLine(aBrush, point1, point2);
e.Graphics.DrawString((i + 1).ToString(), myFont, System.Drawing.Brushes.Gray, point1);
e.Graphics.DrawString((i + 2).ToString(), myFont, System.Drawing.Brushes.Gray, point2);
}
另外,点0, 0
是左上角。
首先,从panel_paint
方法中取出点,并添加额外的属性,如序数。所以,你应该像这样制作 class 而不是数组 X[] 和 Y[]:
public class Dot
{
public Point Coordinates { get; set; }
public int Ordinal { get; set; }
}
然后
List<Dot> Dots { get; set; }
为第一个和第二个选定的点制作两个道具
private Dot FirstDot { get; set; }
private Dot SecondDot { get; set; }
以与填充 X[]
和 Y[]
数组相同的方式填充该列表。
然后在面板上添加 OnMouseClick
处理程序,并在其中编写如下内容:
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
//check if user clicked on any of dots in list
var selectedDot = Dots.FirstOrDefault(dot => e.X < dot.Coordinates.X + 10 && e.X > dot.Coordinates.X
&& e.Y < dot.Coordinates.Y + 10 && e.Y > dot.Coordinates.Y);
//dot is found, add it to selected first or second dot property
if (selectedDot != null)
{
if (FirstDot == null)
FirstDot = selectedDot;
else if (SecondDot == null)
SecondDot = selectedDot;
}
}
现在,在您的绘画方法中,您应该检查是否设置了两个点,如果设置了,请检查它们是否并排,例如
if (FirstDot.Ordinal + 1 == SecondDot.Ordinal)
然后你可以使用
画线e.Graphics.DrawLine(aBrush, FirstDot.Coordinates, SecondDot.Coordinates);
应该是这样。我希望您了解如何实施它。除了几张支票,应该就是了。