troubleshooting Question

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

Avatar of gjon
gjonFlag for United States of America asked on
C#
8 Comments1 Solution321 ViewsLast Modified:
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.


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; }
            }
        }
 
    }
}
ASKER CERTIFIED SOLUTION
mokule

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros