- Home /

# How to find perpendicular line in 2D?

2D game. z is always 0.

I have P1=(x1,y1) and P2=(x2,y2) points and they are specified. Also I have h distance from P1 point that is also known. And I want to find P3 and P4 that located on perpendicular line and h is distance to them. How can i found those P4=? P3=?

**Answer** by cjdev
·
Oct 27, 2013 at 02:18 PM

v = P2 - P1

P3 = (-v.y, v.x) / Sqrt(v.x^2 + v.y^2) * h

P4 = (-v.y, v.x) / Sqrt(v.x^2 + v.y^2) * -h

Basically, you first find the vector from P1 to P2. Then to get a perpendicular vector you take the negative inverse of the vector (switching the axes around and making one negative, which one you make negative determines the direction). After that, you find the normal of the perpendicular vector by dividing the vector by it's magnitude (length). Finally, you multiply by h and -h to get the vectors of the right length in each direction.

**Answer** by Ev
·
Oct 27, 2013 at 02:19 PM

Found how to do it:

```
var first = points[0];
var second = points[1];
var newVec = first-second;
var newVector = Vector3.Cross (newVec, Vector3.forward);
newVector.Normalize();
var newPoint = width*newVector+first;
var newPoint2 = -width*newVector+first;
Debug.DrawLine (newPoint,newPoint2);
```

**Answer** by BobbyDoogle
·
Feb 22 at 10:23 PM

Thought I would share the C# approach that worked for me with some inspiration of these answers:

```
Vector2 vNormalized = (P1.position - P2.position).normalized;
Vector2 vPerpendicular = new Vector2(vNormalized.y, -vNormalized.x).normalized;
Vector2 P4 = new Vector2(P1.position.x, P1.position.y) + (vPerpendicular * h);
```

I may have a redundant normalization but it is working great, and the idea is first get the vector from 1 to 2. Then make a new vector (y, -x) on this angle, switch to (-y, x) for P3.

Cheers,

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Obtaining a perpendicular point? 1 Answer

Calculate point coords which lays on normal vector 2 Answers

Getting perpendicular direction vector from surface normal 2 Answers

Change Global cartesian system vectors 1 Answer

Move an object according to a vector field (flow arrows placed in the scene) 1 Answer