Circular Positioning in C# form application?

This is a discussion on Circular Positioning in C# form application? within the C# Programming forums, part of the General Programming Boards category; For a school project, I'm attempting to position objects in a circular patter around a centralized object. However, I'm having ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    58

    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?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    Cos(2*PI)
    This is a constant. The argument to sin/cos should depend on N.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    I understand that it is a constant, which is why I divided it by N and multiplied that by N-1...
    How would you suggest writing this equation? I'm really dumbfounded when it comes to this type of stuff. =/

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    for(int Index = 0; Index < NrOfPoints; Index++)
    {
      double X = CenterX + Radius * System.Math.Cos(System.Math.PI * ((double)Index / (double)NrOfPoints));
      double Y = CenterY + Radius * System.Math.Sin(System.Math.PI * ((double)Index / (double)NrOfPoints));
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    ...You are awesome.
    Thanks for helping man!

    New dilema now though...

    I know its probably a very simple thing to solve, just because I don't know how to index or use for loops properly...
    But how would you make it so that the following code would populate each new label with each node in the attached XML file?

    Here is the XML:
    Code:
    <subject>
    	<names>
    		<ring count="4">
    			<name1 id="Ghandi"></name1>
    			<name1 id="Bob"></name1>
    			<name1 id="Jerry"></name1>
    			<name1 id="Tony"></name1>
    		</ring>
    	</names>
    </subject>
    Here is the C# file that won't load the proper Names....it creates 4 labels, but each one has the name "Ghandi":
    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)
            {
                int y = (Form1.ActiveForm.Height / 2);
                int x = (Form1.ActiveForm.Width / 2);
                XmlDocument XML = new XmlDocument();
                XML.Load("Influences.xml");
                FileStream fS = new FileStream("Influences.XML", FileMode.Open);
                XmlTextReader xml = new XmlTextReader(fS);
                //
                //Load initial centered label
                //
                Label myLabel = new Label();
                string name = "Me";
                myLabel.Text = name;
                myLabel.Height = 60;
                myLabel.Width = 60;
                myLabel.Location = new System.Drawing.Point(x - (myLabel.Width / 2), y - (myLabel.Height / 2));
                myLabel.BackColor = System.Drawing.Color.Transparent;
                myLabel.ForeColor = System.Drawing.Color.Blue;
                myLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                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);
                int N = 0;
                int xlabeltop = 0;
                int ylabeltop = 0;
                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")
                        {
                            
                            for(int i=0; i < N; i++)
                            {
                                //
                                //Label
                                //
                                Label lbl = new Label();
                                int r = lbl.Width;
                                lbl.Text = xml.GetAttribute("id");
                                lbl.Height = 60;
                                lbl.Width = 60;
                                double xcenterlabel = (x ) + r * (Math.Cos(2*Math.PI * ((double)i) / (double)N));
                                double ycenterlabel = (y ) + r * (Math.Sin(2*Math.PI * ((double)i) / (double)N));
                                xlabeltop = (int)xcenterlabel - (lbl.Width / 2);
                                ylabeltop = (int)ycenterlabel - (lbl.Height / 2);
                                lbl.Location = new System.Drawing.Point(xlabeltop, ylabeltop);
                                lbl.ForeColor = System.Drawing.Color.White;
                                lbl.BackColor = System.Drawing.Color.Transparent;
                                lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                                lbl.Click += new System.EventHandler(this.lbl_Click);
                                this.Controls.Add(lbl);
                   
                            }
                        }
                    }
                }
                fS.Close();
    
            }
        }
    }
    Last edited by arcaine01; 05-08-2008 at 03:11 PM. Reason: New error appeared.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows Form Application ?
    By messi10 in forum C# Programming
    Replies: 3
    Last Post: 04-13-2009, 08:07 AM
  2. Positioning in a circular pattern?
    By arcaine01 in forum C# Programming
    Replies: 1
    Last Post: 05-01-2008, 12:26 AM
  3. Windows form application and Windows application
    By |Wiz| in forum Windows Programming
    Replies: 5
    Last Post: 10-01-2005, 04:14 PM
  4. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21