www.pudn.com > PersonModel-1.1.rar > Form1.cs, change:2012-04-07,size:12769b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing.Drawing2D; 
using System.Collections; 
 
namespace PersonModel 
{ 
    public partial class Form1 : Form 
    { 
        private const int row = 750;//面板宽度 
        private const int col = 510;//面板高度 
        private const int hangkuan = 30; 
        public Pen mypen = new Pen(Brushes.Black); 
        private ArrayList tuList = new ArrayList();//存贮门图积分列表 
        private Model[,] tumian = new Model[(row+hangkuan)/hangkuan,(col+hangkuan)/hangkuan]; 
        private IList<Point> doorList = new List<Point>();//存贮门列表 
        private  IList<Point> personList = new List<Point>(); //存贮人员的列表 
        private IList <Point> spaceList = new List<Point>(); //存贮空白空间的列表 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void btopen_Click(object sender, EventArgs e) 
        { 
            SearchPlan(); 
           // this.cBtyle.SelectedItem = 4; 
            this.cBtyle.SelectedIndex = 4; 
            this.cBtyle.Enabled = false; 
        } 
 
        public enum tyle { person, door, barrier, space,surround};//空间类型 
 
        private int doorcounter = 0; //大门记录 
 
        private void draw(int i, int j,string str) 
        {            
            i /= hangkuan; 
            j /= hangkuan; 
            i *= hangkuan; 
            j *= hangkuan; 
            Label lbl = new Label(); 
             Control[]con = this.phuamian.Controls.Find(lbl + i.ToString() + j.ToString(),true); 
             
            lbl = (Label)con[0]; 
            Color color = new Color (); 
            i /= hangkuan; 
            j /= hangkuan; 
            switch (str) 
            { 
                case "围墙": 
                    if (tumian[i, j].Tyle == tyle.space.ToString()) 
                    { 
                        color = Color.Black; 
                        tumian[i,j].Tyle = tyle.surround.ToString(); 
                        tumian[i,j].Tonggguo = false; 
                        i *= hangkuan; 
                        j *= hangkuan; 
                        spaceList.Remove(new Point(i, j)); 
                        
                    } 
                    else 
                    {  
                        i *= hangkuan; 
                        j *= hangkuan; 
                        MessageBox.Show("此处以绘制,请重新选择!","提示"); 
                        return; 
                     
                  } 
                    break; 
                case "门": 
                    if (tumian[i, j].Tyle == tyle.space.ToString()) 
                    { 
                        color = Color.Red; 
                        doorcounter++; 
                        tumian[i, j].Counter = doorcounter; 
                        tumian[i, j].Tyle = tyle.door.ToString(); 
                        tumian[i, j].Tonggguo = true; 
                        i *= hangkuan; 
                        j *= hangkuan; 
                        doorList.Add(new Point(i, j)); 
                        spaceList.Remove(new Point(i, j)); 
                        
                    } 
                    //else 
                    //{ 
                    //    i *= hangkuan; 
                    //    j *= hangkuan; 
                    //    MessageBox.Show("此处以绘制,请重新选择!", "提示"); 
                    //    return; 
                    //} 
                     
                    break; 
                case "故障": 
                    if (tumian[i, j].Tyle == tyle.space.ToString()) 
                    { 
                        color = Color.Blue; 
                        tumian[i, j].Tyle = tyle.barrier.ToString(); 
                        tumian[i, j].Tonggguo = false; 
                        i *= hangkuan; 
                        j *= hangkuan; 
                        spaceList.Remove(new Point(i, j)); 
                        
                    } 
                    else 
                    { 
                        i *= hangkuan; 
                        j *= hangkuan; 
                        MessageBox.Show("此处以绘制故障,请重新选择!", "提示"); 
                        return; 
                    } 
                    break; 
                case "人物": 
                    if (tumian[i, j].Tyle == tyle.space.ToString()) 
                    { 
                        color = Color.Green; 
                        tumian[i, j].Tyle = tyle.person.ToString(); 
                        i *= hangkuan; 
                        j *= hangkuan; 
                        personList.Add(new Point(i, j)); 
                        
                    } 
                    //else 
                    //{ 
                    //    i *= hangkuan; 
                    //    j *= hangkuan; 
                    //    MessageBox.Show("此处以绘制人物,请重新选择!", "提示"); 
                    //    return; 
                    //} 
                    break; 
                case "space": 
                    if (tumian[i, j].Tyle == tyle.space.ToString() ||tumian[i, j].Tyle == tyle.person.ToString() ) 
                    { 
                       // tumian[i, j].Tonggguo = true; 
                        color = Color.White; 
                        tumian[i, j].Tyle = tyle.space.ToString(); 
                        i *= hangkuan; 
                        j *= hangkuan; 
                    } 
                    break; 
                case "way": 
                    color = Color.Green; 
                    tumian[i, j].Tyle = tyle.person.ToString(); 
                         i *= hangkuan; 
                         j *= hangkuan; 
                    break; 
                default: break; 
            } 
             
            lbl.BackColor = color; 
            
             if (str.Equals("门")) 
            { 
                lbl.Text = doorcounter + str; 
            } 
             else if (str.Equals("space")) 
             { 
                 lbl.Text = null; 
             } 
             else if(str.Equals("way")) 
             { 
                 lbl.Text = "人物"; 
             } 
            else 
             { 
                 lbl.Text = str; 
             } 
  
        } 
 
