2011-06-27 201 views
0

我必須做這樣的事情。隨機元素(座標)

enter image description here

當我點擊一個節點上,它擴大了,這是OK(我使用Powercharts做到這一點)。 我的大問題是創建隨機座標,以便當我打開子節點時,它不會與另一個節點/子節點重疊。

在Powercharts中,我必須通過座標,所以最大的問題在於通過它。

我必須在C#中做隨機座標。

// -------------------------------------------- -----------

這是我做過什麼至今:

這是我做的,是不是overlaping,但我有一個問題。 我該如何從起點開始做圓圈?例如, ,從中間開始(300,300),然後圍繞它做圓圈。有可能嗎?

private void button2_Click(object sender, EventArgs e) 
     { 
      g = pictureBox1.CreateGraphics(); 
      g.Clear(pictureBox1.BackColor); 

      double angle; 
      Circle item0 = new Circle(); 
      item0.x=200; 
      item0.y=150; 
      item0.r=50; 
      listaCirculos.Add(item0); 

      Random randomMember = new Random(); 
      g.DrawEllipse(pen1, 200, 150, 50, 50); 

      while(listaCirculos.Count!=11) 
      { 
       int[] it = GenerateNewCircle(600); 
       Circle item = new Circle(); 
       item.x = it[0]; 
       item.y = it[1]; 
       item.r = 50; 

       if (circleIsAllowed(listaCirculos, item)) 
       { 
        listaCirculos.Add(item); 
        g.DrawEllipse(pen1, Convert.ToInt32(item.x), Convert.ToInt32(item.y), 50, 50); 
       } 
      } 


     } 

     bool circleIsAllowed(List<Circle> circles, Circle newCircle) 
     { 
       foreach(Circle it in circles) 
       { 
        //double sumR = it.x + newCircle.r; 
        //double dx = it.x - newCircle.x; 
        //double dy = it.y - newCircle.y; 
        //double squaredDist = dx * dx + dy * dy; 
        double aX = Math.Pow(it.x - newCircle.x, 2); 
        double aY = Math.Pow(it.y - newCircle.y, 2); 
        double Dif = Math.Abs(aX - aY); 
        double ra1 = it.r/2; 
        double ra2 = it.r/2; 

        double raDif = Math.Pow(ra1 + ra2, 2); 
        if ((raDif + 1) > Dif) return false; 

        //if (squaredDist < sumR*sumR) return false; 
       } 
       return true; // no existing circle overlaps 



     } 

     public int[] GenerateNewCircle(int maxSize) 
     { 
      int x, y; 
       Random randomMember = new Random(); 

       x = randomMember.Next(0,maxSize); 
      if (x - 50 < 0) 
       y = randomMember.Next(x + 50, maxSize); 
      else if (x + 50 > 600) 
       y = randomMember.Next(0, x - 50); 
      else 
       // in this case, x splits the range 0..n into 2 subranges. 
       // get a random number and skip the "gap" if necessary 
       y = randomMember.Next(0, maxSize - 50); 
      if (y > x - 50) 
      { 
       y += 20; 
      } 

      int[] abc = new int[2]; 
      abc[0] = x; 
      abc[1] = y; 


      return abc; 
     } 

回答

0
 Size sizeShape = new Size("SomeWidth" , "SomeHeight"); 
     List<Retangle> rects = new List<Retangle>(); 

     Random r = new Random(); 

    while(rects.count != "someCount") 
    { 
     Point rPoint = new Point(r.Next(500) , r.Next(500)) 
     bool isNew = true; 
     foreach(Rectangle r in rects) 
     { 
      if(r.contains(rPoint)) 
       isNew = false; 
     } 
     if(isNew) 
      rects.Add(GetRect(rPoint , sizeShape)); 
    } 

     private Rectangle GetRect(Point p , Size s) 
     { 
      return new Rectangle(p,s); 
     } 

後比你可以從rects使用, rects具有隨機​​座標。

+0

嗯,你引用了一些變量你並沒有在這裏實例化,你也採取了蠻力的方法,而不是解決實際問題。另外,你正在檢查List 是否包含點對象... – MoarCodePlz

+0

抱歉,我錯了但現在我認爲是正確的。 – hashi

+0

嗨,幾乎是這樣,但我怎麼看,如果一個矩形不重疊另一個? – Luis

0

這是鏈接,讓您知道如何創建Random Numbers in C#

您必須記住您將生成隨機數字,但您還必須確保生成的數字不會使對象重疊。

+0

但是,如果我使用隨機,coordenates可能是錯誤的,因爲子節點,例如在x:500; y:500,然後子節點例如在x:40; y60例如。那是因爲隨機是不正確的:S – Luis

+0

底線:你需要隨機數字,但你也需要對待並驗證它們後,你得到它們。如果他們不適合你,你應該得到新的號碼。警告:謹防無限循環。如果你不清除產生的數字,那麼你的數字就不會像你最初認爲的那樣隨機*。 –

0

儘管這措辭相當糟糕,我相信你在找的是Random類。它實例化爲例如:

Random thisRandom = new Random(); 

然後從那裏,如果你想生成一個隨機座標,你需要知道的最大可能的X和Y座標。知道了這些,您就可以產生X和Y這樣給定的畫面內座標:

int maxX = 500; //This is the maximum X value on your canvas 
int maxY = 500; //This is the maximum Y value on your canvas 

int newX, newY; 

newX = thisRandom.Next(maxX); 
newY = thisRandom.Next(maxY); 

雖然它不是絕對的最好的「真」隨機化而言,這應該給你你需要什麼。

+0

但如果我使用了一個隨機數,那麼coordenates可能是錯誤的,因爲子節點例如在x:500; y:500中,然後子節點例如在x:40; y60中給出。那是因爲隨機不正確:S – Luis

+0

然後確定一個距節點的合適距離。如果合適的距離是20,那麼newX = thisRandom.Next(2 * 20) - 20;然後從最初的x值中減去此值。這將確保您的新x值在您之前的x值的20像素內。 – MoarCodePlz

+0

但是我如何在20的半徑內檢查它? – Luis