user4594580
user4594580

Reputation:

Calculate a simple angle

I'm calculating the angle using 2 points of a square.

The angle works well but it gives me values from -180 to 180 and it's hard for me to code the direction of my robot. I wanted the angle only with position values ex: 0 - 360;

var deltay = pontos_quadrado[0].Y - pontos_quadrado[1].Y;
var deltax = pontos_quadrado[1].X - pontos_quadrado[0].X;
angulo = Math.Atan2(deltay, deltax) * 180 / Math.PI;

angulo = Math.Round(angulo, 0);

Upvotes: 0

Views: 133

Answers (1)

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

You can force the angle to be in the range of 0 to 360 by adding 360 and taking the remainder modulo 360.

var deltay = pontos_quadrado[0].Y - pontos_quadrado[1].Y;
var deltax = pontos_quadrado[1].X - pontos_quadrado[0].X;
angulo = Math.Atan2(deltay, deltax) * 180 / Math.PI;

angulo = (angulo + 360) % 360; // note this line

angulo = Math.Round(angulo, 0);

Upvotes: 4

Related Questions