# How do I find the correct vectors perpendicular to a vector in between two vertices?

I'm trying to push the purple vertices inward by moving it with a vector perpendicular to the red vector shown in the picture.

Each directional vector created will move its respective vertex towards the center. How do I control what direction these perpendicular vectors are going?

Edit: Here's a more descriptive picture illustrating my problem:

So the yellow vertices do move in a direction perpendicular to the direction of the edge vertex (red).

However, they all move in the same direction. This is just a base case but I need to iterate through all of the yellow vertices and make each of the move towards the middle like this:

For each yellow vertex, I want to control them so they move inward towards the middle but no success with the dot product. Here's a snippet of my code:

for (int i = 0; i < yellowVertexArray.Length; i++)
{
Vector2 originToRedVertDir = (redVertexPos - originOfTransform).normalized;
Vector2 perpendicularDir = new Vector2(directionToInitial.y, -directionToInitial.x);

Vector2 cornerToMiddleDir = (yellowVertexArray[i] - redVertexPos).normalized;

if (Vector2.Dot(cornerToMiddleDir, originToRedVertDir) > 0)
{
yellowVertexArray[i].position += (Vector3)perpendicularDir * Time.deltaTime;
}
else if (Vector2.Dot(cornerToMiddleDir, originToRedVertDir) < 0)
{
yellowVertexArray[i].position += -(Vector3)perpendicularDir * Time.deltaTime;
}
}

• You're using the wrong vectors in your dot product. You can write this more simply as SelectedYellowVerts[i].position -= Mathf.Sign(Vector2.Dot(cornerToMiddleDir, perpendicularDir)) * perpendicularDir * Time.deltaTime; with no if at all. Commented Aug 29, 2021 at 20:22
• Yep that works. Thank you so much for the timely responses. That was a dumb mistake on my end. @DMGregory Commented Aug 29, 2021 at 21:02

In 2D you can form a perpendicular with a very simple trick — exchange the x and y components, and negate one of them:

purple.x =  red.y
purple.y = -red.x


There are two ways to do this of course — we could have negated either x or y. That corresponds to the two different purple arrows in your diagram: one negated x and one negated y. (Or looking at it a different way, one purple vector is -1 times the other)

To make the purple vector point inward instead of outward, you can take the dot product of the purple vector with the vector from the corner vertex you're working on to the middle vertex:

dot = purple.x * (middle.x - corner.x)
+ purple.y * (middle.y - corner.y)


If the dot product is positive, then the purple arrow is pointing inward toward the middle. If it's negative, then it's pointing outward — multiply both components by -1 to fix that.

• Would this work regardless of the direction of the initial vector (red vector)? I'd want the vertices between the direction of the initial vector to move inward towards it no matter where it is like so: i.imgur.com/x6iZmsp.png @DMGregory Commented Aug 27, 2021 at 20:07
• That sounds like a question you can answer by yourself by testing it. If you find it doesn't behave the way you want, edit your question to demonstrate a test case for the unwanted behaviour and explain what should happen instead in that case. Commented Aug 27, 2021 at 20:11