Draw a line using an angle and a point in OpenCV

DualSim picture DualSim · Mar 7, 2014 · Viewed 15k times · Source

I have a point and an angle in OpenCV, how can I draw that using those parameters and not using 2 points?

Thanks so much!

Answer

Haris picture Haris · Mar 8, 2014

Just use the equation

x2 = x1 + length * cos(θ)
y2 = y1 + length * sin(θ) 

and θ should be in radians

θ = angle * 3.14 / 180.0

In OpenCV you can rewrite the above equation like

int angle = 45;
int length = 150;
Point P1(50,50);
Point P2;

P2.x =  (int)round(P1.x + length * cos(angle * CV_PI / 180.0));
P2.y =  (int)round(P1.y + length * sin(angle * CV_PI / 180.0));

Done!