
- VBForums
- .NET and More
- C#
- [RESOLVED] 360 degrees is straight to the right, but I want 360 degrees to be straight up when t
-
Apr 22nd, 2008, 06:05 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] 360 degrees is straight to the right, but I want 360 degrees to be straight up when t
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:
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; }
}
}
}
}
Last edited by gjon; Apr 22nd, 2008 at 04:27 PM.
-
Apr 22nd, 2008, 08:58 AM
#2
Re: 360 degrees is straight to the right, but I want 360 degrees to be straight up when t
1. After 439 posts we shouldn't have to ask you to wrap your code in tags.
2. Just subtract 90 degress, or pi/2 radians, from all your angles. You specify 360 (or 0) degrees, the code subtracts 90 degress and the value actually used will be 270, so the line will be vertical. If you specify 270 then the actual value used will be 180, so horizontal to the left. Etc.
-
Apr 22nd, 2008, 04:27 PM
#3
Thread Starter
Hyperactive Member
Re: 360 degrees is straight to the right, but I want 360 degrees to be straight up when t
My bad, i used code snippet, thinking that it would work. I'm rusty.
I think it's corrected now.

- VBForums
- .NET and More
- C#
- [RESOLVED] 360 degrees is straight to the right, but I want 360 degrees to be straight up when t
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|