Reputation: 597
I know the startpoint (the middle of the screen) and the angle (in my example 20°). Now I want to know the position on the edge of the screen, like an invisible line is drawn from the center to the edge in the given angle. For better explanation, I included an image:
Upvotes: 2
Views: 1232
Reputation: 24427
One way to do this is to calculate a point on a circle with a radius equal to or greater than the maximum diagonal, and then just clip it to the screen boundary.
Using Pythagoras' theorem, the length of the maximum diagonal will be
float d = Math.sqrt((width/2)*(width/2) + (height/2)*(height/2));
So you can calculate the point on the circle like this (angle is in radians clockwise from the top):
float x = Math.sin(angle) * d;
float y = -Math.cos(angle) * d;
Then you have to clip the vector from the origin to the point to each of the 4 sides, e.g for the right and left sides:
if(x > width/2)
{
float clipFraction = (width/2) / x; // amount to shorten the vector
x *= clipFraction;
y *= clipFraction;
}
else if(x < -width/2)
{
float clipFraction = (-width/2) / x; // amount to shorten the vector
x *= clipFraction;
y *= clipFraction;
}
Also do this for height/2 and -height/2. Then finally you can add width/2, height/2 to x and y to get the final position (with the center of the screen being width/2, height/2 not 0,0):
x += width/2
y += height/2
Upvotes: 5