- 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.

Amazing code and wonderfully Explained Thank you Thankyou Dec @Crazydadz for answer and also @Bunny83 For the proper question... Thats what i wanted

Im so ashamed of not knowing the most basic highschool math formula, but you just saved me hours of trying to find the right formula, thanks so much man.

**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);
```

For me I couldn't get the solution by @cjdev to work but this one worked perfectly.

I was able to use this to furnish thousands of objects either side of a race track by having a mesh line follow the route of the course and generating a block on each side of the road at every point of the mesh.

Previously I'd furnished the track by manually placing blocks. This new way means I can edit my course, re-save the mesh line and have the course objects update at runtime.

Perfect. Will save me hours.

Uhm using Vector3.Cross does the same as what cjdev did, however including alot unnecessary calculations.

The cross product is simply:

```
c = a x b
c.x = a.y*b.z - a.z*b.y;
c.y = a.z*b.x - a.x*b.z;
c.z = a.x*b.y - a.y*b.x;
```

Now if you only have a 2d vector as "a" and (0, 0, 1) as "b" that means a.z is just 0 and b.x and b.y is also 0

```
c.x = a.y*1 - 0*0;
c.y = 0*0 - a.x*1;
c.z = a.x*0 - a.y*0;
```

So c.x simply becomes "a.y" and c.y becomes -a.x. That's the same thing that "cjdev" did. The division by the square root just normalizes the vector

**Answer** by BobbyDoogle
·
Feb 22, 2019 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