Sorry I'm not sure if here is the correct place to ask this.  

I'm actually having a problem with a calculation for a Game I make in Unity.

Anyway I'm posting this question here instead of StackOverflow because actually it is more the maths that are my problem here.

**What I'm trying to archive**

Given is this grafic:

[![enter image description here](https://i.stack.imgur.com/cKNdC.png)](https://i.stack.imgur.com/cKNdC.png)

In the application an Object can be dragged around from the position `B` using the mouse.  

But for the movement there are two restrictions:

* It can't be tragged further away from `B` as the radius `maxDistB`.
* It can't be tragged closer to `A` than the radius `minDistA`.

So in short it can be placed anywhere in the green space.

In my program I know

* position A
* position B
* maxDistB
* minDistA

My actual (pseudo) code looks like this:

```
if(mouseHolding) {
    newPosition = mousePosition;

    // Check the distance to A
    if(Distance(newPosition, A) > minDistA)
    {

        //Check the distance to B
        if(Distance(newPosition, B) <= maxDistB)
        {
            // in this case just accept the newPosition
            draggedObject.position = newPosition;
        } 
        else 
        {
            // otherwise dragg it in the correct direction but
            // place the object on the circle arround B

            // Get the direction of the dragg as normalized vector
            direction = (newPosition - B).Normalize;

            // Set the position of the Object to a limit range
            draggedObject.position = B + direction * maxDistB;
        }

    } else {

        // THIS IS WHERE I'M STUCK    

    }
}

```

**My Problem**

As you can see I stuck in the case where the object is dragged to close to `A`.

I know that for a movement on a streight line between `A` and `B` I could do e.g.

```
// Get the direction of the dragg as normalized vector
direction = (newPosition - B).Normalize;

// Get the distance between A and B
circleDist = |A - B|;
draggedObject.position = B + direction * (circleDist - minDistA);

```

But I have no clue how to calculate it, when dragging in another direction like shown in this image

[![enter image description here](https://i.stack.imgur.com/IXMbf.png)](https://i.stack.imgur.com/IXMbf.png)

The blue line would be the mouse dragging starting at `B`. I would like the object to end up on the red spot.

**How do I have to calculate this?**