Results 1 to 3 of 3

Thread: C#/VS2005 - The degree and where the line is to the edge of the circle do not seem to

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    C#/VS2005 - The degree and where the line is to the edge of the circle do not seem to

    The circles are drawing properly and the line from the radius to the circumference appears to be drawing correctly, but I do not understand why 25 degrees is not appearing as 25 degrees. My lines are not ending up touching the right degree area on the outside edges of the circles. Please ask away if I need to clear this question up further.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
     
    namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            //set the degree variable, default 0
            int degrees;
            // First snippet
            //create a random type object that will hold our randomly generated number/degree
            Random createRandom = new Random();
            int dialPosition, dialPosition2, dialPosition3, dialPosition4, dialPosition5;
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                newReads();
            }
     
            //drawLine method that will create conversions of degrees to radians
            private void drawLine(Bitmap draw)
            {
                //set the length that the lines will be drawn to
                int length;
                //create points for determining center of the circles
                float x0, y0, x, y;
     
                // Set x0 and y0 to your own numbers. Points to draw our radial line from
                x0 = pictureBox1.Width / 2;
                y0 = pictureBox1.Height / 2;
                
                if (x0 < y0)
                    length = (int)x0 - 3;
                else
                    length = (int)y0 - 3;
     
                x = x0 + (float)(Math.Cos(((double)degrees / 360) * 2 * Math.PI) * length);
                y = y0 + (float)(Math.Sin(((double)degrees / 360) * 2 * Math.PI) * length);
     
                //create a graphics object that will allow us to create the pen object
                Graphics g;
                //create our pen to draw with
                Pen myPen = new Pen(Color.Black, 2);
     
                g = Graphics.FromImage(draw);
                //draw our circle
                g.DrawEllipse(new Pen(Color.Black, 1), x0 - length, y0 - length, length * 2, length * 2);
                //draw our line using the points from earlier calculations and assignments
                g.DrawLine(myPen, x0, y0, x, y);
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                listBox1.SelectedIndex = -1;
                pictureBox1.BackColor = Color.White;
                listBox5.SelectedIndex = -1;
                pictureBox5.BackColor = Color.White;
                newReads();
            }
     
            private void newReads()
            {
                //generate a new degree for the dial when button click
                degrees = createRandom.Next(0, 359);
                dialPosition = degrees;
                label1.Text = dialPosition.ToString();
                Bitmap draw = new Bitmap(pictureBox1.Width,
                    pictureBox1.Height);
                drawLine(draw);
                pictureBox1.Image = draw;
     
                //generate a new degree for the dial when button click
                degrees = createRandom.Next(0, 359);
                dialPosition2 = degrees;
                Bitmap draw2 = new Bitmap(pictureBox2.Width,
                    pictureBox2.Height);
                drawLine(draw2);
                pictureBox2.Image = draw2;
     
                //generate a new degree for the dial when button click
                degrees = createRandom.Next(0, 359);
                dialPosition3 = degrees;
                Bitmap draw3 = new Bitmap(pictureBox3.Width,
                    pictureBox3.Height);
                drawLine(draw3);
                pictureBox3.Image = draw3;
     
                //generate a new degree for the dial when button click
                degrees = createRandom.Next(0, 359);
                dialPosition4 = degrees;
                Bitmap draw4 = new Bitmap(pictureBox4.Width,
                    pictureBox4.Height);
                drawLine(draw4);
                pictureBox4.Image = draw4;
     
                //generate a new degree for the dial when button click
                degrees = createRandom.Next(0, 359);
                dialPosition5 = degrees;
                label5.Text = dialPosition5.ToString();
                Bitmap draw5 = new Bitmap(pictureBox5.Width,
                    pictureBox5.Height);
                drawLine(draw5);
                pictureBox5.Image = draw5;
            }
     
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (listBox1.SelectedIndex == 1)
                {
                    if (dialPosition > 34 & dialPosition < 72)
                    {
                        pictureBox1.BackColor = Color.Green;
                    }
                    else { pictureBox1.BackColor = Color.Red; }
                }
            }
     
            private void listBox5_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (listBox5.SelectedIndex == 1)
                {
                    if (dialPosition5 > 34 & dialPosition5 < 72)
                    {
                        pictureBox5.BackColor = Color.Green;
                    }
                    else { pictureBox5.BackColor = Color.Red; }
                }
            }
     
        }
    }

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: C#/VS2005 - The degree and where the line is to the edge of the circle do not seem to

    There is a DrawPie method that should take care of this for you. In my example I draw 6 pies, each 60 degrees to make a circle that is split into equal peices.
    Code:
            Graphics g = this.CreateGraphics;
            Pen p = New Pen(Color.Blue, 3);
    
            g.DrawPie(p, 1, 1, 100, 100, 0, 60);
            g.DrawPie(p, 1, 1, 100, 100, 60, 60);
            g.DrawPie(p, 1, 1, 100, 100, 120, 60);
            g.DrawPie(p, 1, 1, 100, 100, 180, 60);
            g.DrawPie(p, 1, 1, 100, 100, 240, 60);
            g.DrawPie(p, 1, 1, 100, 100, 300, 60);

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: C#/VS2005 - The degree and where the line is to the edge of the circle do not seem to

    Thank you for the example. I will give it a look-see later tonight to see if it simplifies things for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width