Reputation: 32354
I'm making a simple simulation program and I've run into some problems... Inhabitants of the simulated world have 4 eyes: left, up, right and down. Only one eye can be active at the time, and that eye needs to point in the direction of the nearest object (basically telling the inhabitant is the nearest object left, up, right or down). Each eye has it's quadrant in the coordinate system, which is rotated from the main coordinate system by 45 degrees. All the numbers (positions, coordinates) actually have to do with the not rotated coordinate system, I just use the rotated one for the explanation, it isn't really there.
Here's a picture that will explain it better: So, the final question is: how to find out in which of these quadrants is the object? Given the object's position in the not rotated coordinate system.
Additional info:
Upvotes: 3
Views: 3656
Reputation: 27233
Let x, y be the coordinates of the object in the non-rotated coordinate system translated so that the eye lies at the origin. You can take advantage of basic properties of two linear functions whose charts partition your simulated space into the four quadrants you drew ("left", "up", "right" and "down").
The tilted line going from left bottom to top right is given by y=x. This means that (assuming y grows upwards and x grows rightwards) the points lying in quadrants "down" and "right" have coordinates satisfying y < x. Similarly points in quadrants "up" and "left" have coordinates satisfying y > x.
In order to differentiate between "down" and "right" and between "up" and "left" quadrants we can use the other line (from top left to bottom right) whose formula is y=-x. This time we see that points belonging to the "left" and "down" quadrants have coordinates obeying y < -x. Similarly, points belonging to the "right" and "up" quadrants have coordinates satisfying y > -x.
Combining these conditions we see that the object with coordinates x, y lies in:
These conditions assume that the point where all four quadrants meet is the origin of the coordinate system used to express x and y. You should perform the necessary translation using the known position of the eye before using these conditions.
Note that if you want to perform this process relative to a few eyes with different positions, you must use different translation each time. One unwanted consequence of this is that you may arrive at different quadrants for the same objects from two different eyes. This is a consequence of the problem and is independent of how you solve it.
You also need to make a choice regarding the classification of objects lying exactly on quadrants boundary. The choice will require you to change some of the strict inequalities above so that they allow for equality.
Upvotes: 5