Z
Z
Zakhar Guskov2021-09-15 23:46:16
Mathematics
Zakhar Guskov, 2021-09-15 23:46:16

How to rotate a line to a coordinate system?

Good evening! I don't understand what the error is when turning a straight line on a coordinate system. Given 2 points of a straight line, found that the angle of inclination of a straight line is calculated by the formula: (y2-y1) / (x2-x1). According to the resulting value, I take tg and convert it to radians.

private static double GetAngleOfInclination(Point a, Point b)
        {
            var angleOfInclination = (double)(b.Y - a.Y) / (b.X - a.X);
            angleOfInclination = Math.Tan(angleOfInclination);
            return GetRadiance(angleOfInclination);
        }

I also saw that you can rotate the points on the axis using the formulas:
x'=x*cosA + y*sinA
y'=y*cosA - x*sinA


private static double GetXPointWithInclination(double pointX, double pointY, double angleOfInclination)
        {
            var x = pointX * Math.Cos(angleOfInclination) + pointY * Math.Sin(angleOfInclination);
            return Math.Round(x, 1);
        }

        private static double GetYPointWithInclination(double pointX, double pointY, double angleOfInclination)
        {
            var y = -pointX * Math.Sin(angleOfInclination) + pointY * Math.Cos(angleOfInclination);
            return Math.Round(y, 1);
        }


As a result, the result is practically no different from the initial one. Expected result: for the line specified by the points to lie on one of the axes, therefore, X or Y must become zero for the points.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
Wataru, 2021-09-15
@Zuguki

You need to take the arc tangent from the ratio, not the tangent (y2-y1) / (x2-x1).

L
Lynn "Coffee Man", 2021-09-16
@Lynn

> take tg and convert to radians
First, you need the arc tangent.
Secondly, it will already be in radians.
Well, and thirdly, it is not necessary at all. You have two points and formulas to translate them into a new coordinate system. You just need to translate them there and you will have new coordinates of these points that describe the line.

G
Griboks, 2021-09-16
@Griboks

Expected result: for the line specified by the points to lie on one of the axes, therefore, X or Y must become zero for the points.

Coordinates of the first point: (0; 0),
second: (1; 0).
Now your straight line has automatically rotated to the desired angle and lies on the axis.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question