none
網路圖形成有問題 RRS feed

  • 一般討論

  • 大大們大家好 目前我想要做一個網路圖 基本上 就是所有的點都會跟其他點連結(比如說有5個點 總共就會有10條線)

    而我目前想要的是 我在WPF的介面上 利用textbox 輸入我要的點以下是我的程式碼

            private int size ;
            private Point[] Positions ;
            private Single[,] Network;

    private void button1_Click(object sender, RoutedEventArgs e)
            {
                setnet(Network,Positions);
                shownet(Network);
            }

     private void setnet(Single[,] Net,Point[]pos)
            {
                for (int i = 0; i < size; i++)
                {
                    pos[i].X = pt[i].X;
                    pos[i].Y = pt[i].Y;
                    for (int j = 0; j <= i; j++)
                    {
                        Net[i, j] = distance(pos[i], pos[j]);
                        Net[j, i] = Net[i, j];
                        if (i == j) Net[i, j] = 0;
                    }
                }
            }

    private Single distance(Point a, Point b)
            {
                return (Single)Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
            }

    private void shownet(Single[,]Net)
            {
                Line myLine;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if (Net[i, j] != 0)
                        {
                            myLine = new Line();
                            myLine.Stroke = System.Windows.Media.Brushes.Black;
                            myLine.X1 = Positions[i].X;
                            myLine.X2 = Positions[j].X;
                            myLine.Y1 = Positions[i].Y;
                            myLine.Y2 = Positions[j].Y;
                            myLine.StrokeThickness = 1;
                            canvas1.Children.Add(myLine);
                        }
                    }
                }
                Rectangle myMarker;
                for (int i = 0; i < size; i++)
                {
                    myMarker = new Rectangle();
                    myMarker.Stroke = System.Windows.Media.Brushes.Black;
                    myMarker.Fill = System.Windows.Media.Brushes.Red;
                    myMarker.Height = 10;
                    myMarker.Width = 10;
                    myMarker.SetValue(Canvas.TopProperty, Positions[i].Y - myMarker.Height / 2);
                    myMarker.SetValue(Canvas.LeftProperty, Positions[i].X - myMarker.Width / 2);
                    canvas1.Children.Add(myMarker);
                }
            }

    private Point[] pt;

    private void button3_Click_1(object sender, RoutedEventArgs e)
            {
                size++;
                Positions = new Point[size];
                Network = new Single[size, size];
                pt = new Point[size];
                textBox2.Text = Convert.ToString(size);
                if (textBox1.Text == "" || textBox1.Text == null)
                {
                    MessageBox.Show("Null");
                }
                else
                {
                    for (int i = 0; i < size; i++)
                    {
                        pt[i].X = Convert.ToDouble(textBox1.Text);
                        pt[i].Y = Convert.ToDouble(textBox3.Text);
                    }
                }
            }

    但當我在textbox 鍵入點值 且按下button3後

    再利用button1形成網路圖 但只會形成鍵入的最後一個點

    (比如說 我輸入了3個點 而最後一個點為(3,4) window介面上就只會產生(3,4)這個點)

    不知大大們有蝦米看法 是不是只抓到最後輸入那個點的值呢

    • 已變更類型 ZebraTWModerator 2012年10月8日 上午 09:10 not wince question
    2010年4月23日 下午 05:00