PROG0370 - Compass

no tags 

A classic compass rose is divided into 360°. The four cardinal directions are on the main axes, and are respectively called north, east, south and west (see figure).

compass rose
Compass rose with subdivisions in degrees and mil (inner scale).

These cardinal directions cover areas of ±45° around the main axes (see tabel). They are simply used to indicate a well-defined direction anywhere in the world. North and south face their respective poles near the ends of the Earth axis. The rotation of the Earth around that axis is used to define the eastward and westward directions.

direction area
north [0°,45°[ and [315°,360°[
east [45°,135°[
south [135°,225°[
west [225°,315°[

Assignment

Define a class Compass that can be used to create instances of compasses. The needle of the compass gives the angle $a \in {\mathbb{Z}}$ (expressed in degrees), where $0 \leq a < 360$. The objects of the class Compass must at least support the following methods:

  • An initialization method that has an optional parameter that takes an angle $a \in {\mathbb{Z}}$ (expressed in degrees). If no angle is passed, it is assumed that the compass is aligned so that its needle points to the north.
  • A method __repr__ that returns a string representation of the object, corresponding to the Python expression that can be used to create a similar object. The string representation always needs to explicitly mention the angle $a \in {\mathbb{N}}$, and it must hold that $0 \leq a < 360$.
  • A method __str__ that returns a string representation of the object in the format a° (direction). In this, $a \in {\mathbb{N}}$ indicates the angle the compass points to, where $0 \leq a < 360$, and direction is the corresponding cardinal direction.
  • A method turn that takes an angle $a \in {\mathbb{Z}}$ (expressed in degrees). If this method is called, the compass must be rotated over the given angle.
  • A method direction that returns a string corresponding to the cardinal direction pointed to by the compass.

Example

>>> compass = Compass()
>>> compass
Compass(0)
>>> compass.direction()
'north'
>>> print(compass)
0° (north)e

>>> compass.turn(222)
>>> compass
Compass(222)
>>> compass.direction()
'south'
>>> print(compass)
222° (south)

>>> compass.turn(157)
>>> compass
Compass(19)
>>> compass.direction()
'north'
>>> print(compass)
19° (north)

Een klassieke kompasroos wordt onderverdeeld in 360°. De vier hoofdwindrichtingen staan op de hoofdassen, en worden respectievelijk noord, oost, zuid en west genoemd (zie figuur).

kompasroos
Kompasroos met verdeling in graden en mil (binnenste schaal).

Deze hoofdwindrichtingen beslaan gebieden van ±45° rond de hoofdassen (zie tabel). Ze worden simpelweg gebruikt om overal ter wereld een welbepaalde richting aan te geven. Noord en zuid zijn gericht naar hun respectievelijke polen nabij elk uiteinde van de aardas. De rotatie van de aarde rond die as wordt gebruikt om de richtingen oost en west te definiëren.

richting gebied
noord [0°,45°[ en [315°,360°[
oost [45°,135°[
zuid [135°,225°[
west [225°,315°[

Opgave

Definieer een klasse Kompas waarmee instanties van kompassen kunnen aangemaakt worden. De naald van een kompas geeft telkens een hoek $h \in {\mathbb{Z}}$ (uitgedrukt in graden) aan, waarbij geldt dat $0 \leq h < 360$. De objecten van de klasse Kompas moeten ondersteuning bieden aan de volgende methoden:

  • Een initialisatiemethode waaraan optioneel een hoek $h \in {\mathbb{Z}}$ (uitgedrukt in graden) kan doorgegeven worden. Indien geen hoek wordt doorgegeven, dan veronderstellen we dat het kompas naar het noorden wijst.
  • Een methode __repr__ die een stringvoorstelling van het object teruggeeft, die overeenkomt met de Python expressie die kan gebruikt worden om eenzelfde object aan te maken. Hierbij moet de hoek $h \in {\mathbb{N}}$ altijd expliciet opgegeven worden, en moet gelden dat $0 \leq h < 360$.
  • Een methode __str__ die een stringvoorstelling van het object teruggeeft in het formaat h° (richting). Hierbij geeft $h \in {\mathbb{N}}$ de hoek aan waarnaar het kompas wijst, waarbij $0 \leq h < 360$, en is richting de corresponderende hoofdwindrichting.
  • Een methode draai waaraan een hoek $h \in {\mathbb{Z}}$ (uitgedrukt in graden) moet doorgegeven worden. Indien deze methode wordt aangeroepen, dan moet het kompas over de opgegeven hoek gedraaid worden.
  • Een methode windrichting die een string teruggeeft die de hoofdwindrichting beschrijft waarnaar het kompas wijst.

Voorbeeld

>>> kompas = Kompas()
>>> kompas
Kompas(0)
>>> kompas.windrichting()
'noord'
>>> print(kompas)
0° (noord)

>>> kompas.draai(222)
>>> kompas
Kompas(222)
>>> kompas.windrichting()
'zuid'
>>> print(kompas)
222° (zuid)

>>> kompas.draai(157)
>>> kompas
Kompas(19)
>>> kompas.windrichting()
'noord'
>>> print(kompas)
19° (noord)


Added by:Peter Dawyndt
Date:2013-04-04
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None