|
-
Apr 1st, 2008, 04:40 PM
#1
Thread Starter
Hyperactive Member
VS2005 C# a line from the center of a circle to a random point on the diameter of t
How to draw a line from the center of a circle to a random point on the diameter of that circle.
I am hoping to make a lil' proggy that will help improve my ability to read dials at work. I removed the dial numbers and just want to be able to read them by position. So far I have the five circles drawing up right where I want them on the form. I can draw a line, but I can't figure out how to have the line go to random points on the diameter. I imagine that what I'll need it to do is draw itself from a center location to the diameter's edge. A random numbered degree 1-360 I am guessing. Then once that's working take a number of the degrees and assign them to a number of the dial. So a range of 1-10' might be the numeral one on the dial.
Well, I can only hope that this could work out something like that.
Anyways here's what I have so far. I really don't know what I am doing, but I would really appreciate some assistance in having such a handy app to study from. I can print out a hundred sheets and just right on them, but this iiiiis 2008 so maybe some software would be ideal; SAVE THE TREES? hehe
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MeterDials
{
public partial class DialsForm : Form
{
public DialsForm()
{
InitializeComponent();
}
private void DialsForm_Load(object sender, EventArgs e)
{
}
private void Bttn_Reset_Click(object sender, EventArgs e)
{
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
formGraphics.Dispose();
}
private void DialsForm_Paint_1(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen mypen = new Pen(Color.Black, 2);
SolidBrush mybrush = new SolidBrush(Color.Blue);
//g.FillEllipse(mybrush, 10, 50, 150, 150);
g.DrawEllipse(mypen, 20, 20, 100, 100);
g.DrawEllipse(mypen, 140, 20, 100, 100);
g.DrawEllipse(mypen, 260, 20, 100, 100);
g.DrawEllipse(mypen, 380, 20, 100, 100);
g.DrawEllipse(mypen, 500, 20, 100, 100);
}
}
}
A sample I found that may help figure something out:
Edge coordinates:
x_edge coordinate = x_center + Math.Cos(u) * radius
y_edge coordinate = y_center - Math.Sin(u) * radius
where u is the angle measured in radians. To convert from degrees to radians, use: degrees * Math.PI / 180.
Where I have no idea where to begin with this.
Thank you so much for your time in reviewing this.
Thanks in advance for any assistance.
-
Apr 1st, 2008, 05:23 PM
#2
Re: VS2005 C# a line from the center of a circle to a random point on the diameter of t
I believe they meant this

When you draw your line, you can already have the center: 20+(100/2) in the first case.
You just need the point on the edge of the circle. Because you want to place it at a random point, I believe that you should generate the angle (in degrees) randomly.
Convert to radians, you have the formula for doing that.
Calculate the 'x' edge, again you have the formula for that.
Calculate the 'y' edge point, for which you have the formula.
Keep in mind that it's just a step-by-step process. You calculate one value, place it in the next statement which is nothing but a 'translated' formula for you to use.
-
Apr 1st, 2008, 06:41 PM
#3
Thread Starter
Hyperactive Member
Re: VS2005 C# a line from the center of a circle to a random point on the diameter o
Generate the degrees randomly.
Random createRandom = new Random();
createRandom.Next(0, 360);
label1.Text = createRandom.ToString();
The result is a string System.Random
I don't understand why the string is displaying as such rather than a random number between 0 and 360.
-
Apr 1st, 2008, 08:28 PM
#4
Thread Starter
Hyperactive Member
Re: VS2005 C# a line from the center of a circle to a random point on the diameter o
Ok, got past that part.
I got incorrect cast at first, then switched everything to double.
But now I get invalid arguments in the last line. I don't know why. Thank you so much for showing me such a great example.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MeterDials
{
public partial class DialsForm : Form
{
public DialsForm()
{
InitializeComponent();
}
private void DialsForm_Load(object sender, EventArgs e)
{
}
private void Bttn_Reset_Click(object sender, EventArgs e)
{
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
formGraphics.Dispose();
Random createRandom = new Random();
double degrees = createRandom.Next(0, 359);
label1.Text = degrees.ToString();
double length = 10;
double x0, y0, x, y;
x0 = degrees;
degrees = createRandom.Next(0, 359);
y0 = degrees;
x = x0 + (Math.Cos((degrees / 360) * 2 * Math.PI) * length);
y = y0 + (Math.Sin((degrees / 360) * 2 * Math.PI) * length);
formGraphics.DrawLine(myPen, 0, 0, x, y);
}
private void DialsForm_Paint_1(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen mypen = new Pen(Color.Black, 2);
SolidBrush mybrush = new SolidBrush(Color.Blue);
g.DrawEllipse(mypen, 20, 20, 100, 100);
g.DrawEllipse(mypen, 140, 20, 100, 100);
g.DrawEllipse(mypen, 260, 20, 100, 100);
g.DrawEllipse(mypen, 380, 20, 100, 100);
g.DrawEllipse(mypen, 500, 20, 100, 100);
}
}
}
-
Apr 2nd, 2008, 02:07 AM
#5
Re: VS2005 C# a line from the center of a circle to a random point on the diameter of t
formGraphics.DrawLine(myPen, 0, 0, x, y);
The DrawLine() method wants floats and you are most likely supplying it with doubles based upon your previous operations. Convert them to (float).
-
Apr 2nd, 2008, 02:15 AM
#6
Re: VS2005 C# a line from the center of a circle to a random point on the diameter of t
My question is why are you using doubles at all? The numbers are supposed to represent coordinates and coordinates are always going to be integers, so those variables should be integers. Instead of changing the type of the variables you should be changing the type of the values you assign to them. The problem is that this expression:
CSharp Code:
Math.Cos((degrees / 360) * 2 * Math.PI)
is going to return a double. It's right THERE that you should be converting to an integer, then using that integer in the rest of the calculation, finally assigning the integer result to an integer variable.
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
|