4
$\begingroup$

I am coding a video game, but I am not so good at the math. I am hoping for some help here:

Given:

  • $X, Y$ that is the center of the Circle
  • $R$ that is the radius of the Circle
  • $X_1, Y_1$ that may or may not be in the circle.

The idea is that I have a tower that will shoot at a bad guy when it comes in range. The bad guy has an $X_1, Y_1$ coordinate that will continually update.

I need an equation I can run to see if the bad guy is in range.

3 Answers 3

11

$(X-X_1)^2+(Y-Y_1)^2\leq R^2$ is true if and only if the bad guy is in range, since the distance between $(X,Y)$ and $(X_1,Y_1)$ is $\sqrt{(X-X_1)^2+(Y-Y_1)^2}$.

5

The circle is defined by all points $(x_1, y_1)$ satisfying $(x_1-x)^{2} + (y_1-y)^{2} \le R^{2}$. That is your logical condition to test if someone is in the circle.

0

You need to check the distance $d\leq \sqrt(Xp-Xc)^2+(Yp-Yc)^2$ from the center to the bad guy and since it has square root it's simpler to test $d^2\leq (Xp-Xc)^2+(Yp-Yc)^2\leq R^2$

$Xc$ and $Yc$ are 0 since they are in the center of circle the bad guy is in range if $(Xp)^2+(Yp)^2\leq R^2$

In some Java code it's look like this

public static Point[] internalPoints(Point[] points, double radius) {      int countPoints = 0;     for (int i = 0; i < points.length; i++) {         double xp = points[i].getX();         double yp = points[i].getY();         // points are inside the circle if d^2 <= r^2         // d^2 = (Xp-Xc)^2 + (Yp-Yc)^2         // Xp and Yp is the point that should be checked         // Xc and Xc is the point center (orgin)         // Xc and Yc are 0 you end up with d^2 = (Xp)^2 + (Yp)^2         if (xp * xp + yp * yp <= radius * radius) {             countPoints++;         }     }     int companionVar = 0;     Point[] pointsInside = new Point[countPoints];     for (int j = 0; j < countPoints; j++) {         pointsInside[companionVar] = points[j];         companionVar++;     }     return pointsInside;  }