Given an angle and length, how do I calculate the coordinates

后端 未结 5 1529
太阳男子
太阳男子 2020-12-30 02:18

Assuming the upper left corner is (0,0) and I\'m given an angle of 30 degrees, a starting point of (0,300), a line length of 600, how do I calculate the ending point of the

5条回答
  •  感情败类
    2020-12-30 03:10

    People are forgetting the complex library in C++, which does polar to rectangular conversions for us.

    complex getEndPoint(complex const &startPoint, double magnitude, double radians)
    {
        return startPoint + polar(magnitude, radians);
    }
    
    int main()
    {
        complex startingPoint(0.0, 300.0);
        auto newPoint = getEndPoint(startingPoint, 600, 0.523598776);
    
        cout << newPoint << endl;
    }
    

    I'd also be careful with your chosen terminology. When I see get in a name, I think of it as retrieving an answer stored somewhere. In this example, we're computing something, and that could be false assurance given to a user of your code.

提交回复
热议问题