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

  • Home
  • SEARCH
  • 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 937133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:25:39+00:00 2026-05-15T21:25:39+00:00

To me, this is the obvious way to do this: given a start point

  • 0

To me, this is the obvious way to do this: given a start point and an end point, tell me if the end point is up / down / left / upleft / downright… etc of the start point. Heres the core of my logic :

function getSector() {
    var y = startY - endY;
    var x = startX - endX;
    var angle = Math.atan2(y,x) * 57.29578;//~57 deg per radian
    //atan2(y,x) returns a num between -PI and PI, represents angle in radians
    console.log("angle:" + angle);
    if( (angle > -22.5) && (angle < 22.5) ) attackDir = "left";
    if( (angle > 22.5) && (angle < 67.5) ) attackDir = "upleft";
    if( (angle > 67.5) && (angle < 112.5) ) attackDir = "up";
    if( (angle > 112.5) && (angle < 157.5) ) attackDir = "upright";
    if( (angle > 157.5) && (angle <= 180) ) attackDir = "right";
    if( (angle > -179) && (angle <= -157) ) attackDir = "right";
    if( (angle > -157) && (angle < -112.5) ) attackDir = "downright";
    if( (angle > -112.5) && (angle < -67.5) ) attackDir = "down";
    if( (angle > -67.5) && (angle < -22.5) ) attackDir = "downleft";
    console.log("attackDir:" + attackDir);
}

I’m interested in seeing a better way, but more so, HOW you arrived at that way.

I guess a key of sorts would be in order :
left = 0
up = 90
right = 179 OR -179 (down is like a negative mirror of up)
down = -90

  • 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-15T21:25:40+00:00Added an answer on May 15, 2026 at 9:25 pm

    When doing bounds checking, it’s much more readable if you create a function named Between(min, val, max);

    function Between(min, val, max)
    {
     return (val > min) && (val < max);
    }
    

    In addition, you can test for right/left, and then after that test for down/up.. while appending to a string. Then your logic is cut down to 4 if statements.

    var final="";
    if(/*IsUp*/) final="up"
    else if(/*IsDown*/) final="down"
    
    if(/*IsRight*/) final+= "right";
    else if(/*IsLeft*/) final += "left";
    

    Edit: As stated by Stefan, 57.29578 is a magical number. I didn’t even realize you meant 180/(Math.Pi)… As he also stated, the “optimization” you’re making makes your code just illegible, and the performance gain is moot.

    You should always check for u/d/l/r by x/y position relative to an origin, not by angles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an obvious way to do this that I'm missing? I'm just trying
This might seem obvious but I've had this error when trying to use LINQ
you would think this would be obvious, but searching through documentation, SAP forums, Googling,
I'm new to jquery and asp.net so please forgive if this is an obvious
I already know the obvious answer to this question: just download <insert favorite windows
I know this sounds like a really obvious question, but it's proving harder to
From this post . One obvious problem is scalability/performance. What are the other problems
I know this is probably really obvious, but I cannot figure out why I
Having a heckuva time with this one, though I feel I'm missing something obvious.
I have to be missing something obvious here. I don't get why this cast

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.