{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise for Object Orientation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the definition of a `Point` class for handling points in the 3-dimensional space $R^3$. \n", "Objects from this class should have a\n", "* a method `show` to display the coordinates of the point\n", "* a method `move` to move a point \n", "* a method `dist` that computes the distance between 2 points\n", "* a method `dotprod` that computes the scalar product of 2 points\n", "* a method `crossrod` that computes the scalar product of 2 points\n", "* a method `norm` that returns the norm of the point\n", "* a method `normalize` that makes the points norm equal 1\n", "* a method `scale` that scales the point by scalar alpha \n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [], "source": [ "\n", "\n", "class Point(object):\n", " \"\"\"Class to handle point in R^3\"\"\"\n", "\n", " def __init__(self, x, y, z):\n", " \"\"\"\n", " :param x,y,z: coordinates\n", " :type x,y,z: float\n", " \"\"\"\n", " self.x = x\n", " self.y = y\n", " self.z = z\n", "\n", " def show(self):\n", " \"\"\"\n", " :return: the coordinate of this point\n", " :rtype: a tuple of 3 elements (float, float,float)\n", " \"\"\"\n", " return self.x, self.y, self.z\n", "\n", " # ... TODO \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check your Implementation on some simple examples" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3)\n", "(4, 5, 6)\n", "(5, 7, 9)\n", "12.449899597988733\n", "1.0\n", "(-0.24096579867075008, 0.4819315973414997, -0.24096579867074963)\n", "(0, 0, 0)\n" ] } ], "source": [ "p1 = Point(1,2,3)\n", "p2 = Point(4,5,6)\n", "\n", "print ( p1.show() )\n", "print ( p2.show() )\n", "\n", "p1.move(p2)\n", "print ( p1.show() )\n", "\n", "print(p1.norm())\n", "p1.normalize()\n", "print(p1.norm())\n", "\n", "\n", "Point.dotprod(p1,p1)\n", "n=Point.crossprod(p1,p2)\n", "print(n.show())\n", "\n", "n=Point.crossprod(p2,p2)\n", "print(n.show())\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geometry in $R^3$\n", "\n", "Now we want to do some geometry in $R^3$. We define classes for a line and a plane, based on our point class with:\n", "\n", "Line:\n", " * a init method to define the line by starting point x0 and direction vector v\n", " \n", "Plane: \n", " * a init method to define the plane by starting point x0 and direction vectors v,w\n", " * a normal method to return the normal of the plane (normalized)\n", " * a line_intersection method which returns the intersection with a given line g " ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ "class Line(object):\n", " \"\"\"Class to handle lines in R^3\"\"\"\n", " \n", " # ... TODO\n", " \n", " \n", "class Plane(object): \n", " \"\"\"Class to handle planes in R^3\"\"\"\n", " \n", " # ... TODO\n", " \n", " \n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do some tests with the Plane and Line class, check the values." ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(-0.408248290463863, 0.816496580927726, -0.408248290463863)\n" ] } ], "source": [ "x0=Point(1,1,1)\n", "v =Point(1,2,1)\n", "g=Line(x0,v)\n", "\n", "x0=Point(1,2,3)\n", "v =Point(4,5,6)\n", "w =Point(7,8,9)\n", "E =Plane(x0,v,w) \n", "\n", "n=E.normal()\n", "print (n.show())\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Intersection plane-line\n", "Now go ahead to our Plane class and implement a method\n", "\n", "* line_intersection(self,g)\n", "\n", "which returns the intersection point of the plane with line g. Then check it with the following calls:" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1. -4. -7.]\n", " [ 2. -5. -8.]\n", " [ 1. -6. -9.]]\n", "6.000000000000005\n", "(1.0, 1.0, 1.0)\n" ] } ], "source": [ "s=E.line_intersection(g)\n", "print(s.show())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Intersection plane - plane\n", "Now again go ahead to our Plane class and implement a method\n", "\n", " plane_intersection(self,E)\n", "\n", "which returns the intersection line of the plane with another plane E. \n", "\n", "This can be done by having the planes in normal form:\n", "$$\n", "E_1: n_1 \\cdot x + d_1=0 \\\\\n", "E_2: n_2 \\cdot x + d_2=0 \\\\\n", "$$\n", "where $\\cdot$ is the scalar product and $n_1$,$n_2$ the normal vectors of the planes.\n", "\n", "So we have $2$ equations for $3$ unknowns $x_1,x_2,x_3$. \n", "\n", "Lets add the condition:\n", "$$\n", "(n_1 \\times n_2) \\cdot x = 0\n", "$$\n", "with $\\times$ as the cross product in R^3, which leads to $3$ equations for $3$ unknowns.\n", "\n", "The solution $x$ is a point on the intersection line, because it fulfills both plane equations.\n", "\n", "Moreover $(n_1 \\times n_2)$ is a vector whicht is orthogonal to both normal vectors of the planes,\n", "so it is parallel to both planes and a direction vector of the intersection line.\n", "\n", "\n", "Then check it with the following calls:\n" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 0. 1.]\n", " [ 0. -1. 0.]\n", " [ 1. -0. -0.]]\n", "1.0\n", "(0.0, -0.0, 0.0)\n", "(1.0, -0.0, -0.0)\n" ] } ], "source": [ "# x-y plane\n", "x0=Point(0,0,0)\n", "v =Point(1,0,0)\n", "w =Point(0,1,0)\n", "E =Plane(x0,v,w) \n", "\n", "# x-z plane\n", "x0=Point(0,0,0)\n", "v =Point(1,0,0)\n", "w =Point(0,0,1)\n", "E2 =Plane(x0,v,w) \n", "\n", "# should be the x-axis \n", "r=E.plane_intersection(E2)\n", "print(r.x0.show())\n", "print(r.v.show())\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As the last task for today compute the intersection line of the planes:\n", "$$\n", "E1: \\quad \\left[ \\begin{matrix} x \\\\ y \\\\ z \\end{matrix} \\right]\n", "= \\left[ \\begin{matrix} 1 \\\\ 4 \\\\ 3 \\end{matrix} \\right]\n", "+ \\lambda_1 \\left[ \\begin{matrix} 0 \\\\ 2 \\\\ 1 \\end{matrix} \\right]\n", "+ \\eta_1 \\left[ \\begin{matrix} -1 \\\\ 4 \\\\ 0 \\end{matrix} \\right]\n", "$$\n", "\n", "\n", "$$\n", "E2: \\quad \\left[ \\begin{matrix} x \\\\ y \\\\ z \\end{matrix} \\right]\n", "= \\left[ \\begin{matrix} 6 \\\\ 4 \\\\ 2 \\end{matrix} \\right]\n", "+ \\lambda_2 \\left[ \\begin{matrix} 3 \\\\ 3 \\\\ 2 \\end{matrix} \\right]\n", "+ \\eta_2 \\left[ \\begin{matrix} 2 \\\\ 1 \\\\ -1 \\end{matrix} \\right]\n", "$$\n", "\n", "Because one can use different starting points and different scaled direction vectors for a line your\n", "solution will not have the same numbers like the solution \n", "\n", "$$\n", "g: \\quad \\left[ \\begin{matrix} x \\\\ y \\\\ z \\end{matrix} \\right]\n", "= \\left[ \\begin{matrix} 1 \\\\ 0 \\\\ 1 \\end{matrix} \\right]\n", "+ t \\left[ \\begin{matrix} 1 \\\\ 2 \\\\ 3 \\end{matrix} \\right]\n", "$$\n", "\n", "Implement a test which say if your solution is the line g from above or not." ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.87287156 -0.21821789 0.43643578]\n", " [-0.5488213 0.76834982 -0.32929278]\n", " [-0.26347778 -0.52695556 -0.79043333]]\n", "0.971887550200803\n", "(0.7142857142857145, -0.571428571428572, 0.14285714285714315)\n", "(-0.26347777762091695, -0.5269555552418339, -0.7904333328627509)\n", "||d||=0.000000 ... looks good \n" ] } ], "source": [ "x0=Point(1,4,3)\n", "v =Point(0,2,1)\n", "w =Point(-1,4,0)\n", "E1 =Plane(x0,v,w) \n", "\n", "x0=Point(6,4,2)\n", "v =Point(3,3,2)\n", "w =Point(2,1,-1)\n", "E2 =Plane(x0,v,w) \n", "\n", "r=E1.plane_intersection(E2)\n", "print(r.x0.show())\n", "print(r.v.show())\n", "\n", "def samelines(g1,g2):\n", " \n", " # TODO\n", " \n", " if ( ... ):\n", " print(\" ... looks good \")\n", " else:\n", " print(\" ... looks bad \")\n", " \n", " \n", " \n", "\n", "x0=Point(1,0,1)\n", "v=Point(1,2,3)\n", "gs=Line(x0,v)\n", "\n", "samelines(gs,r)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6.9" } }, "nbformat": 4, "nbformat_minor": 4 }