This formula can be improved.
It is used in jumping to find point "p" on the screen's border (so when people jump in, they don't just appear, they look like they jump in).
[Only registered and activated users can see links. Click Here To Register...]
(TQ's coord axis is inverted)
[Only registered and activated users can see links. Click Here To Register...]
It is used in jumping to find point "p" on the screen's border (so when people jump in, they don't just appear, they look like they jump in).
[Only registered and activated users can see links. Click Here To Register...]
(TQ's coord axis is inverted)
[Only registered and activated users can see links. Click Here To Register...]
Code:
public static double GetAngle(ushort x, ushort y, ushort x2, ushort y2)
{
double xf1 = x, xf2 = x2, yf1 = y, yf2 = y2;
double result = 90 - Math.Atan((xf1 - xf2) / (yf1 - yf2)) * 180 / Math.PI;
if (xf1 - xf2 < 0 && yf1 - yf2 < 0)
result += 180;
else if (xf1 - xf2 == 0 && yf1 - yf2 < 0)
result += 180;
else if (yf1 - yf2 < 0 && xf1 - xf2 > 0)
result -= 180;
return result;
}
public class Vector { public ushort X, Y; }
public static Vector GetBorderCoords(ushort old_x, ushort old_y, ushort Target_x, ushort Target_y)
{
double Θ = GetAngle(old_x, old_y, Target_x, Target_y);
double w, h;
Vector v = new Vector();
byte quadrant = 1;
if (Θ < 0)
Θ += 360;
else if (Θ == 360)
Θ = 0;
while (Θ >= 90)
{
Θ -= 90;
quadrant++;
}
double screendistance = ScreenDistance;
if (quadrant == 1)
{
screendistance = ScreenDistance / (Math.Cos(Θ * Math.PI / 180));
if (screendistance > 25)
screendistance = ScreenDistance / (Math.Sin(Θ * Math.PI / 180));
else if (Θ != 0)
v.Y++;
h = screendistance * (Math.Sin(Θ * Math.PI / 180));
w = screendistance * (Math.Cos(Θ * Math.PI / 180));
v.X += (ushort)(Target_x + Math.Round(w));
if (Θ == 90)
v.Y += (ushort)(Target_y - Math.Round(h));
else
v.Y += (ushort)(Target_y + Math.Round(h));
}
else if (quadrant == 2)
{
screendistance = ScreenDistance / (Math.Cos(Θ * Math.PI / 180));
if (screendistance > 25)
{
screendistance = ScreenDistance / (Math.Sin(Θ * Math.PI / 180));
v.Y++;
}
w = screendistance * (Math.Sin(Θ * Math.PI / 180));
h = screendistance * (Math.Cos(Θ * Math.PI / 180));
v.X += (ushort)(Target_x - w);
v.Y += (ushort)(Target_y + h);
}
else if (quadrant == 3)
{
screendistance = ScreenDistance / (Math.Cos(Θ * Math.PI / 180));
if (screendistance > 25)
screendistance = ScreenDistance / (Math.Sin(Θ * Math.PI / 180));
h = screendistance * (Math.Sin(Θ * Math.PI / 180));
w = screendistance * (Math.Cos(Θ * Math.PI / 180));
v.X += (ushort)(Target_x - w);
v.Y += (ushort)(Target_y - h);
}
else if (quadrant == 4)
{
screendistance = ScreenDistance / (Math.Cos(Θ * Math.PI / 180));
if (screendistance > 25)
screendistance = ScreenDistance / (Math.Sin(Θ * Math.PI / 180));
else if (Θ > 0)
v.X++;
w = screendistance * (Math.Sin(Θ * Math.PI / 180));
h = screendistance * (Math.Cos(Θ * Math.PI / 180));
v.X += (ushort)(Target_x + w);
v.Y += (ushort)(Target_y - h);
}
return v;
}