{"id":26,"date":"2014-10-13T08:47:13","date_gmt":"2014-10-13T12:47:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/graphics\/?p=26"},"modified":"2014-10-13T08:57:08","modified_gmt":"2014-10-13T12:57:08","slug":"bezier-curves","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/graphics\/2014\/10\/13\/bezier-curves\/","title":{"rendered":"B\u00e9zier Curves"},"content":{"rendered":"<div class=\"content\"><h3>B&eacute;zier Curves and Kronecker's Tensor Product<\/h3><p>Last time we talked about Martin Newell's famous teapot. Today we're going to talk about the curves which the teapot is made of. These are known as <a href=\"http:\/\/en.wikipedia.org\/wiki\/B%C3%A9zier_curve\">B&eacute;zier curves<\/a>. These are extremely useful curves, and you'll encounter them in lots of different places in computer graphics.<\/p><p>Let's look at how to draw a B&eacute;zier curve. We'll start by drawing a line the hard way. We'll start with these two points.<\/p><pre class=\"codeinput\">pt1 = [ 5;-10];\r\npt2 = [45; 15];\r\n<\/pre><p>We'll use this simple function to label the points.<\/p><pre class=\"codeinput\">type <span class=\"string\">placelabel<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction placelabel(pt,str)\r\n    x = pt(1);\r\n    y = pt(2);\r\n    h = line(x,y);\r\n    h.Marker = '.';\r\n    h = text(x,y,str);\r\n    h.HorizontalAlignment = 'center';\r\n    h.VerticalAlignment = 'bottom';\r\n    \r\n<\/pre><p>And we'll use 'axis equal' to make the scaling uniform in the X &amp; Y directions.<\/p><pre class=\"codeinput\">placelabel(pt1,<span class=\"string\">'pt_1'<\/span>);\r\nplacelabel(pt2,<span class=\"string\">'pt_2'<\/span>);\r\nxlim([0 50])\r\naxis <span class=\"string\">equal<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_01.png\" alt=\"\"> <p>Now we want to draw the line segment between them. We can think of this line as being a linear combination of the two points.<\/p><p>$$0&lt;=t&lt;=1$$<\/p><p>$$pt(t) = (1-t)*pt_1 + t*pt_2$$<\/p><p>When $t$ is equal to 0, we get:<\/p><p>$$pt(0) = (1-0)*pt_1 + 0*pt_2$$<\/p><p>which is equal to $pt1$. When $t$ is equal to 1, we get:<\/p><p>$$pt(1) = (1-1)*pt_1 + 1*pt_2$$<\/p><p>which is equal to $pt2$. When $t$ is equal to 1\/2, we get:<\/p><p>$$pt(1\/2) = (1-1\/2)*pt_1 + 1\/2*pt_2$$<\/p><p>which is the midpoint of the line segment. And indeed, for any value of $t$ between 0 and 1, we get a point on the line.<\/p><p>There are a couple of ways to implement this in MATLAB, but we'll do it the following way. The <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/kron.html\">kron<\/a> function will give us the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Kronecker_product\">Kronecker tensor product<\/a> of two arrays. This means that it will give us all of the possible products of the elements in those two arrays. So, for example, if we create an array of t values like this:<\/p><pre class=\"codeinput\">t = linspace(0,1,101);\r\n<\/pre><p>We can use kron to multiply all of them by the two coordinate values in each point like this:<\/p><pre class=\"codeinput\">pts = kron((1-t),pt1) + kron(t,pt2);\r\n<\/pre><p>And then we can plot them.<\/p><pre class=\"codeinput\">hold <span class=\"string\">on<\/span>\r\nplot(pts(1,:),pts(2,:))\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_02.png\" alt=\"\"> <p>and voila! We have drawn the line between the points.<\/p><p>So why do we want to do this? Wouldn't it be easier to just use the line command?<\/p><pre class=\"codeinput\">cla\r\nline([pt1(1), pt2(1)],[pt1(2), pt2(2)])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_03.png\" alt=\"\"> <p>The reason is that we're going to expand this to more points and higher-order functions of $t$.  Instead of linear combinations of two points, we're going to create higher-order combinations of three or four points.<\/p><p>We'll start with the quadratic B&eacute;zier curve. This curve takes three points.<\/p><pre class=\"codeinput\">pt1 = [ 5;-10];\r\npt2 = [18; 18];\r\npt3 = [45; 15];\r\n\r\ncla\r\nplacelabel(pt1,<span class=\"string\">'pt_1'<\/span>);\r\nplacelabel(pt2,<span class=\"string\">'pt_2'<\/span>);\r\nplacelabel(pt3,<span class=\"string\">'pt_3'<\/span>);\r\nxlim([0 50])\r\naxis <span class=\"string\">equal<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_04.png\" alt=\"\"> <p>And we've still got $t$ going from 0 to 1, but we'll use second-order polynomials like this:<\/p><p>$$pt(t) = (1-t)^2*pt_1 + 2 (1-t) t*pt_2 + t^2*pt_3$$<\/p><p>These are known as the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bernstein_polynomial\">Bernstein basis polynomials<\/a>.<\/p><p>We can use the kron function to evaluate them, just as we did in the linear case.<\/p><pre class=\"codeinput\">pts = kron((1-t).^2,pt1) + kron(2*(1-t).*t,pt2) + kron(t.^2,pt3);\r\n\r\nhold <span class=\"string\">on<\/span>\r\nplot(pts(1,:),pts(2,:))\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_05.png\" alt=\"\"> <p>Notice that the resulting curve starts at $pt1$ and ends at $pt3$. In between, it moves towards, but doesn't reach, $pt2$.<\/p><p>Next we can go up to the cubic B&eacute;zier. This is the one which the teapot uses. We'll need 4 points ...<\/p><pre class=\"codeinput\">pt1 = [ 5;-10];\r\npt2 = [18; 18];\r\npt3 = [38; -5];\r\npt4 = [45; 15];\r\n\r\ncla\r\nplacelabel(pt1,<span class=\"string\">'pt_1'<\/span>);\r\nplacelabel(pt2,<span class=\"string\">'pt_2'<\/span>);\r\nplacelabel(pt3,<span class=\"string\">'pt_3'<\/span>);\r\nplacelabel(pt4,<span class=\"string\">'pt_4'<\/span>);\r\nxlim([0 50])\r\naxis <span class=\"string\">equal<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_06.png\" alt=\"\"> <p>... and the following third-order equation:<\/p><p>$$pt(t) = (1-t)^3*pt_1 + 3 (1-t)^2 t*pt_2 + 3 (1-t) t^2*pt_3 + t^3*pt_4$$<\/p><pre class=\"codeinput\">pts = kron((1-t).^3,pt1) + kron(3*(1-t).^2.*t,pt2) + kron(3*(1-t).*t.^2,pt3) + kron(t.^3,pt4);\r\n\r\nhold <span class=\"string\">on<\/span>\r\nplot(pts(1,:),pts(2,:))\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_07.png\" alt=\"\"> <p>Again we can see that the curve starts at the first point and ends at the last point, while moving towards each intermediate point in turn.<\/p><p>Also, notice the pattern of the coefficients of the polynomials in these three examples.<\/p><div><ul><li>Order 1 - [1 1]<\/li><li>Order 2 - [1 2 1]<\/li><li>Order 3 - [1 3 3 1]<\/li><\/ul><\/div><p>Do you recognize them? They're our old friends the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Binomial_coefficient\">binomial coefficients<\/a>. You can follow this pattern and create higher order B&eacute;zier curves with more points.<\/p><p>So why do computer graphics programmers love B&eacute;zier curves? Because they have a lot of useful properties. They're based on those simple polynomials, and we know how to do all sorts of things with polynomials like these.<\/p><p>For example, we can calculate the first derivative with respect to $t$.<\/p><pre class=\"codeinput\">a = -3*t.^2 +  6*t - 3;\r\nb =  9*t.^2 - 12*t + 3;\r\nc = -9*t.^2 +  6*t;\r\nd =  3*t.^2;\r\n\r\ntvec = kron(a,pt1) + kron(b,pt2) + kron(c,pt3) + kron(d,pt4);\r\n<\/pre><p>This gives us the tangent vector at any point on the curve. We can add these to our plot with hold on, but we won't draw all of them because it gets too crowded.<\/p><pre class=\"codeinput\"><span class=\"keyword\">for<\/span> i=1:10:101\r\n    l = line([pts(1,i), pts(1,i)+tvec(1,i)\/6], <span class=\"keyword\">...<\/span>\r\n             [pts(2,i), pts(2,i)+tvec(2,i)\/6]);\r\n    l.Color = <span class=\"string\">'green'<\/span>;\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_08.png\" alt=\"\"> <p>If you look at the values of those derivatives, you'll see an interesting pattern. When $t$ equals 0, the four terms are [-3, 3, 0, 0]. That means that the tangent of the start of the curve is simply $3pt_2 - 3pt_1$. The same thing happens at the other end when $t$ equals 1.<\/p><p>This makes it easy to connect two B&eacute;zier curves so that they are tangent at the meeting point. You just make the last two points of the first curve colinear with the first two points of the second curve.<\/p><pre class=\"codeinput\">cla\r\nxlim([0 50])\r\naxis <span class=\"string\">equal<\/span>\r\n\r\npt1 = [ 5;-10];\r\npt2 = [13; -2];\r\npt3 = [ 5; 20];\r\npt4 = [25; 20];\r\n\r\nplacelabel(pt1,<span class=\"string\">'pt_1'<\/span>);\r\nplacelabel(pt2,<span class=\"string\">'pt_2'<\/span>);\r\nplacelabel(pt3,<span class=\"string\">'pt_3'<\/span>);\r\nplacelabel(pt4,<span class=\"string\">'pt_4'<\/span>);\r\n\r\npt5 = [45; 20];\r\npt6 = [35; -2];\r\npt7 = [43;-10];\r\n\r\nplacelabel(pt5,<span class=\"string\">'pt_5'<\/span>);\r\nplacelabel(pt6,<span class=\"string\">'pt_6'<\/span>);\r\nplacelabel(pt7,<span class=\"string\">'pt_7'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_09.png\" alt=\"\"> <p>Notice that $pt3$, $pt4$, and $pt5$ are colinear:<\/p><pre class=\"codeinput\">pt4 - pt3\r\npt5 - pt4\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n    20\r\n     0\r\n\r\n\r\nans =\r\n\r\n    20\r\n     0\r\n\r\n<\/pre><p>And if we plot the curves, we can see that we get a nice, smooth join where they meet.<\/p><pre class=\"codeinput\">pts1 = kron((1-t).^3,pt1) + kron(3*(1-t).^2.*t,pt2) + kron(3*(1-t).*t.^2,pt3) + kron(t.^3,pt4);\r\npts2 = kron((1-t).^3,pt4) + kron(3*(1-t).^2.*t,pt5) + kron(3*(1-t).*t.^2,pt6) + kron(t.^3,pt7);\r\n\r\nhold <span class=\"string\">on<\/span>\r\nplot(pts1(1,:),pts1(2,:))\r\nplot(pts2(1,:),pts2(2,:))\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/bezierpost_10.png\" alt=\"\"> <p>You can find a list of other useful properties of the B&eacute;zier curve at this <a href=\"http:\/\/en.wikipedia.org\/wiki\/B%C3%A9zier_curve#Properties\">Wikipedia page<\/a>.<\/p><p>The teapot is actually made of B&eacute;zier patches. These are parametric surfaces which are made from the same math as B&eacute;zier curves, but with two parameters (usually called $u$ and $v$) instead of the one parameter (i.e. $t$) that we've been using here. We'll come back to investigate B&eacute;zier patches in a future post, but first we're going to explore how to draw some other useful curves.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_1f7bbf6ce91f48cab832065852df497d() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='1f7bbf6ce91f48cab832065852df497d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1f7bbf6ce91f48cab832065852df497d';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_1f7bbf6ce91f48cab832065852df497d()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014b<br><\/p><\/div><!--\r\n1f7bbf6ce91f48cab832065852df497d ##### SOURCE BEGIN #####\r\n%% B\u00c3\u00a9zier Curves and Kronecker's Tensor Product\r\n% Last time we talked about Martin Newell's famous teapot. Today we're \r\n% going to talk about the curves which the teapot is made of. These are \r\n% known as <http:\/\/en.wikipedia.org\/wiki\/B%C3%A9zier_curve B\u00c3\u00a9zier curves>.\r\n% These are extremely useful curves, and you'll encounter them in lots of \r\n% different places in computer graphics.\r\n%\r\n% Let's look at how to draw a B\u00c3\u00a9zier curve. We'll start by drawing a line \r\n% the hard way. We'll start with these two points.\r\npt1 = [ 5;-10];\r\npt2 = [45; 15];\r\n\r\n%%\r\n% We'll use this simple function to label the points.\r\ntype placelabel\r\n\r\n%%\r\n% And we'll use 'axis equal' to make the scaling uniform in the X & Y \r\n% directions.\r\nplacelabel(pt1,'pt_1');\r\nplacelabel(pt2,'pt_2');\r\nxlim([0 50])\r\naxis equal\r\n\r\n%%\r\n% Now we want to draw the line segment between them. We can think of this \r\n% line as being a linear combination of the two points. \r\n%\r\n% $$0<=t<=1$$\r\n%\r\n% $$pt(t) = (1-t)*pt_1 + t*pt_2$$\r\n%\r\n% When $t$ is equal to 0, we get:\r\n%\r\n% $$pt(0) = (1-0)*pt_1 + 0*pt_2$$\r\n%\r\n% which is equal to $pt1$. When $t$ is equal to 1, we get:\r\n%\r\n% $$pt(1) = (1-1)*pt_1 + 1*pt_2$$\r\n%\r\n% which is equal to $pt2$. When $t$ is equal to 1\/2, we get:\r\n%\r\n% $$pt(1\/2) = (1-1\/2)*pt_1 + 1\/2*pt_2$$\r\n%\r\n% which is the midpoint of the line segment. And indeed, for any value of $t$\r\n% between 0 and 1, we get a point on the line. \r\n%\r\n% There are a couple of ways to implement this in MATLAB, but we'll do it \r\n% the following way. The <https:\/\/www.mathworks.com\/help\/matlab\/ref\/kron.html kron> function will give us the <http:\/\/en.wikipedia.org\/wiki\/Kronecker_product Kronecker tensor \r\n% product> of two arrays. This means that it will give us all of the\r\n% possible products of the elements in those two arrays. So, for example, \r\n% if we create an array of t values like this:\r\nt = linspace(0,1,101);\r\n\r\n%%\r\n% We can use kron to multiply all of them by the two coordinate values in \r\n% each point like this:\r\npts = kron((1-t),pt1) + kron(t,pt2);\r\n\r\n%%\r\n% And then we can plot them.\r\nhold on\r\nplot(pts(1,:),pts(2,:))\r\nhold off\r\n\r\n%% \r\n% and voila! We have drawn the line between the points.\r\n%\r\n% So why do we want to do this? Wouldn't it be easier to just use the line\r\n% command?\r\ncla\r\nline([pt1(1), pt2(1)],[pt1(2), pt2(2)])\r\n\r\n%%\r\n% The reason is that we're going to expand this to more points and\r\n% higher-order functions of $t$.  Instead of linear combinations of two \r\n% points, we're going to create higher-order combinations of three or four \r\n% points.\r\n%\r\n% We'll start with the quadratic B\u00c3\u00a9zier curve. This curve takes three points.\r\n%\r\npt1 = [ 5;-10];\r\npt2 = [18; 18];\r\npt3 = [45; 15];\r\n\r\ncla\r\nplacelabel(pt1,'pt_1');\r\nplacelabel(pt2,'pt_2');\r\nplacelabel(pt3,'pt_3');\r\nxlim([0 50])\r\naxis equal\r\n\r\n%%\r\n% And we've still got $t$ going from 0 to 1, but we'll use second-order\r\n% polynomials like this:\r\n%\r\n% $$pt(t) = (1-t)^2*pt_1 + 2 (1-t) t*pt_2 + t^2*pt_3$$\r\n%\r\n% These are known as the <http:\/\/en.wikipedia.org\/wiki\/Bernstein_polynomial\r\n% Bernstein basis polynomials>.\r\n%\r\n% We can use the kron function to evaluate them, just as we did in the \r\n% linear case.\r\npts = kron((1-t).^2,pt1) + kron(2*(1-t).*t,pt2) + kron(t.^2,pt3);\r\n\r\nhold on\r\nplot(pts(1,:),pts(2,:))\r\nhold off\r\n\r\n%%\r\n% Notice that the resulting curve starts at $pt1$ and ends at $pt3$. In\r\n% between, it moves towards, but doesn't reach, $pt2$. \r\n\r\n\r\n%%\r\n% Next we can go up to the cubic B\u00c3\u00a9zier. This is the one which the teapot \r\n% uses. We'll need 4 points ...\r\npt1 = [ 5;-10];\r\npt2 = [18; 18];\r\npt3 = [38; -5];\r\npt4 = [45; 15];\r\n\r\ncla\r\nplacelabel(pt1,'pt_1');\r\nplacelabel(pt2,'pt_2');\r\nplacelabel(pt3,'pt_3');\r\nplacelabel(pt4,'pt_4');\r\nxlim([0 50])\r\naxis equal\r\n\r\n%%\r\n% ... and the following third-order equation:\r\n%\r\n% $$pt(t) = (1-t)^3*pt_1 + 3 (1-t)^2 t*pt_2 + 3 (1-t) t^2*pt_3 + t^3*pt_4$$\r\n%\r\npts = kron((1-t).^3,pt1) + kron(3*(1-t).^2.*t,pt2) + kron(3*(1-t).*t.^2,pt3) + kron(t.^3,pt4);\r\n\r\nhold on\r\nplot(pts(1,:),pts(2,:))\r\nhold off\r\n\r\n%%\r\n% Again we can see that the curve starts at the first point and ends at the\r\n% last point, while moving towards each intermediate point in turn.\r\n%\r\n% Also, notice the pattern of the coefficients of the polynomials in these \r\n% three examples. \r\n%\r\n% * Order 1 - [1 1]\r\n% * Order 2 - [1 2 1]\r\n% * Order 3 - [1 3 3 1]\r\n%\r\n% Do you recognize them? They're our old friends the\r\n% <http:\/\/en.wikipedia.org\/wiki\/Binomial_coefficient binomial\r\n% coefficients>. You can follow this pattern and create higher order \r\n% B\u00c3\u00a9zier curves with more points.\r\n\r\n%%\r\n% So why do computer graphics programmers love B\u00c3\u00a9zier curves? Because they \r\n% have a lot of useful properties. They're based on those simple polynomials, \r\n% and we know how to do all sorts of things with polynomials like that. \r\n%\r\n% For example, we can calculate the first derivative with respect to $t$.\r\n\r\na = -3*t.^2 +  6*t - 3;\r\nb =  9*t.^2 - 12*t + 3;\r\nc = -9*t.^2 +  6*t;\r\nd =  3*t.^2;\r\n\r\ntvec = kron(a,pt1) + kron(b,pt2) + kron(c,pt3) + kron(d,pt4);\r\n\r\n%%\r\n% This gives us the tangent vector at any point on the curve. We can these\r\n% to our plot with hold on, but we won't draw all of them because it gets\r\n% too crowded.\r\nfor i=1:10:101\r\n    l = line([pts(1,i), pts(1,i)+tvec(1,i)\/6], ... \r\n             [pts(2,i), pts(2,i)+tvec(2,i)\/6]);\r\n    l.Color = 'green';\r\nend\r\n\r\n%%\r\n% If you look at the values of those derivatives, you'll see an interesting\r\n% pattern. When $t$ equals 0, the four terms are [-3, 3, 0, 0]. That means \r\n% that the tangent of the start of the curve is simply $3pt_2 - 3pt_1$. The\r\n% same thing happens at the other end when $t$ equals 1. \r\n%\r\n% This makes it easy to connect two B\u00c3\u00a9zier curves so that they tangent at \r\n% the meeting point. You just make the last two points of the first curve \r\n% colinear with the first two points of the second curve.\r\n%\r\ncla\r\nxlim([0 50])\r\naxis equal\r\n\r\npt1 = [ 5;-10];\r\npt2 = [13; -2];\r\npt3 = [ 5; 20];\r\npt4 = [25; 20];\r\n\r\nplacelabel(pt1,'pt_1');\r\nplacelabel(pt2,'pt_2');\r\nplacelabel(pt3,'pt_3');\r\nplacelabel(pt4,'pt_4');\r\n\r\npt5 = [45; 20];\r\npt6 = [35; -2];\r\npt7 = [43;-10];\r\n\r\nplacelabel(pt5,'pt_5');\r\nplacelabel(pt6,'pt_6');\r\nplacelabel(pt7,'pt_7');\r\n\r\n%%\r\n% Notice that $pt3$, $pt4$, and $pt5$ are colinear:\r\npt4 - pt3\r\npt5 - pt4\r\n\r\n%%\r\n% And if we plot the curves, we can see that we get a nice, smooth join \r\n% where they meet.\r\n\r\npts1 = kron((1-t).^3,pt1) + kron(3*(1-t).^2.*t,pt2) + kron(3*(1-t).*t.^2,pt3) + kron(t.^3,pt4);\r\npts2 = kron((1-t).^3,pt4) + kron(3*(1-t).^2.*t,pt5) + kron(3*(1-t).*t.^2,pt6) + kron(t.^3,pt7);\r\n\r\nhold on\r\nplot(pts1(1,:),pts1(2,:))\r\nplot(pts2(1,:),pts2(2,:))\r\nhold off\r\n\r\n%%\r\n% You can find a list of other useful properties of the B\u00c3\u00a9zier curve at this \r\n% <http:\/\/en.wikipedia.org\/wiki\/B%C3%A9zier_curve#Properties Wikipedia\r\n% page>.\r\n%\r\n% The teapot is actually made of B\u00c3\u00a9zier patches. These are parametric surfaces \r\n% which are made from the same math as B\u00c3\u00a9zier curves, but with two\r\n% parameters (usually called $u$ and $v$) instead of the one parameter (i.e. $t$)\r\n% that we've been using here. We'll come back to investigate B\u00c3\u00a9zier patches\r\n% in a future post, but first we're going to explore how to draw some other\r\n% useful curves.\r\n##### SOURCE END ##### 1f7bbf6ce91f48cab832065852df497d\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/feature_image\/bezierpost_10.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>B&eacute;zier Curves and Kronecker's Tensor ProductLast time we talked about Martin Newell's famous teapot. Today we're going to talk about the curves which the teapot is made of. These are known as... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/graphics\/2014\/10\/13\/bezier-curves\/\">read more >><\/a><\/p>","protected":false},"author":89,"featured_media":34,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/26"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/users\/89"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":12,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/26\/revisions\/40"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/media\/34"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}