Let's clarify your problem and compute the distance (shot length) from the center of the polygon to its outline (edge or vertex) along a given direction (angle).
Given:
- Polygon is a square (4 sides), side length ( s = 150 )px.
- Polygon is regular, oriented such that one side is facing exactly 90° (up).
- Angles (directions) are measured, with 0° presumably pointing right (standard Cartesian coordinate system).
- Central cannon fires a shot in a direction (\theta = 100°).
- You want to calculate (d), the shortest distance from polygon center to the polygon outline (which could be along an edge or toward a vertex) in that direction.
Understanding your example values:
-
For the square with side = 150 px:
- The apothem ( a = \frac{s}{2} = 75) px. (half-length from center to edge along axis)
-
At 0° (right) or 90° (up): ray hits midpoint of side, distance = 75 px
-
At 45° (toward vertex), distance is ~106.07 px (diagonal vertex distance)
Step 1: Polygon geometry setup
Because polygon is a square, centered at origin:
- Vertices (relative to center) at (assuming one side facing 90°, so top side flat horizontally):
Vertices:
- Top-right: (75, 75)
- Top-left: (-75, 75)
- Bottom-left: (-75, -75)
- Bottom-right: (75, -75)
Wait, the side length is 150 px, so half-side is 75 px.
So the square vertices:
[
V_1 = (75, 75), \quad
V_2 = (-75, 75), \quad
V_3 = (-75, -75), \quad
V_4 = (75, -75)
]
Sides:
- top edge: from (V_2) to (V_1)
- left edge: from (V_3) to (V_2)
- bottom edge: from (V_4) to (V_3)
- right edge: from (V_1) to (V_4)
Step 2: Ray direction vector from center
Given angle (\theta = 100°):
Convert angle to radians:
[
\theta_r = 100^\circ \times \frac{\pi}{180} = \frac{5\pi}{9} \approx 1.745 \text{ rad}
]
Direction vector unit:
[
\mathbf{d} = (\cos \theta_r, \sin \theta_r) = (\cos 100^\circ, \sin 100^\circ)
]
Calculate numerical:
[
\cos 100^\circ \approx -0.1736, \quad \sin 100^\circ \approx 0.9848
]
Step 3: Find intersection of ray ( \mathbf{r}(t) = t \mathbf{d} ), (t \ge 0) with polygon edges
Each edge is a segment ( \mathbf{p}(u) = \mathbf{p_0} + u \mathbf{v} ), (
u \in [0,1]
),
Where:
- (\mathbf{p_0}) is start vertex of edge
- (\mathbf{v}) is vector from start vertex to end vertex
For each edge, solve for (t,u) such that:
[
t \mathbf{d} = \mathbf{p_0} + u \mathbf{v}
]
Rewrite:
[
t \mathbf{d} - u \mathbf{v} = \mathbf{p_0}
]
Which is a linear system two unknowns ((t,u)).
Write in components:
[
t d_x - u v_x = p_{0x}
]
[
t d_y - u v_y = p_{0y}
]
Step 4: Solve linear system for each edge
Rearranged as:
[
\begin{bmatrix}
d_x & -v_x \
d_y & -v_y
\end{bmatrix}
\begin{bmatrix}
t \
u
\end{bmatrix}
\begin{bmatrix}
p_{0x} \
p_{0y}
\end{bmatrix}
]
Denote the matrix as (M), vector unknowns as (\mathbf{x} = (t, u)^T), RHS as (\mathbf{p}_0).
Use Cramer's rule or inverse matrix:
[
\mathbf{x} = M^{-1} \mathbf{p}_0
]
Check if (t \ge 0) (ray direction) and (0 \le u \le 1) (on edge segment).
For all edges that satisfy this, pick the minimal positive (t).
Step 5: Calculate for 100°
Let's do this edge by edge.
Edge 1: top edge (V_2 \to V_1)
- ( \mathbf{p_0} = (-75, 75) )
- ( \mathbf{v} = V_1 - V_2 = (75 - (-75), 75 - 75) = (150, 0) )
Matrix
[
M =
\begin{bmatrix}
d_x & -v_x \
d_y & -v_y
\end{bmatrix}
\begin{bmatrix}
-0.1736 & -150 \
0.9848 & 0
\end{bmatrix}
]
Solve:
[
M \begin{bmatrix} t \ u \end{bmatrix} = \begin{bmatrix} -75 \ 75 \end{bmatrix}
]
Determinant:
[
\det M = d_x \times (-v_y) - d_y \times (-v_x) = (-0.1736)(0) - 0.9848 \times (-150) = 0 + 147.72 = 147.72 \neq 0
]
Invert (M):
[
M^{-1} = \frac{1}{\det M}
\begin{bmatrix}
-v_y & v_x \
-d_y & d_x
\end{bmatrix}
\frac{1}{147.72}
\begin{bmatrix}
0 & 150 \
-0.9848 & -0.1736
\end{bmatrix}
]
Multiply:
[
\begin{bmatrix} t \ u \end{bmatrix} = M^{-1} \mathbf{p}_0 = \frac{1}{147.72}
\begin{bmatrix}
0 \times (-75) + 150 \times 75 \
-0.9848 \times (-75) + (-0.1736) \times 75
\end{bmatrix}
\frac{1}{147.72}
\begin{bmatrix}
11250 \
73.86 - 13.02
\end{bmatrix}
\frac{1}{147.72}
\begin{bmatrix}
11250 \
60.84
\end{bmatrix}
]
Calculate:
[
t = \frac{11250}{147.72} \approx 76.15
]
[
u = \frac{60.84}{147.72} \approx 0.412
]
Check constraints:
( t \ge 0 )? Yes, 76.15 > 0
( 0 \le u \le 1)? Yes, 0.412 is within edge segment
So edge 1 is intersected at (t = 76.15).
Edge 2: left edge (V_3 \to V_2)
- ( \mathbf{p_0} = (-75, -75) )
- ( \mathbf{v} = (-75 - (-75), 75 - (-75)) = (0, 150) )
Matrix:
[
M =
\begin{bmatrix}
-0.1736 & 0 \
0.9848 & -150
\end{bmatrix}
]
Determinant:
[
\det M = d_x \times (-v_y) - d_y \times (-v_x) = -0.1736 \times (-150) - 0.9848 \times 0 = 26.04 > 0
]
Inverse:
[
M^{-1} = \frac{1}{26.04}
\begin{bmatrix}
-150 & 0 \
-0.9848 & -0.1736
\end{bmatrix}
]
Multiply:
[
M^{-1} \mathbf{p}_0 =
\frac{1}{26.04}
\begin{bmatrix}
-150 \times (-75) + 0 \times (-75) \
-0.9848 \times (-75) + (-0.1736) \times (-75)
\end{bmatrix}
\frac{1}{26.04}
\begin{bmatrix}
11250 \
73.86 + 13.02
\end{bmatrix}
\frac{1}{26.04}
\begin{bmatrix}
11250 \
86.88
\end{bmatrix}
]
Calculate:
[
t = \frac{11250}{26.04} \approx 432
]
[
u = \frac{86.88}{26.04} \approx 3.3
]
(t \ge 0)? Yes.
(u \in [0,1])? No. It's 3.3 > 1, so intersection point lies outside segment.
Disregard edge 2.
Edge 3: bottom edge (V_4 \to V_3)
- (\mathbf{p_0} = (75, -75))
- (\mathbf{v} = (-75 - 75, -75 - (-75)) = (-150, 0))
Matrix:
[
M =
\begin{bmatrix}
-0.1736 & 150 \
0.9848 & 0
\end{bmatrix}
]
Determinant:
[
\det M = d_x (-v_y) - d_y (-v_x) = (-0.1736)(0) - 0.9848 \times (-150) = 147.72 > 0
]
Inverse:
[
M^{-1} = \frac{1}{147.72}
\begin{bmatrix}
0 & -150 \
-0.9848 & -0.1736
\end{bmatrix}
]
Multiply:
[
M^{-1} \mathbf{p}_0 =
\frac{1}{147.72}
\begin{bmatrix}
0 \times 75 + (-150) \times (-75) \
-0.9848 \times 75 + (-0.1736) \times (-75)
\end{bmatrix}
\frac{1}{147.72}
\begin{bmatrix}
11250 \
-73.86 + 13.02
\end{bmatrix}
\frac{1}{147.72}
\begin{bmatrix}
11250 \
-60.84
\end{bmatrix}
]
Calculate:
[
t = 76.15
]
[
u = \frac{-60.84}{147.72} = -0.412
]
Check (u\in[0,1])? No, -0.412 < 0.
Disregard edge 3.
Edge 4: right edge (V_1 \to V_4)
- (\mathbf{p_0} = (75, 75))
- (\mathbf{v} = (75 - 75, -75 - 75) = (0, -150))
Matrix:
[
M =
\begin{bmatrix}
-0.1736 & 0 \
0.9848 & 150
\end{bmatrix}
]
Determinant:
[
\det M = (-0.1736)(-150) - 0.9848 \times 0 = 26.04 > 0
]
Inverse:
[
M^{-1} = \frac{1}{26.04}
\begin{bmatrix}
150 & 0 \
-0.9848 & -0.1736
\end{bmatrix}
]
Multiply:
[
M^{-1} \mathbf{p}_0 =
\frac{1}{26.04}
\begin{bmatrix}
150 \times 75 + 0 \times 75 \
-0.9848 \times 75 + (-0.1736) \times 75
\end{bmatrix}
\frac{1}{26.04}
\begin{bmatrix}
11250 \
-73.86 - 13.02
\end{bmatrix}
\frac{1}{26.04}
\begin{bmatrix}
11250 \
-86.88
\end{bmatrix}
]
Calculate:
[
t = 432
]
[
u = \frac{-86.88}{26.04} = -3.3
]
(u \in [0,1])? No. Disregard edge 4.
Step 6: Result
The only suitable intersection is with edge 1 (top edge), at distance (t = 76.15) px.
Final answer:
[
\boxed{
\text{Distance to polygon outline along } 100^\circ \approx 76.15 \text{ px}
}
]
Summary procedure:
- For each polygon edge with start point (\mathbf{p_0}) and vector (\mathbf{v}),
-
Solve
[
\begin{bmatrix}
d_x & -v_x \
d_y & -v_y
\end{bmatrix}
\begin{bmatrix}
t \
u
\end{bmatrix}
\begin{bmatrix}
p_{0x} \
p_{0y}
\end{bmatrix}
]
- Check if (t \ge 0) and (0 \le u \le 1).
- Take minimum (t) passing above check.
- (t) is distance to polygon outline along angle (\theta).
If you want, I can provide code (e.g. Python) that does this calculation for arbitrary polygon angles and sides.