{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2f24dc84",
   "metadata": {},
   "source": [
    "# 06-03 · Меняем направление\n",
    "\n",
    "Практика к разделу [«Заставляем черепашку менять направление»](../../site/chapters/glava-06/06-03-povorot-cherepashki.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abce54e5",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить абсолютное направление: `setheading()`, `heading()`, `home()`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abcce0fe",
   "metadata": {},
   "source": [
    "## Настройка (выполнить один раз)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "61d26288",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:26.543445Z",
     "iopub.status.busy": "2026-08-14T20:52:26.543242Z",
     "iopub.status.idle": "2026-08-14T20:52:26.720418Z",
     "shell.execute_reply": "2026-08-14T20:52:26.720094Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Окно Turtle готово.\n"
     ]
    }
   ],
   "source": [
    "import turtle\n",
    "\n",
    "screen = turtle.Screen()\n",
    "artist = turtle.Turtle()\n",
    "artist.speed(0)\n",
    "print(\"Окно Turtle готово.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c319e26f",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "26ffd6a3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:26.721712Z",
     "iopub.status.busy": "2026-08-14T20:52:26.721592Z",
     "iopub.status.idle": "2026-08-14T20:52:27.181110Z",
     "shell.execute_reply": "2026-08-14T20:52:27.180595Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Курс: 180.0\n"
     ]
    }
   ],
   "source": [
    "artist.reset()\n",
    "artist.setheading(90)\n",
    "artist.forward(100)\n",
    "artist.setheading(180)\n",
    "artist.forward(100)\n",
    "print(\"Курс:\", artist.heading())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "190ee6bf",
   "metadata": {},
   "source": [
    "## Эксперимент 1\n",
    "\n",
    "Проверьте курс для всех четырёх сторон света: 0°, 90°, 180°, 270°."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8f1cbb56",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:27.182091Z",
     "iopub.status.busy": "2026-08-14T20:52:27.181973Z",
     "iopub.status.idle": "2026-08-14T20:52:27.850449Z",
     "shell.execute_reply": "2026-08-14T20:52:27.849698Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Курс 0° -> позиция (60.00,0.00)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Курс 90° -> позиция (60.00,60.00)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Курс 180° -> позиция (0.00,60.00)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Курс 270° -> позиция (-0.00,0.00)\n"
     ]
    }
   ],
   "source": [
    "artist.reset()\n",
    "for ugol in [0, 90, 180, 270]:\n",
    "    artist.setheading(ugol)\n",
    "    artist.forward(60)\n",
    "    print(f\"Курс {ugol}° -> позиция {artist.position()}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2a147dc",
   "metadata": {},
   "source": [
    "## Эксперимент 2\n",
    "\n",
    "`home()` возвращает черепашку в исходную точку и исходный курс одной командой."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0bf67ebd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:27.851698Z",
     "iopub.status.busy": "2026-08-14T20:52:27.851576Z",
     "iopub.status.idle": "2026-08-14T20:52:28.208065Z",
     "shell.execute_reply": "2026-08-14T20:52:28.207444Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "До home(): (70.71,70.71) 45.0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "После home(): (0.00,0.00) 0.0\n"
     ]
    }
   ],
   "source": [
    "artist.reset()\n",
    "artist.setheading(45)\n",
    "artist.forward(100)\n",
    "print(\"До home():\", artist.position(), artist.heading())\n",
    "\n",
    "artist.home()\n",
    "print(\"После home():\", artist.position(), artist.heading())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bb01dc5",
   "metadata": {},
   "source": [
    "## Типичная ошибка\n",
    "\n",
    "`left()`/`right()` меняют курс *относительно текущего*, а `setheading()` — задаёт курс *абсолютно*. Если перепутать, фигура получится не той, что ожидалось."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5a837b03",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:28.209586Z",
     "iopub.status.busy": "2026-08-14T20:52:28.209400Z",
     "iopub.status.idle": "2026-08-14T20:52:28.481792Z",
     "shell.execute_reply": "2026-08-14T20:52:28.481360Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Итоговый курс: 180.0 (вероятно, не то, что ожидали)\n"
     ]
    }
   ],
   "source": [
    "artist.reset()\n",
    "artist.setheading(90)   # смотрим вверх\n",
    "artist.left(90)         # ОТНОСИТЕЛЬНЫЙ поворот от 90 -> 180, а не setheading(90)!\n",
    "print(\"Итоговый курс:\", artist.heading(), \"(вероятно, не то, что ожидали)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bf31491",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Нарисуйте крест (плюс) из четырёх лучей, выходящих из центра, используя `setheading()` для каждого луча: 0°, 90°, 180°, 270°."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5c372337",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:28.483013Z",
     "iopub.status.busy": "2026-08-14T20:52:28.482895Z",
     "iopub.status.idle": "2026-08-14T20:52:29.482362Z",
     "shell.execute_reply": "2026-08-14T20:52:29.481921Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Готово, финальная позиция: (0.00,0.00)\n"
     ]
    }
   ],
   "source": [
    "artist.reset()\n",
    "for ugol in [0, 90, 180, 270]:\n",
    "    artist.setheading(ugol)\n",
    "    artist.forward(80)\n",
    "    artist.backward(80)\n",
    "print(\"Готово, финальная позиция:\", artist.position())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e3afd9f",
   "metadata": {},
   "source": [
    "## Завершение (выполнить один раз, в самом конце)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0017e970",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:29.483387Z",
     "iopub.status.busy": "2026-08-14T20:52:29.483266Z",
     "iopub.status.idle": "2026-08-14T20:52:29.486470Z",
     "shell.execute_reply": "2026-08-14T20:52:29.486050Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Окно Turtle закрыто.\n"
     ]
    }
   ],
   "source": [
    "screen.bye()\n",
    "print(\"Окно Turtle закрыто.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
