我必須做這樣的事情。隨機元素(座標)
當我點擊一個節點上,它擴大了,這是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;
}
嗯,你引用了一些變量你並沒有在這裏實例化,你也採取了蠻力的方法,而不是解決實際問題。另外,你正在檢查List是否包含點對象... –
MoarCodePlz
抱歉,我錯了但現在我認爲是正確的。 – hashi
嗨,幾乎是這樣,但我怎麼看,如果一個矩形不重疊另一個? – Luis