Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:11:29+00:00 2026-05-28T14:11:29+00:00

Oh god this must be so simple. I have a heading in range (0,

  • 0

Oh god this must be so simple. I have a heading in range (0, 2π) and two points from which I get the heading in between. I must compare them to see if one is within a range of the other. What I’ve got so far is.

//get the angle
float angle = atan(here.x - there.x, here.y - there.y);

//atan2 uses (-pi, pi) range, convert to (0, 2pi)
if(angle < 0) angle += 2*pi;

//subtract them pesky headings
float diff = angle - givenAngle;

//a difference of 350 degrees really is a difference of 10 degrees
if(diff > pi) diff = 2*pi - diff;

//a difference of -10 degrees really is a difference of 10 degrees
if(diff < 0) diff *= -1;

//check if the point is in range of givenAngle
if(diff > fov) do_magic(diff - fov);

However, I get all sorts of issues when both angles wrap around to zero and I’ve been wasting way too much brainpower in solving this solved problem.

Where am I doing it wrong? How can I find the difference between two headings correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:11:30+00:00Added an answer on May 28, 2026 at 2:11 pm

    One of the main problems was in this line:

    float angle = atan(here.x - there.x, here.y - there.y);
    

    It was kind of tricky to notice, but that’s not the right argument order for atan – even the mathematical definition on wikipedia takes the y component (sine) before the x component (cosine).

    float angle = atan(here.y - there.y, here.x - there.x);
    

    A second problem is that, as it turns out, angle was off by 180 degrees. In other words, instead of calculating here - there, I should be calculating there - here.

    float angle = atan(there.y - here.y, there.x - here.x);
    

    Add in sverre’s observation about the incorrect order of operations, and we have something that works much better:

    float angle = atan(there.y - here.y, there.x - here.x);
    if(angle < 0) angle += pi * 2; //use (0, 2pi) range, the same as angle
    float diff = abs(angle - givenAngle);
    if(diff > pi) diff = 2*pi - diff;
    if(diff > fov) do_magic(diff - fov);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

some comments here have confused me! I thought I knew this and god knows
I am trying to have God monitor two instances of Resque, one for production
I have a class that I am monitoring using god. This is it's structure:
I have this route: {controller}/{id}/{action} Because I think it makes more sense from RESTful
Really wish i was God, then I would understand what this means: The element
My God, EF is so frustrating. I can't seem to be able to get
I have this error when i perform the following task, results = db1.executeSelectCommand(siteSql, (),)
God i feel like i'm spamming stackoverflow, this is my 3rd post for today.
I must be missing something simple, but I can't see it. First, the setup:
This might be a god awful question but I'm not sure why it won't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.