[.net] 360 degrees is straight to the right, but I want 360 degrees to be straight up when t

Started by
10 comments, last by benryves 15 years, 12 months ago
C#/VS2005 360 degrees is straight to the right, but I want 360 degrees to be straight up when the line is drawn in my circle. Can someone please show or explain how I can adjust this? I am guessing that something needs to be adjusted in the lines: x = x0 + (float)(Math.Cos(((double)degrees / 360) * 2 * Math.PI) * length); y = y0 + (float)(Math.Sin(((double)degrees / 360) * 2 * Math.PI) Code Snippet 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; } } } } }
I rate users. Its just not having much impact as my rating is low...
Advertisement
Just subtract 90 degrees from the angle.

x = x0 + (float)(Math.Cos(((double)(degrees - 90) / 360) * 2 * Math.PI) * length);y = y0 + (float)(Math.Sin(((double)(degrees - 90) / 360) * 2 * Math.PI) 
Mike Popoloski | Journal | SlimDX
Thank you so much for the suggestion. When I do as you say:

x = x0 + (float)(Math.Cos(((double)degrees - 90 / 360) * 2 * Math.PI) * length);
y = y0 + (float)(Math.Sin(((double)degrees - 90 / 360) * 2 * Math.PI) * length);

It seems to break everything. All the lines line up straight right at 360'
I rate users. Its just not having much impact as my rating is low...
Quote:Original post by Mike.Popoloski
Just subtract 90 degrees from the angle.

*** Source Snippet Removed ***


Multiply and divide have a higher precidence than addition and subtraction. You're actually doing:

degrees - (90 / 360)

You'll missing a pair of brackets around your "degrees - 90".
thank you!
I rate users. Its just not having much impact as my rating is low...
Quote:Original post by OrangyTang
Quote:Original post by Mike.Popoloski
Just subtract 90 degrees from the angle.

*** Source Snippet Removed ***


Multiply and divide have a higher precidence than addition and subtraction. You're actually doing:

degrees - (90 / 360)

You'll missing a pair of brackets around your "degrees - 90".


Just to be clear, did somebody edit my post for me? Because it seems to me that my snippet contains the correct brackets.
Mike Popoloski | Journal | SlimDX
Quote:Original post by Mike.Popoloski
Quote:Original post by OrangyTang
Quote:Original post by Mike.Popoloski
Just subtract 90 degrees from the angle.

*** Source Snippet Removed ***


Multiply and divide have a higher precidence than addition and subtraction. You're actually doing:

degrees - (90 / 360)

You'll missing a pair of brackets around your "degrees - 90".


Just to be clear, did somebody edit my post for me? Because it seems to me that my snippet contains the correct brackets.

Sorry, I think I quoted you by accident. Foolios's code had the missing brackets.
You could always swap cos/sin around also.
Quote:Original post by Niksan2
You could always swap cos/sin around also.


How do you mean?
I rate users. Its just not having much impact as my rating is low...
x = x0 + (float)(Math.Sin(((double)degrees / 360) * 2 * Math.PI) * length);
y = y0 + (float)(Math.Cos(((double)degrees / 360) * 2 * Math.PI) * length);

instead of

x = x0 + (float)(Math.Cos(((double)degrees / 360) * 2 * Math.PI) * length);
y = y0 + (float)(Math.Sin(((double)degrees / 360) * 2 * Math.PI) * length);

This topic is closed to new replies.

Advertisement