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 6563583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:52:06+00:00 2026-05-25T13:52:06+00:00

public Point getPointOnSegment(double length, int x1, int y1, int x2, int y2) { if(length==0)

  • 0
public Point getPointOnSegment(double length, int x1, int y1, int x2, int y2) {
    if(length==0) return new Point(x2, y2);
    double angle = Math.atan((double)(y2-y1)/(double)(x2-x1));

    return new Point((int)((length*Math.cos(angle))+x1), (int)((length*Math.sin(angle))+y1));
}

I have found this function that is supposed to return a point on a line that is x distance along said line. But for some reason it sometimes returns a point in the opposite direction that is is supposed to. It seems to happen if the angle of the line is greater than 180 degrees.

I have been looking at this for way over 5 hours now and I am afraid that I have stared myself blind to the issue. Can anyone see what the problem is, or suggest a better function to substitute it?

  • 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-25T13:52:06+00:00Added an answer on May 25, 2026 at 1:52 pm

    The issue is that you loose signs with your division. For example if both distances are negative the result will be positive as it would be for both beeing positive.

    Try to calculate the angle using method atan2:

    angle = Math.atan2((double)(y2-y1), (double)(x2-x1));
    

    This function respects the signs of your differences in all cases and returns an angle for the complete clock. It also handles the special case for x2-x1 being zero.

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

Sidebar

Related Questions

Let's say I have a class Point: class Point { int x, y; public:
I have two classes, point and pixel: class point { public: point(int x, int
I have this code: public class Area { Texture2D point; Rectangle rect; SpriteBatch _sB;
I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
What's the point of using : int in the enum declaration as following? public
What do you guys find preferable? private Point location; public int LocationX { get
I have an interface like the following: package example; import java.awt.Point; public interface Thing
Consider the two classes: public class Point { public int x; public int y;
I have a point object: class Point { final int x,y; ... } Because
Say I've got a very lightweight object: public class Point { public int x;

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.