Circular Positioning in C# form application?
For a school project, I'm attempting to position objects in a circular patter around a centralized object. However, I'm having trouble finding an equation that works properly.
...I'm most familiar with the programming language: C#
so this is the way that I'm writing the formula that I have come up with.
N=number of objects to be positioned
for(int i=0; i < N; i++)
{
centerofobjectX = focuspointX * ((Cos(2*PI)/N)(N-1));
centerofobjectY = focuspointY * ((Sin(2*PI)/N)(N-1));
}
This equation doesn't work, and I have no idea why. Can someone please help me find a working equation to position objects in a circular pattern?
--------------------------------------------------------
More detailed junk:
--------------------------------------------------------
What I am attempting to do, is position the dynamically created labels in a circular pattern around the center label.
Here is my XML:
Code:
<subject id="you">
<name id="Me">
<ring id="bob" count="12">
<name1 id="Ghandi"></name1>
<name1 id="Bob"></name1>
<name1 id="Jerry"></name1>
<name1 id="Tony"></name1>
<name1 id="Francis"></name1>
<name1 id="Fabio"></name1>
<name1 id="Lulu"></name1>
<name1 id="Genie"></name1>
<name1 id="Sabrina"></name1>
<name1 id="Bambi"></name1>
<name1 id="Rambo"></name1>
<name1 id="Larry"></name1>
</ring>
</name>
</subject>
Now here is my C# Form1.cs file:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace Influences
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument XML = new XmlDocument();
XML.Load("Influences.xml");
FileStream fS = new FileStream("Influences.XML", FileMode.Open);
XmlTextReader xml = new XmlTextReader(fS);
Label myLabel = new Label();
int y = (Form1.ActiveForm.Height / 2 - (myLabel.Height / 2));
int x = (Form1.ActiveForm.Width / 2 - (myLabel.Width / 2));
string name = "Me";
myLabel.Text = "Name: " + name;
myLabel.Height = 14;
myLabel.Location = new System.Drawing.Point(x, y);
myLabel.ForeColor = System.Drawing.Color.White;
myLabel.Click += new System.EventHandler(this.myLabel_Click);
this.Controls.Add(myLabel);
fS.Close();
}
private void myLabel_Click(object sender, EventArgs e)
{
FileStream fS = new FileStream("Influences.XML", FileMode.Open);
XmlTextReader xml = new XmlTextReader(fS);
//string strExpression;
//docNav = new XPathDocument("Influences.xml");
//nav = docNav.CreateNavigator();
//strExpression = "subject/name/*";
//nodeIter = nav.Select(strExpression);
int N=0;
int xlabeltop=0;
int ylabeltop=0;
double xcenterlabel;
double ycenterlabel;
int x = Form1.ActiveForm.Width / 2;
int y = Form1.ActiveForm.Height / 2;
//int count = 0;
while (xml.Read())
{
if (xml.NodeType.ToString() == "Element")
{
if (xml.Name == "ring")
{
N = Convert.ToInt32(xml.GetAttribute("count"));
}
if (xml.Name == "name1")
{
Label lbl = new Label();
lbl.Text = xml.GetAttribute("id");
lbl.Height = 14;
for(int i=0; i < N; i++)
{
xcenterlabel = x * (Math.Cos(2* Math.PI)/ N) * (N - 1);
ycenterlabel = y * (Math.Sin(2 * Math.PI) / N) * (N - 1);
xlabeltop = Math.Abs((int)xcenterlabel - (lbl.Width/ 2));
ylabeltop = Math.Abs((int)ycenterlabel - (lbl.Height / 2));
}
lbl.Location = new System.Drawing.Point(xlabeltop, ylabeltop);
lbl.ForeColor = System.Drawing.Color.White;
this.Controls.Add(lbl);
}
}
// int r = myLabel.Bounds();
// v = "subject/*";
// nav = docNav.CreateNavigator();
// nodeIter = nav.Select(v);
}
fS.Close();
}
}
}
Any suggestions?