C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-07-2008, 02:18 PM   #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?
arcaine01 is offline   Reply With Quote
Old 05-07-2008, 02:32 PM   #2
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Old 05-07-2008, 03:11 PM   #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. =/
arcaine01 is offline   Reply With Quote
Old 05-08-2008, 12:10 AM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Old 05-08-2008, 02:31 PM   #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.
arcaine01 is offline   Reply With Quote
Reply

Tags
circular, csharp, dynamic, position, positioning

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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