On which side of the vector on graph is the origin? [Geometry]
I have a vector on a graph. The position of the vector on the graph is known. The vector value is normalized and is known. If the graph is divided along the vector's position and direction, on which side is the origin, left or right?
float x = someValue;
float y = otherValue;
Vector2 vector = someVector.normalized;
// Find:
bool originIsOnRight;
Given these variables, how to calculate originIsOnRight?
Answer by Bunny83 · Apr 08, 2019 at 09:26 AM
The way you specify your input variables is very confusing. First of all a 2d vector is just a pair of two numbers (x and y). So the given values are actually two vectors. One that specify the point (x,y) and one that specify the direction you're interested in. Next keep in mind that vectors themselfs do not have a position. This is all just interpretation. Technically all vectors (whether they are positions or directions) start at the origin. It doesn't have to be the world origin and could be any space they belong to. However this information is not part of the vector itself.
About your issue: All you have to do is check the dot product between the vector from the origin to your point and the perpendicular of your vector / direction. This will tell you if the (world) origin is to the left or right. You get the perpendicular of a 2d vector by simply swapping it's coordinates and negate one of them. Which you negate will determine if you rotate 90° clockwise or counter clockwise.
For example:
Vector2 p; // our point (x,y)
Vector2 d; // our direction "vector"
Vector2 normal = new Vector2(-d.y, d.x); // clockwise rotation
float dot = Vector2.Dot(p, normal);
if (dot > 0)
{
// the origin is to the left of vector
}
else if (dot < 0)
{
// the origin is to the right of vector
}
else
{
// the vector passes through the origin
}
Are you sure this is actually a game development question? This sounds more like a school geometry question. I've moved the question into the help room since it's not really Unity related.
Ok by looking at your profile you seem to be serious and it's not a "school question" ^^. Btw if you do a counter clockwise rotation like this:
Vector2 normal = new Vector2(d.y, -d.x); // note the "-" has moved to the second argument
The left / right conditions would be inverted. So in this case if dot is greater than 0 the origin would be on the right, if it's smaller it would be on the left side.
The dot product is one of the cheapest vector operations you can do. The vectors do not need to be normalized. The dot product is simply:
float Dot(Vector2 a, Vector2 b)
{
return a.x * b.x + a.y * b.y;
}