# [.net] How to draw a line from the center of a circle to a random point on the diameter of t

This topic is 3908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
The diameter is twice the radius. What I think you are looking for, based on your description, is how to find the coordinates of a point on a circle's perimeter (circumference) given the degrees. This becomes a trigonometry problem involving a right triangle.

Here's a sample solution. First, there's four quadrants that you can divide the circle into. So to begin with, pick a random number of degrees, and reduce it to below 90 by subtracting 90 consecutively. Now assume the center of the circle is at 0,0 (you can adjust later). Then

x = r * cos(alpha)
y = r * sin(alpha)

where x,y are coordinates of the point you are looking for, r is the radius of the circle, and alpha is an angle between 0 and 90 degrees (convert to radians if your library uses radians). Now you have a point, but it's in the first quadrant ("upper right"). You need to rotate the point around the center in a multitude of 90 degrees to get it to the quadrant you want. Since we defined the center to be 0,0 you can rotate:

newx = x * cos(beta) - y * sin(beta)
newy = x * sin(beta) + y * cos(beta)

where x,y is the point on the circle, and beta is a multitude of 90 degrees. Now you can move both origin (0,0) and point (newx,newy) to where you want them on the screen, and draw a line segment between them.

Here's an illustration :-)

Well, I think that's about right. I haven't really done trig in a while. There seems to be something more to your question though. Maybe you can draw a diagram explaining it?

##### Share on other sites
Sure, let me clarify it a little more. Ok, meter dials rotate clockwise and counter clockwise beginning from the right dial to the left dial.
Meter dials have 10 numbers on them as opposed to a regular time clocks 12.
These numbers are from 0 - 9.

What happens is that after electrical usage, the dials spin. I want to be able to read these dials from a distance without having to actually read the numbers. I figure if I practice reading them from just the hand positions since I can see those from far away that that would speed up my reading greatly since I read a great number of these dials all day long.

So, I figure if the program could draw these hands/lines randomly at certain clock positions, I could use that to practice. If the first dial randomly chose position 2 which is about where the 3 0'clock position is on a regular time clock, I would then guess that it was a 2. I would set the degrees or range of where a 2 would be on this dial and have the program check whether I guessed the number correctly.

I hope that explains it much better. I really need to see an example of what you are explaining, it's mostly over my head.

Thank you so much for replying.

##### Share on other sites
Ok, I can vaguely see how it relates to game programming (you are making a game in a sense). So the dials move in a continuum and end up somewhere along the way. In the end, it looks like the formulae above do apply to your situation.

The example above is pretty simple actually, here's how it works: If you think of any point on a circle with radius r, that point is always distance r from the origin. Now if you drop down a line from that point to the y=0 line (the horizontal), you can think of the radius, the line you dropped, and the line from that line's intersection with y=0 to the origin, as a right triangle (see diagram in previous post).

Right triangles (triangles where one of the angles is 90 degrees) have special properties in that you can always determine the third side if you know two sides (Pythagorean Theorem), and also, what's interesting in this case, you can always determine any side if you know any other side and one of the acute angles. So what you do with those formulas above, you are actually solving for the lengths of the sides of that triangle (which also happen to be the coordinates of the point you are looking for). The radius is the hypothenuse, and you transform the formulae involving hypothenuse and opposite side, and hypothenuse and adjacent side.

The answer is always gonna be positive though, which means you are restricted to 90 degrees (all positive points are in the first quadrant - both x and y are positive there). That's why, for numbers larger than 90 degrees, you need to figure out which quadrant the point is in, and then rotate to get there. By the way, I was measuring degrees from the y=0 horizontal line, going counterclockwise, which is, as far as I know, the correct way to measure them. So if you take that frame of reference, 90-180 degrees is 2nd quadrant, 180-270 is 3rd quadrant, etc.

##### Share on other sites
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.

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

}
}

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013451
×