        private void phuamian_Paint(object sender, PaintEventArgs e) 
        { 
 
            for (int i = 0; i <= row; i += hangkuan) 
                for (int j = 0; j <= col; j += hangkuan) 
                {                     
                    e.Graphics.DrawRectangle(new Pen(Color.Black, 2), i, j, hangkuan, hangkuan);             
                } 
 
        } 
 
        private void phuamian_MouseDown(object sender, MouseEventArgs e) 
        { 
            
            string wanggemingchen = null; 
            switch(this.cBtyle.SelectedIndex) 
            {                 
                case 0:                    
                    wanggemingchen = "围墙"; 
                    draw( e.X, e.Y, wanggemingchen); 
                    break; 
                case 1:                   
                    wanggemingchen = "门"; 
                    draw( e.X, e.Y, wanggemingchen); 
                    break; 
                case 2:                    
                    wanggemingchen = "故障"; 
                    draw(e.X, e.Y, wanggemingchen); 
                    break; 
                case 3:                   
                    wanggemingchen = "人物"; 
                    draw( e.X, e.Y, wanggemingchen); 
                    break; 
                default: break; 
            } 
        } 
 
        private void SearchPlan() 
        { 
            Astar star = new Astar(); 
            foreach (Point door in doorList) 
            { 
                foreach (Point person in personList) 
                { 
                    PersonPlan(person,door); 
                } 
            } 
            this.btgo.Enabled = true; 
        } 
 
        private void PersonPlan(Point person ,Point door) 
        { 
            Astar star = new Astar(); 
            IList<Point> plan = new List<Point>(); 
 
            plan = star.Plan(person, door, tumian); 
 
            foreach (Point go in plan) 
            { 
 
              this.tumian[person.X / hangkuan, person.Y / hangkuan].plan.Add(go); 
            } 
                    //plan = null; 
                    if (plan == null) 
                    { 
                        MessageBox.Show("kong"); 
                        return; 
                    } 
        } 
 
        #region personTimer 
        /// <summary> 
        /// personTimer_Tick事件函数 
        /// </summary>        
 
        int counter = 0; 
        private void personTimer_Tick(object sender, EventArgs e) 
        { 
            GoStep(counter); 
            counter++; 
        } 
 
        public void GoStep(int count) 
        { 
            foreach (Point pe in personList) 
            { 
                if (count < this.tumian[pe.X / hangkuan, pe.Y / hangkuan].plan.Count - 1) 
                { 
                    Point dd = new Point(); 
                    dd = this.tumian[pe.X / hangkuan, pe.Y / hangkuan].plan[count]; 
                    Point bb = new Point(); 
                    bb = this.tumian[pe.X / hangkuan, pe.Y / hangkuan].plan[count + 1]; 
 
                    if (tumian[bb.X / hangkuan, bb.Y / hangkuan].Tyle.ToString() == tyle.space.ToString()) 
                    {                        
                            draw(dd.X, dd.Y, "space"); 
                            draw(bb.X, bb.Y, "way");                         
                    } 
                    else if (tumian[bb.X / hangkuan, bb.Y / hangkuan].Tyle.ToString() == tyle.door.ToString()) 
                    { 
                        draw(dd.X, dd.Y, "space"); 
                        draw(bb.X, bb.Y, "门"); 
                        Label lbl = new Label(); 
                        Control[] con = this.phuamian.Controls.Find(lbl + bb.X.ToString() + bb.Y.ToString(), true); 
                        lbl = (Label)con[0]; 
                        lbl.BackColor = Color.Red; 
                    } 
                    else 
                    { 
                        this.tumian[pe.X / hangkuan, pe.Y / hangkuan].plan.Insert(count,new Point(0,0)); 
                    } 
                } 
            }  
        } 
 
 
        #endregion 
 
 
        private void btAdjustGood_Click(object sender, EventArgs e) 
        { 
            tumian[10, 10].Tonggguo = false; 
        } 
 
 
        private void btgo_Click(object sender, EventArgs e) 
        { 
            this.personTimer.Start(); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        {   
 
            for (int i = 0; i <= row; i += hangkuan) 
                for (int j = 0; j <= col; j += hangkuan) 
                { 
                    Model temp = new Model(); 
                    temp.X = i; 
                    temp.CostG = 0; 
                    temp.CostH = 0; 
                    temp.PreviousNode = null; 
                    temp.Y = j; 
                    spaceList.Add(new Point(i, j)); 
                    temp.Intergral = 0; 
                    temp.Isbiaoji = false; 
                    temp.Tonggguo = true; 
                    temp.Counter = 0; 
                    temp.Tyle = tyle.space.ToString(); 
                    i /= hangkuan; 
                    j /= hangkuan; 
                    tumian[i, j] = new Model(); 
                    tumian[i, j] = temp; 
                    i *= hangkuan; 
                    j *= hangkuan; 
 
                    Label lbl = new Label(); 
                    Color color = new Color(); 
                    lbl.Name = lbl + i.ToString() + j.ToString(); 
                    lbl.Location = new Point(i, j); 
                    lbl.BackColor = color; 
                    lbl.Size = new Size(hangkuan, hangkuan); 
                    lbl.Text = ""; 
                    lbl.Enabled = false; 
                    phuamian.Controls.Add(lbl); 
 
                } 
 
            for (int i = 0; i <= row; i += hangkuan) //画边框 
            { 
                 draw(i, 0, "围墙"); 
                 draw(i, col, "围墙"); 
            } 
 
            for (int i = hangkuan; i <= col - hangkuan; i += hangkuan)//画边框 
            { 
               draw(0, i, "围墙"); 
               draw(row, i, "围墙"); 
            } 
        } 
    } 
 
}