Avatar of gjon
gjon
Flag for United States of America asked on

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

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; }
            }
        }
 
    }
}

Open in new window

C#

Avatar of undefined
Last Comment
gjon

8/22/2022 - Mon
mokule

It's not 25? What ii is then?
Mind that it's clockwise counted.

btw
createRandom.Next(0, 359);
should be rather
createRandom.Next(0, 360);  // maxValue is exclusive unlike the min value which is inclusive
gjon

ASKER
Thank you for correcting my random numbers range.

I will try to make it more clear. I don't understand what you are asking me. What is ii? Do you mean when 25 comes up randomly?

Take for example, I am displaying what the random number degree is:
label1.Text = dialPosition.ToString();

Well, when the line is drawn onto the circle, it doesn't correspond to the degree. The degree will read 280  degrees in the label1 but the circle displays as if the degree should be 180 degrees. I thought that the degrees and line would correspond when using the formula but it does not appear that they do.

SOmeone mentioned that the circles will actually have quadrants of degrees, but I do not understand if that is valid or not.
mokule

ii was typo should be it. be never mind.
I couldn't see any problem in the code.

when I've set
            degrees = 90;
            dialPosition = degrees;
            label1.Text = dialPosition.ToString();
the line is stright down

when I've set
            degrees = 180;
the line is exactly to the left

when I've set
            degrees = 270;
the line is stright up

Where is the problem?
I'm afraid to ask, but i must. Do You know degrees?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
gjon

ASKER
Ok, maybe that's my problem. What you've explained seem to indicate so. Let me explain how I believed it should be and then maybe you can explain it better to me so I can correct my understanding.

I assumed 360 would have a line from center to 0 which would be straight up. That 180 should be centered from center to right. I throught that the range for 36-72 degrees would be in the upper right area of the circle, kind of like where the one is on a clock.

If you could explain where the degrees are in relation to a clock, that might help straighten this out.
gjon

ASKER
Dial number one is showing 358 as being from center to almost straight right. In my opinion something is wrong.
ASKER CERTIFIED SOLUTION
mokule

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gjon

ASKER
ooh, I didn't understand this. How would I make 360 straight up?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gjon

ASKER
Thank you for explaining it to me. I don't know how to change where the 360 mark is, so I adjusted my measurements as if 270 is my zero.