{ "cells": [ { "cell_type": "markdown", "id": "002431be", "metadata": {}, "source": [ "# Defining a Piece of a Bezier Curve" ] }, { "cell_type": "markdown", "id": "6e4d6cfc", "metadata": {}, "source": [ "A piece of a Bezier curve is a cubic plane curve, defined by $(x(t), y(t))$ two polynomials of degree 3 in one parameter $t$." ] }, { "cell_type": "code", "execution_count": null, "id": "6ec83bc7", "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "On input are two arrays x and y,\n", "with the coordinates of the four points\n", "that define a piece of a Bezier curve.\n", "On return are the coefficients of the\n", "two cubics that define the piece.\n", "\"\"\"\n", "function Bezier(x,y)\n", " bx = 3*(x[2]-x[1])\n", " by = 3*(y[2]-y[1])\n", " cx = 3*(x[3]-x[2]) - bx\n", " cy = 3*(y[3]-y[2]) - by\n", " dx = x[4] - x[1] - bx - cx\n", " dy = y[4] - y[1] - by - cy\n", " xt = [x[1], bx, cx, dx]\n", " yt = [y[1], by, cy, dy]\n", " return (xt, yt)\n", "end" ] }, { "cell_type": "markdown", "id": "e7a03f14", "metadata": {}, "source": [ "We will define one piece that starts at (1,1) with control (1,3), ends at (2,2), with control (3,3). The four x-values are 1, 1, 3, 2. The four y-values are 1, 3, 3, 2. The coordinates of the control points are the middle values. The function piece takes two arrays of 4 numbers on input and returns the two arrays, with the four coefficients of x(t) and y(t) of the piece of the Bezier curve as specified by the input." ] }, { "cell_type": "code", "execution_count": null, "id": "574a84af", "metadata": {}, "outputs": [], "source": [ "a = [1, 1, 3, 2]; b = [1, 3, 3, 2];\n", "cffx, cffy = Bezier(a,b)\n", "println(\"coefficients for x(t) \", cffx)\n", "println(\"coefficients for y(t) \", cffy)" ] }, { "cell_type": "code", "execution_count": null, "id": "e9c3bc44", "metadata": {}, "outputs": [], "source": [ "startpoint = (evalpoly(0, cffx), evalpoly(0,cffx))\n", "endpoint = (evalpoly(1,cffx), evalpoly(1,cffy))\n", "print(\"The curve starts at \", startpoint)\n", "println(\" and ends at \", endpoint, \".\")" ] }, { "cell_type": "markdown", "id": "f590b38b", "metadata": {}, "source": [ "# Plotting a Plane Curve" ] }, { "cell_type": "markdown", "id": "36ecbe8d", "metadata": {}, "source": [ "To define the curve $(x(t), y(t))$, we copy the coefficients in ``cffx`` and ``cffy`` in the definition of the coordinate functions ``x(t)`` and ``y(t)``." ] }, { "cell_type": "code", "execution_count": null, "id": "6583e3d7", "metadata": {}, "outputs": [], "source": [ "x(t) = 1 + 6*t^2 - 5*t^3\n", "y(t) = 1 + 6*t - 6*t^2 + t^3\n", "print(\"The curve starts at (\", x(0), \",\", y(0), \")\")\n", "print(\" and ends at (\", x(1), \",\", y(1), \")\")" ] }, { "cell_type": "markdown", "id": "a2815c84", "metadata": {}, "source": [ "In making the plot, observe that this is different from plotting y as a function of x. The plane curve is defined by two functions ``x(t)`` and ``y(t)``." ] }, { "cell_type": "code", "execution_count": null, "id": "50b327af", "metadata": {}, "outputs": [], "source": [ "using Plots" ] }, { "cell_type": "markdown", "id": "634d0240", "metadata": {}, "source": [ "To plot, we have to define a range where we sample the functions." ] }, { "cell_type": "code", "execution_count": null, "id": "23c2cfc2", "metadata": {}, "outputs": [], "source": [ "r = 0:0.01:1\n", "xr = [x(t) for t in r];\n", "yr = [y(t) for t in r];" ] }, { "cell_type": "markdown", "id": "7487b8ae", "metadata": {}, "source": [ "We define the limits of the range for x and y from 0 to 3. To see the same spacings in both directions, we set the aspect ratio to one. The legend is removed." ] }, { "cell_type": "code", "execution_count": null, "id": "e9821346", "metadata": { "scrolled": false }, "outputs": [], "source": [ "plot(xr,yr,xlims=(0,3),ylims=(0,3), aspect_ratio=1, leg=false)" ] }, { "cell_type": "code", "execution_count": null, "id": "8d28fae2", "metadata": {}, "outputs": [], "source": [ "# savefig(\"bezierplot.png\")" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.8.0", "language": "julia", "name": "julia-1.8" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.8.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 5 }