Let's carefully analyze your problem step-by-step.
Problem Restatement:
- You have a regular polygon with:
- Number of sides ( n = 4 ) (a square)
- Side length ( s = 150 ) px
- Each interior angle ( = 90^\circ )
- The polygon is regularly oriented, such that one side faces exactly toward 90° (upward).
- You want to compute the distance from the center of this polygon along a given shooting direction (angle (\theta)) to the polygon's outline (boundary).
- Important facts/examples:
- At (\theta = 0^\circ), the distance to the outline is 75 px (apothem)
- At (\theta = 90^\circ), the distance is also 75 px (apothem)
- At (\theta = 45^\circ), the distance is 106.066... px (vertex)
You want the formula for the distance from center to polygon outline along any angle (\theta).
Step 1: Basic Polygon Properties
For a regular square of side length ( s ):
- Apothem ( a = \frac{s}{2} = 75 \text{ px} ) (distance from center to midpoint of any side, assuming square is oriented with sides aligned)
- The polygon radius (distance from center to vertex) ( R ) is:
[
R = \frac{s}{\sqrt{2}} = \frac{150}{\sqrt{2}} \approx 106.066 \text{ px}
]
which matches your 45° example.
Step 2: Orientation and Angles
- Polygon sides aligned so one side faces at (90^\circ), i.e., the square is oriented so its top side is horizontal at 90°.
- That means vertices are at:
[
\text{Vertices angles} = 45^\circ, 135^\circ, 225^\circ, 315^\circ
]
since vertex angles are centered between sides for a square.
- Sides face (0^\circ, 90^\circ, 180^\circ, 270^\circ)
Step 3: Problem Restatement in terms of Geometry
For shooting direction (\theta), find the distance (D) from the center to the polygon outline along that ray.
Step 4: Known General Formula for Distance from Center to Regular Polygon Outline on given angle (\theta)
For a regular polygon with:
- ( n ) sides
- radius (vertex distance) ( R )
- interior angle (\phi = \frac{2\pi}{n})
At arbitrary angle (\theta), the distance to polygon outline is:
[
D(\theta) = \frac{R \cos(\frac{\pi}{n})}{\cos\left(\left(\theta - \alpha \right) \bmod \frac{2\pi}{n} - \frac{\pi}{n}\right)}
]
Where:
- (\alpha) is the angle offset (rotation of polygon)
Step 5: Applying for your case: Square (4 sides)
- ( n = 4 )
- ( R = \frac{s}{\sin\frac{\pi}{n}} = \frac{150}{\sin(45^\circ)} = \frac{150}{\sqrt{2}/2} = 150 \times \frac{2}{\sqrt{2}} = 150 \sqrt{2} / 1 = 212.132 \text{ px} )
Wait — but directly from side length and vertex radius, this formula gives vertex radius. Let's double-check.
- For regular polygon of side length ( s ), radius is:
[
R = \frac{s}{2 \sin(\pi/n)}
]
For square ( n=4 ):
- (\sin(45^\circ) = \frac{\sqrt{2}}{2}),
- (R = \frac{150}{2 \times \sqrt{2} / 2} = \frac{150}{\sqrt{2}} \approx 106.066), which matches your vertex length.
I made a mistake in step 5 — the next formulas depend on this ( R = \frac{s}{2 \sin(\pi/n)} ).
[
a = \frac{s}{2 \tan(45^\circ)} = \frac{150}{2 \times 1} = 75
]
as expected.
Step 6: Actual distance along (\theta)
Regular polygon distance from center to outline along direction (\theta) can be expressed using:
[
D(\theta) = \frac{a}{\cos\left(\left(\theta - \alpha \right) \bmod \frac{2\pi}{n} - \frac{\pi}{n}\right)}
]
where:
- ( a ) is apothem (= \frac{s}{2 \tan(\pi/n)} )
- (\alpha) is polygon rotation to align side at angle (in radians)
- (\theta) is shooting angle (radians)
Step 7: Determine (\alpha) for your problem
You said polygon rotated so side at 90°, which means:
- One side aligns with 90°
- We can choose (\alpha = 90^\circ = \pi/2) radians
Step 8: Formula in degrees for your case
Let:
- ( n = 4 ),
- side length ( s = 150 ),
- ( a = \frac{s}{2 \tan(180^\circ / n)} = \frac{150}{2 \tan(45^\circ)} = 75 \text{ px} ),
- shooting angle: (\theta) degrees,
- polygon rotation (\alpha = 90^\circ)
Then,
[
D(\theta) = \frac{a}{\cos\left(\left( (\theta - \alpha) \bmod \frac{360^\circ}{n} \right) - \frac{180^\circ}{n} \right)}
]
Substituting ( n = 4 ):
[
D(\theta) = \frac{75}{\cos\left(\left( (\theta - 90) \bmod 90 \right) - 45 \right)}
]
Interpretation:
- Shift (\theta) by (-90^\circ) to align polygon side at 0°
- Take modulo 90° (sector angle)
- Subtract 45° (half sector)
- Calculate cosine of result, invert (divide apothem by this cosine) to get the outline distance.
Step 9: Verify with your examples
[
\text{Inside cosine} = ((90 - 90) \bmod 90) - 45 = (0) - 45 = -45^\circ
]
[
\cos(-45^\circ) = \frac{\sqrt{2}}{2} \approx 0.7071
]
[
D(90^\circ) = \frac{75}{0.7071} = 106.066...
]
Hm, this gives vertex distance at 90°, while you have 75 px at 90°.
Conflict here.
Step 10: Issue arises because in the formula you gave:
- 90° direction is apothem = 75 px, but formula gave vertex distance at 90°.
The formula above assumes side at 0°.
But you said "Polygon side toward 90°".
That means your polygon is rotated by 0° for side at 0°, rotated 90° for side at 90°.
Therefore, the cosine's argument is:
[
\left((\theta - \alpha) \bmod \frac{360}{n}\right) - \frac{180}{n}
]
When side at 90°, (\alpha = 0^\circ) (?)
No.
Let's try again.
Step 11: Realign assumptions.
- If side is towards 90°, rotate polygon by 90° (i.e. (\alpha = 90^\circ) in formula)
- Alternatively, rotate polygon zero degrees, then side at 0°
Compare outputs.
Step 12: Work through example numerically
Try (\theta = 0^\circ):
[
((0 - 90) \bmod 90) - 45 = ( -90 \bmod 90) - 45 = (0) - 45 = -45^\circ
]
[
D(0^\circ) = \frac{75}{\cos(-45^\circ)} = \frac{75}{0.7071} = 106.066
]
Your example says ( D(0^\circ) = 75 ).
Disagreement.
Step 13: Alternative approach: consider polygon rotated -90°
Try (\alpha = 0):
[
D(\theta) = \frac{75}{\cos\left(\left(\theta \bmod 90\right) - 45\right)}
]
At (\theta = 0^\circ):
[
(0 \bmod 90) - 45 = -45^\circ
\Rightarrow
D = \frac{75}{\cos(45^\circ)} = 106.066
]
Still getting vertex distance at 0°.
At (\theta = 45^\circ):
[
(45 \bmod 90) - 45 = 0
\Rightarrow
D = \frac{75}{\cos 0} = 75
]
At 45°, distance is 75 px. But your example says 106 px at 45°.
Conflict again.
Step 14: Correcting angle inside cosine
In past, the formula is often written as (for regular polygon distance from center along direction (\theta)):
[
D(\theta) = \frac{a}{\cos\left(\pi/n - (\theta - \alpha) \bmod (2\pi/n)\right)}
]
where ( a ) is apothem.
Convert degrees to radians:
- ( n=4 )
- ( \pi/n = 45^\circ )
- ( 2\pi/n = 90^\circ )
Try again with:
[
D(\theta) = \frac{a}{\cos\left(\frac{\pi}{n} - \left((\theta - \alpha) \bmod \frac{2\pi}{n}\right)\right)}
]
Step 15: Compute result with (\alpha=90^\circ)
At (\theta=0^\circ):
[
\cos(45^\circ - ((0 - 90) \bmod 90))
]
Note modulo for negative angles:
[
(0 - 90) \bmod 90 = (-90) \bmod 90 = 0
]
So:
[
\cos(45^\circ - 0) = \cos(45^\circ) = 0.7071
]
[
D(0^\circ) = \frac{75}{0.7071} = 106.066
]
At (\theta=45^\circ):
[
(45 - 90) \bmod 90 = (-45) \bmod 90 = 45
]
[
\cos(45^\circ - 45^\circ) = \cos 0 = 1
]
[
D(45^\circ) = \frac{75}{1} = 75
]
At (\theta=90^\circ):
[
(90 - 90) \bmod 90 = 0
]
[
\cos(45^\circ - 0) = 0.7071
]
[
D(90^\circ) = 106.066
]
Step 16: Compare to your given examples
- Your 0° = 75, 45°= 106, 90°=75
Result from above formula:
- (D(0)=106.066)
- (D(45)=75)
- (D(90)=106.066)
Swapped.
Step 17: Polygon rotation assumption switched
Try (\alpha=45^\circ):
At (\theta=0^\circ):
[
(0 - 45) \bmod 90 = 45
]
[
\cos(45^\circ - 45^\circ) = \cos 0 = 1
]
[
D=75
]
At (\theta=45^\circ):
[
(45 - 45) \bmod 90 = 0
]
[
\cos(45^\circ - 0) = 0.7071
]
[
D= \frac{75}{0.7071} =106.066
]
At (\theta=90^\circ):
[
(90 - 45) \bmod 90 = 45
]
[
\cos(45^\circ - 45^\circ) = 1
]
[
D=75
]
Matching your examples now!
You said:
With (\alpha=45^\circ), formula is:
[
D(\theta) = \frac{75}{\cos\left(45^\circ - ((\theta - 45^\circ) \bmod 90^\circ)\right)}
]
Summary:
- ( n = 4 )
- ( s = 150 )
- ( a = \frac{s}{2 \tan(180^\circ / n)} = 75 \text{ px} )
- Polygon rotation angle (\alpha = 45^\circ) to align one side at 90°
- Distance at shooting angle (\theta):
[
\boxed{
D(\theta) = \frac{75}{\cos\left(45^\circ - \left( (\theta - 45^\circ) \bmod 90^\circ \right) \right)}
}
]
How to apply:
- Compute difference angle: (\delta = (\theta - 45) \bmod 90)
- Subtract (\delta) from 45°
- Take cosine of result (in degrees)
- Divide 75 by cosine value to get distance in pixels.
Example:
[
\delta = (30 - 45) \bmod 90 = (-15) \bmod 90 = 75^\circ
]
[
45^\circ - 75^\circ = -30^\circ
]
[
D = \frac{75}{\cos(-30^\circ)} = \frac{75}{0.8660} = 86.6 \text{ px}
]
Final note:
- In radians, replace 45° with (\pi/4), 90° with (\pi/2).
- Can generalize to any regular polygon:
[
a = \frac{s}{2 \tan(\pi/n)}
]
[
D(\theta) = \frac{a}{\cos\left(\frac{\pi}{n} - ((\theta - \alpha) \bmod \frac{2\pi}{n})\right)}
]
Let me know if you want code or additional help!