# Vector Math help

This topic is 3842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Sorry to be an annoying git, but I freakin suck at math, I've been struggling with this simple vector problem for about an hour now, so I just said fuck it and decided to post begging for help. Basically, I have 2 2d vectors. Each is described by an x and y coordinate. Let's call them Vec1 and Vec2. I have to extract a component vector from Vec2 that is perpendicular to Vec1. Here is a picture to illustrate: Sounds pretty simple but I can't seem to pull it off. Can somebody explain in really simple terms how to do this? It would be even better if you could write it in pseudocode.

##### Share on other sites
blue = black + pink
pink = blue - black

my lame guess. Could be wrong.

##### Share on other sites
ORTHOGONAL(black,blue)  Project blue onto black  real factor = (dot blue black) / |black|   vector project = factor black   Substract the projection  return blue - project

##### Share on other sites
Quote:
 Original post by RivieraKidblue = black + pinkpink = blue - blackmy lame guess. Could be wrong.

Unfortunately, this doesn't work for all vectors of varying magnitude that point in the same direction. It would work if the |v1|2 - |v2|2 = |v3|2, but we don't know if that is the case.

ToohrVyk has it right.

##### Share on other sites
Tossing this over to the Math & Physics forum.

##### Share on other sites
Quote:
 Original post by ToohrVykORTHOGONAL(black,blue) Project blue onto black real factor = (dot blue black) / |black| vector project = factor black Substract the projection return blue - project

Thanks. I translated your code into a c++ function. I'm not sure that it's done right though.

void Ortho(double vec1x,double vec1y,double vec2x,double vec2y) {double factor = (vec1x*vec2x+vec1y*vec2y)/sqrt(vec1x*vec1x+vec1y*vec1y);double projx=factor*vec1x;double projy=factor*vec1y;resultx=vec2x-projx;resulty=vec2y-projy;}

##### Share on other sites
Everyone sucks at math, but be patient and work with her :)

void Ortho(double vec1x,double vec1y,double vec2x,double vec2y) {double factor = (vec1x*vec2x+vec1y*vec2y)/sqrt(vec1x*vec1x+vec1y*vec1y);double projx=factor*vec1x;double projy=factor*vec1y;resultx=vec2x-projx;resulty=vec2y-projy;}