D
D
Drottarutarnum2020-07-26 22:24:42
C++ / C#
Drottarutarnum, 2020-07-26 22:24:42

How to rotate a line from the center at an angle?

I need an arrow (line) that points from the center of the pictureBox position (from 0 to 360)

The code that I have now (not working)

void Render() {
            g.Clear(Color.White);

            DraLine(30, Color.Black);

            g.Dispose();
        }


  

        private double Radian(float angle) {
            return (Math.PI / 180.0) * angle; 
        }


        void DraLine(float angle, Color color) {
            Pen pen = new Pen(color, 3);

            double length = pictureBox_motor.Size.Height / 2;
            double x = pictureBox_motor.Size.Width / 2;
            double y = pictureBox_motor.Size.Height / 2;

            var center = new Point((int)x, (int)y);



            var endPoint = new Point();
            if(angle <= 45) {
                endPoint.X = (int)x;
                endPoint.Y = (int)(x - x * Math.Tan(Radian(angle)));
            } else {
                endPoint.Y = (int)y;
                endPoint.X = (int)(x * Math.Tan(Radian(90 - angle)));
            }


            g.DrawLine(pen, center, endPoint);
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Developer, 2020-07-27
@samodum

What's with the tangents?
x - through cosine, y - through sine.
And no condition is needed.
And multiplication not by x, but by radius
UPD: Let's remember the geometry lessons for the 7th grade.
x1, y1 - coordinates of the beginning of the segment (picture center)
x2, y2 - second end
x2 = x1 + cos(a)*R
y2 = y1 + sin(a)*R

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question