Skip to content
ExamplesOpen editor ↗

atan2

→ atan2()

Returns the angle formed by a point, the origin, and the x-axis. **y comes first.**

atan2(y, x)

Watch the argument order: atan2(y, x) — y comes FIRST, x second. This is the one place in Bopa where that happens; everywhere else x leads (thalaSediko(x, y, bogolo), thalaMothalo(x1, y1, x2, y2)). It is the standard order in every language, so Bopa does not change it — and it is the mistake people make most.

What it does is answer one question: which way is that? Given a point, atan2 gives you the angle from (0, 0) out to it.

The usual use is pointing one thing at another:

metlha sekhutlo = atan2(y2 - y1, x2 - x1);

By default the result lies between -PI and PI; in degrees, between -180 and 180.

The name is left as it stands — see sin.

NameTypeDescription
ypaloThe y position of the point. It comes first.
xpaloThe x position of the point.
Lines all pointing at the middle
tiro setaLoeto() {
  thalaKanefase(400, 400);
  bokamorago(19, 24, 34);
  boketeMothalo(2);

  metlha fagareX = 200;
  metlha fagareY = 200;

  gopheta (diri x = 40; x < 400; x = x + 60) {
    gopheta (diri y = 40; y < 400; y = y + 60) {
      // Y PELE, X MORAGO — sekhutlo go tswa mo ntlheng go ya fa gare
      metlha sekhutlo = atan2(fagareY - y, fagareX - x);

      mothaloMmala(lekanyetsa(sekhutlo, -PI, PI, 70, 250), 160, 220);
      thalaMothalo(x, y, x + cos(sekhutlo) * 26, y + sin(sekhutlo) * 26);
    }
  }
}
atan2 tells apart what atan cannot
tiro setaLoeto() {
  thalaKanefase(400, 400);
  bokamorago(244);

  seeloSaSekhutlo(DEGREES);

  // Dintlha tse pedi tse di lebaganeng — serepodi se a lekana
  kwala(atan(4 / 3));
  kwala(atan(-4 / -3));

  // Mme atan2 e a di farologanya
  kwala(atametsa(atan2(4, 3)));
  kwala(atametsa(atan2(-4, -3)));

  boketeMothalo(3);
  mothaloMmala(60, 130, 200);
  thalaMothalo(200, 200, 200 + 3 * 30, 200 + 4 * 30);
  mothaloMmala(220, 120, 70);
  thalaMothalo(200, 200, 200 - 3 * 30, 200 - 4 * 30);
}

atan, sin, cos, seeloSaSekhutlo, sekgala