Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
how can I draw a circle in gdi+ , I wanna use DrawLine function
I know the formula is x^2+Y^2=r^2
((actually my math was weak!))

but how can i make a circle by timer tick ??
I mean it draw the circle by a matter of time
(for example by each second draw one diameter of the circle)
at last you see a colored circle .


please post snippet code .:rose:
I have something like this

C#
private void timer1_Tick(object sender, EventArgs e)
{

Graphics gr;
            
            gr = this.panel1.CreateGraphics();

            if ((Math.Pow(x, 2.0) + Math.Pow(y, 2.0)) == Math.Pow(4, 2.0))
            {
                gr.DrawLine(Pens.Blue, x1, y2, 200, 200);
            }
x1+=10;
y2+=10;
}
Posted
Updated 3-Mar-10 0:47am
v2

I'm not sure why you are using a timer to draw, but if it is a circle you want, and for some reason you do not want to use the Graphics::DrawEllipse methot, than you should deffinately look at parametric equations first (Click[^])

I refuse to give you a redy to use code, as I think you can benefit from implementing this particullar exercise yourself. This is the way to do it:

1. The parametric equation for a circle is:
x = a * cos(t)
y = a * sin(t) 

where a is the radious

2. Using the timer you have to increment the value of t - the parameter. This actually is the angle in radians, but you can read about that later.
The important thing is that by changing the value you get the next point to draw. Remember that the increment value needs to be small enough, otherwise you will end up drawing a polygon, not a circle. Well you draw a polygon anyway - but using small steps you can at least make it look like a circle :)

3. Each time you increment t you get the new values of x and y - this is the current point on your circle. By drawing a line from the previous point to the current point you draw the circle.

4. t should equal 0 when you start

5. reaching t == 2 * PI should get you a full circle!

Hope this helps - let me know how it goes :)


EDIT:
To answer the second revision of your question:
"but how can i make a circle by timer tick ??
I mean it draw the circle by a matter of time
(for example by each second draw one diameter of the circle)
at last you see a colored circle ."


I'm guessing from your edit that what you need is a kind of a progress indicator by means of a circle. In that case I *strongly* suggest you use the following function:

Graphics.FillPie( Brushes.Green, myRectangle, 0, (float)( 360 * ElapsedTime ) / MaxTime )


Where:
myRectangle is the area in which the part of the circle will be drawn
ElapsedTime is the time that passed since the drawing of the circle began
MaxTime is the point in time when the circle should be fully drawn


If you still want to use lines to draw the full circle - my first answer still applies - with small modifications - you draw the line from the center of the circle to the newly calculated point, and not to the previously painted point. But trust me - this method is not elegant and will probably look ugly. Think about the FillPie method instead!
 
Share this answer
 
v2
nimanaqipoor wrote:
how can i make a circle by timer ?


That doesn't make any sense. Please edit your question to clarify what you mean.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900