my original question with a given answer: How to animate a drawline to move in a circle?
the goal is to make a drawn line to rotate nonstop 360 degrees and to be able to set the line length(radius) and position(pivot point/rotation point).
The form1 size is 1000,1000 and pictureBox1 size is 800,800
when using this code it's drawing a red line and rotating it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace Line_Rotation
{
public partial class Form1 : Form
{
float angle = 0;
System.Timers.Timer timer = new System.Timers.Timer();
public Form1()
{
InitializeComponent();
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
angle += 0.1f;
pictureBox1.Invalidate();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
DrawLine(pictureBox1,e.Graphics, angle);
}
public void DrawLine(PictureBox pb, Graphics g, float angle)
{
double radius = pictureBox1.ClientSize.Width / 2;
var center = new Point(100,100);
// remember that sin(0)=0 and cos(0)=1
// so if angle==0 the line is pointing up
var end = new Point((int)(radius * Math.Sin(angle)), (int)(radius * Math.Cos(angle)));
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawLine(new Pen(Color.Red, 2f), center, end);
}
}
}
but now I want to change the line radius(length) and also t change the line center point. for example, that the center point will be the pictureBox1 center and the length of the line(radius) will be for example 100.
so I changed a bit the method DrawLine:
public void DrawLine(PictureBox pb, Graphics g, float angle)
{
double radius = 100;
var center = new Point(pictureBox1.ClientSize.Width / 2, pictureBox1.ClientSize.Height / 2);
// remember that sin(0)=0 and cos(0)=1
// so if angle==0 the line is pointing up
var end = new Point((int)(radius * Math.Sin(angle)), (int)(radius * Math.Cos(angle)));
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawLine(new Pen(Color.Red, 2f), center, end);
}
change the radius variable value to 100 and changed the center variable to be the middle of the pictureBox1 point and also added to the end variable a cast (int) on both sides.
the result now is a long line from the center of the pictureBox1 but it's not radius 100 it looks much more then length 100 of the line and also the line is not rotating 360 degrees as before the changes it's just moving back and forth like trying to start rotating but then moving back and so on it's never rotating 360 again.