← Back to challenges

Cannon Fire

PythonHardtrigonometrymath

Instructions

A cannon sits in an open field ready to fire. You know the location of the cannon in cartesian coordinates. You know the direction that the barrel is pointed as a clockwise bearing from the north. You also know the distance that the cannonball will travel.

Devise a function that finds the coordinates of the spot where the cannonball strikes its target. Round to the nearest integer.

cannon(location, angle, distance) ➞ target location

A clockwise bearing from the north simply means that at 0 degrees the cannon is pointing north, at 90 degrees it is pointing east, at 180 degrees it is pointing south, and at 270 degrees it is pointing west.

Examples

cannon((0, 0), 0, 10) ➞ (0, 10)

cannon((0, 0), 270, 10) ➞ (-10, 0)

cannon((0, 0), 45, 10) ➞ (7, 7)

cannon((-12, -2), 193, 9) ➞ (-14, -11)

Notes

The distance here is the length along the ground that the cannonball travels, not its arc above ground, so treat this as a plane trigonometry problem.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.