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.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 39401
In Process

askthedev.com Latest Questions

Asked: April 13, 20252025-04-13T18:14:02+05:30 2025-04-13T18:14:02+05:30

How can I find a point a specific distance between two given points in 2D space using C#?

anonymous user

I’ve been tackling a little challenge in C# that I think you might have some insight on. So, here’s the deal: I’ve got two points in 2D space, which I’ve labeled as Position A at (0,0) and Position B at (10, 5). What I’m trying to do is find another point, which I’m calling Position C, that’s a specific distance from Position A, precisely 5 units away, and directed towards Position B.

Now, I’ve come across this distance formula: (BX-AX) + (BY-AY) / 2. But here’s where I need some clarification. You see, it seems like this equation could yield multiple points at that distance from A, and I want to ensure that I’m pinpointing the exact location that lies directly on the line between A and B. Otherwise, I could end up with a point anywhere in the vicinity of Position A that fits the distance criteria, and that’s not what I’m aiming for!

I’ve understood that the equation is essentially about normalizing the distances between coordinates and averaging them out, but I’m struggling to visualize how to properly implement it. After getting the differences between the points, how exactly do I determine which point lies on the direct line from A to B?

To illustrate the problem further, I know that if I just use a simple linear interpolation, it seems straightforward, but I’m not entirely sure how to structure it to ensure the direction is right. I feel like there’s a straightforward solution here, but I just can’t wrap my head around it.

Would it be possible for you to break this down for me? Maybe give me a clear step-by-step approach or some pseudocode? I’d really appreciate any help you can offer, particularly if you can keep it simple with no heavy math jargon that’s tough to work with. Thanks for any guidance you can provide!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2025-04-13T18:14:04+05:30Added an answer on April 13, 2025 at 6:14 pm

      Finding Position C in 2D Space

      To find Position C, which is 5 units away from Position A (0,0) and directed towards Position B (10,5), we can follow a simple approach.

      Step-by-Step Approach:

      1. Calculate the Direction Vector:

        First, you need to find the direction from A to B. This is done by subtracting the coordinates of A from B:

                    DirectionX = BX - AX = 10 - 0 = 10
                    DirectionY = BY - AY = 5 - 0 = 5
                    
      2. Calculate the Length of the Direction Vector:

        Next, you need to find the length of this vector using the distance formula:

                    Length = sqrt(DirectionX^2 + DirectionY^2) 
                           = sqrt(10^2 + 5^2) 
                           = sqrt(100 + 25) 
                           = sqrt(125) 
                           = 11.18 (approximately)
                    
      3. Normalize the Direction Vector:

        Now, we want the direction of the vector as a unit vector:

                    UnitVectorX = DirectionX / Length
                                 = 10 / 11.18 ≈ 0.894
                                 
                    UnitVectorY = DirectionY / Length
                                 = 5 / 11.18 ≈ 0.447
                    
      4. Scale the Unit Vector to the Desired Distance:

        Since we want Position C to be 5 units away from A, we multiply each component of the unit vector by 5:

                    Cx = AX + (UnitVectorX * 5)
                       = 0 + (0.894 * 5) ≈ 4.47
                       
                    Cy = AY + (UnitVectorY * 5)
                       = 0 + (0.447 * 5) ≈ 2.24
                    
      5. Position C Coordinates:

        So, Position C will be approximately at:

                    C(4.47, 2.24)
                    

      This gives you the point C, which is exactly 5 units away from A and directed towards B. You can implement this in C# using the steps outlined above!

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2025-04-13T18:14:05+05:30Added an answer on April 13, 2025 at 6:14 pm

      The key idea here is to first calculate the direction vector between Position A and Position B. To do this clearly, subtract A’s coordinates from B’s coordinates: DirectionX = BX – AX and DirectionY = BY – AY. In your particular case, Position A is (0,0) and Position B is (10,5), so your direction vector is (10,5). Next, calculate the length of this vector using the formula sqrt(DirectionX² + DirectionY²). In this example, the length is sqrt(10² + 5²) which is approximately 11.18. Then, to ensure Position C is exactly 5 units away from Position A, we divide each component of the direction vector by its length (normalizing it) and multiply by the target distance (5 units). This looks like: Cx = AX + (DirectionX / Length) * Distance, and Cy = AY + (DirectionY / Length) * Distance.

      So in short, to clearly implement this in pseudocode (C# style), it looks something like this:
      float DirectionX = BX – AX;
      float DirectionY = BY – AY;
      float Length = Math.Sqrt(DirectionX * DirectionX + DirectionY * DirectionY);
      float Distance = 5;
      float Cx = AX + (DirectionX / Length) * Distance;
      float Cy = AY + (DirectionY / Length) * Distance;
      This method ensures that Position C will always lie directly on the line segment joining Positions A and B, exactly at the specified distance from A, thus giving you a precise and correct solution without ambiguity.

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

    Sidebar

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    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.