{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Target Point"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting up Colab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colab_nb = 'google.colab' in str(get_ipython())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if colab_nb:\n",
    "  from google.colab import drive\n",
    "  drive.mount('/content/drive')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if colab_nb:\n",
    "  %cd /content/drive/My Drive/aad/code/tests/control"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You need to implement the function `get_target_point()` in `code/exercises/control/get_target_point.py`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First you can set `run_student_code = False` and see the sample solution at work. After that set `run_student_code = True` and see your implementation at work. It should behave like the sample solution!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "run_student_code = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import sys\n",
    "from pathlib import Path\n",
    "sys.path.append(str(Path('../../')))\n",
    "if run_student_code:\n",
    "    from exercises.control.get_target_point import get_target_point\n",
    "else:\n",
    "    from solutions.control.get_target_point import get_target_point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import interact\n",
    "\n",
    "def test_target_point(lookahead=5):\n",
    "    # create data\n",
    "    polyline = np.array([[1,1], [2,3], [3,6], [4,7]])\n",
    "    # plot data\n",
    "    fig, ax = plt.subplots()\n",
    "    ax.plot(polyline[:,0], polyline[:,1], color=\"g\")\n",
    "    circle = plt.Circle((0, 0), lookahead, color=\"k\", fill=False)\n",
    "    ax.add_artist(circle)\n",
    "    # get function output and plot it\n",
    "    intersec = get_target_point(lookahead, polyline)\n",
    "    if intersec is not None:\n",
    "        plt.scatter([intersec[0]], [intersec[1]], color=\"r\")\n",
    "    plt.axis(\"equal\")\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "interact(test_target_point, lookahead=(0,10,0.1));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import interact\n",
    "\n",
    "def test_geometry(lookahead=5):\n",
    "    # create data\n",
    "    polyline = np.array([[-4,-7],[-3,-6],[-2,-3],[-1,-1],[1,1], [2,3], [3,6], [4,7]])\n",
    "    # plot data\n",
    "    fig, ax = plt.subplots()\n",
    "    ax.plot(polyline[:,0], polyline[:,1], color=\"g\")\n",
    "    circle = plt.Circle((0, 0), lookahead, color=\"k\", fill=False)\n",
    "    ax.add_artist(circle)\n",
    "    # get function output and plot it\n",
    "    intersec = get_target_point(lookahead, polyline)\n",
    "    if intersec is not None:\n",
    "        plt.scatter([intersec[0]], [intersec[1]], color=\"r\")\n",
    "    plt.axis(\"equal\")\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "interact(test_geometry, lookahead=(0,10,0.1));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
