What you learn today
- Angles from ratios
- atan2 with two values
- Quadrants
Yesterday went from angle to ratio: sin, cos, tan. Today goes back. The question, for example: the ore field lies 30 m east and 40 m north – in which direction?
Angles from ratios
| Instruction | takes | returns (radians) |
|---|---|---|
asin r a | a sine value −1 … 1 | −π/2 … π/2 |
acos r a | a cosine value −1 … 1 | 0 … π |
atan r a | any ratio | −π/2 … π/2 |
All of them return radians. For degrees follow up with mul r r rad2deg.
asin a 0.5 # 0.5236 = 30 degrees
mul a a rad2deg # 30
atan2 with two values
atan only sees the ratio y/x. Whether the target lies north-east (x and y positive) or south-west (both negative) gives the same ratio – atan cannot tell them apart. atan2 gets both values separately, y first, then x:
atan2 angle y x # -pi ... pi
mul angle angle rad2deg # -180 ... 180
Quadrants
Counting is mathematical: 0° is east (positive x axis), 90° north, counter-clockwise.
| x | y | atan2 in degrees |
|---|---|---|
| + | + | 0 … 90 |
| − | + | 90 … 180 |
| − | − | −180 … −90 |
| + | − | −90 … 0 |
Negative angles are awkward on a display. mod helps: on the chip mod is always positive (explicitly not like % in other languages), so −135 becomes exactly 225 with mod a a 360.
asin and acos only take values from −1 to 1. Even 1.0000001 gives NaN – and that easily happens through rounding, for example when you compute a cosine from readings. Do clamp r r -1 1 first, then the value is safe.
You get the distance to the same target right away: Pythagoras with mul, add and sqrt from week 21.
Exercise
Task
Two logic memories “Ost” and “Nord” contain how far a target lies east (x) and north (y) of the base.
- Display “Winkel”: direction in degrees, 0 to below 360 (0° = east, 90° = north)
- Display “Entfernung”: distance to the target
- If a memory is missing, both show −1
Hints
Hint 1
atan2 angle y x – y first!
Hint 2
Then mul angle angle rad2deg and mod angle angle FULLCIRCLE against negative angles.
Hint 3
Distance: x², y², add, sqrt.
Quiz
4 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in