0

using timer I want the line to rotate in a circle nonstop.

public void DrawLine(PictureBox pb, Graphics g, float angle)
{
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    g.DrawLine(new Pen(Color.Red, 2f), new Point(pictureBox1.ClientSize.Width / 2, pictureBox1.ClientSize.Height / 2), new Point((int)w,(int)h));
}

in paint

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    DrawLine(pictureBox1, e.Graphics, 0);
}

instead, static drawn line to make the line around in circle without leaving lines behind just the single drawline.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • 1
    [Rotate Point around pivot Point repeatedly](https://stackoverflow.com/a/50478311/7444103) -- You (most probably) don't need `rotationSpin` -- Draw a line between the center point and the coordinates returned by `RotatePoint()` – Jimi Apr 29 '23 at 09:31
  • @Jimi it's working but how do I make that the drawn line between the points will start/end from each point center? now it's on the edge of the points and when the rotation speed is increasing the line loose some position from the rotating point. this is the only line I added to the code in the link to your answer: e.Graphics.DrawLine(new Pen(Color.Red, 2f), new Point(100,100), rotatingPoint); if it's not clear I will opend a new question. – Sheron Blumental Apr 29 '23 at 10:58
  • 1
    As mentioned, in that code `rotationSpin` increases the rotation speed incrementally. Remove it from the calculation if you need constant speed. Then you don't need to draw the points (the ellipses), just one line between the center point and the rating point – Jimi Apr 29 '23 at 12:14

1 Answers1

3

You have to define a variable double angle then increment it by some amount every tot milliseconds then just multiply the radius lenght by sin and cosine of than angle:

//using System.Timers;

public void DrawLine(PictureBox pb, Graphics g, float angle)
{
    double radius = pictureBox1.ClientSize.Width / 2;
    var center = new Point(radius, radius);
    // remember that sin(0)=0 and cos(0)=1
    // so if angle==0 the line is pointing up
    // EDIT HERE: added center.x and center.y 
    var end = new Point(center.X + radius * Math.sin(angle), center.Y + radius * Math.cos(angle));
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    g.DrawLine(new Pen(Color.Red, 2f), center, end);
}
public static void rotate(Object source, ElapsedEventArgs e)
{
    angle += <someAmountInRadiants>;
}
public static void SetTimer()
{
    rotationTimer.Elapsed += rotate;
    ...  // *for the uses of timers see below
}
double angle = 0;
Timer rotationTimer = new(<someAmountInMilliseconds>);
setTimer();

*Microsoft Wiki\Timer

To decide the speed of the rotation using total rotation time and steps you can just use Math.PI:

# seconds needed to complete a full rotation
double rotTime = 2; # what you want

# how many times per second the line get mooved
double rotSteps = 360; # what you want

# amount of rotation that the line has to do, we can use this variable in rotate()
double rotAmount = Math.PI / rotSteps; 

# every how much time increment the angle?
double rotDelay = rotTime / rotSteps; 

# we use this in the timer, so we need to convert from seconds to milliseconds
rotDelay *= 1000;