{"id":402,"date":"2023-10-31T07:30:53","date_gmt":"2023-10-31T11:30:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/?p=402"},"modified":"2023-10-31T09:11:22","modified_gmt":"2023-10-31T13:11:22","slug":"gourds-to-graphics-the-matlab-pumpkin","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2023\/10\/31\/gourds-to-graphics-the-matlab-pumpkin\/","title":{"rendered":"Gourds to Graphics: The MATLAB Pumpkin"},"content":{"rendered":"<div class=\"rtcContent\">\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\"><span style=\"font-weight: bold\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft wp-image-405\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/EricPortrait.png\" alt=\"\" width=\"100\" height=\"100\" \/><br \/>\n<\/span><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<p><span style=\"font-weight: bold\">Guest Writer: <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869244\">Eric Ludlam<\/a><br \/>\n<\/span><\/p>\n<div class=\"rtcContent\">\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">\n<p>Today's graphics adventure is written by Eric Ludlam, development manager of MATLAB's Charting team. Eric has a whopping 27 years of experience working in MATLAB Graphics, starting in MATLAB 5.0! Eric is renowned within MathWorks for his love of trebuchets, catapults and <a href=\"https:\/\/blogs.mathworks.com\/community\/2021\/10\/26\/flying-pumpkins-dancing-pumpkins\/\"><em>Punkin Chunkin<\/em><\/a>. His exceptional graphics demos have appeared in MathWorks' Community blog, MATLAB blog, and in MATLAB social media feeds. Today he answers the collective question we all have. <span style=\"font-style: italic\">How'd you do that?<\/span><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<hr \/>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">The MATLAB social media team has been sharing images and code for different pumpkins around Halloween time for the past couple years, and these gourds have been pretty popular. One question asked in the comments was how the pumpkin was designed. Today I'd like to explain the types of decisions made while developing the code and share some fun tricks with vectorization and implicit expansion for anyone just learning how to write efficient code in MATLAB.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">The pumpkin I'd like to use today is a speckled yellow and orange pumpkin I picked up locally here in Massachusetts. Here's a picture of our target pumpkin:<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\"><img decoding=\"async\" loading=\"lazy\" class=\"imageNode\" style=\"vertical-align: baseline;width: 427px;height: 308px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_1.png\" alt=\"speckled_pumpkin.jpg\" width=\"427\" height=\"308\" \/><\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">There are a few interesting features of this pumpkin, including a stubby little stem, 2 sets of creases\/bumps, a 2-tone color scheme where the 2nd color is in the deeper crease, and speckles. A bunch of fun challenges!<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Before we start let's review the various components of our algorithm. We can break the overall shape of our pumpkin into 2 major parts, the semi-spherical pumpkin part, and the stem.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">The algorithm for the pumpkin part is made of 3 key components which will be combined together into one set of matrices drawn with the <span style=\"font-family: monospace\">surf<\/span> command.<\/div>\n<ul style=\"margin: 10px 0px 20px;padding-left: 0px;font-family: Helvetica, Arial, sans-serif;font-size: 14px\">\n<li style=\"margin-left: 56px;line-height: 21px;min-height: 0px;text-align: left\">Unit Sphere of the underlying shape<\/li>\n<li style=\"margin-left: 56px;line-height: 21px;min-height: 0px;text-align: left\">Creases and bumps as seen around the circumference<\/li>\n<li style=\"margin-left: 56px;line-height: 21px;min-height: 0px;text-align: left\">Dimple on the top and bottom<\/li>\n<\/ul>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">I'll start describing how to make the patterns for creases, bumps, and the dimple for the pumpkin, then move to how the stem is made before combining it all at the end.<\/div>\n<h2 style=\"margin: 20px 10px 5px 4px;padding: 0px;line-height: 20px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 20px;font-weight: bold;text-align: left\">Computing Creases and Bumps<\/h2>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">In this article, I'll use \"crease\" to describe an indentation that travels from the stem to the bottom of the pumpkin. I'll use the term \"bump\" to generally refer to one pattern that runs from crease-to-crease around the circumference.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">There is one set of creases that are deeper (and more orange) than the other. I'll call a deep crease the 'primary' crease, and the other is the 'secondary' crease. Since these creases are mapped onto a sphere, there are the same number of bumps as there are creases. To develop a crease\/bump pattern we need a way to create a repeating pattern numerically. To do this we need to set up a few parameters, including how many bumps there are, and how many vertices we'd like in our graphic.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">I count around 10 creases, with secondary crease between each one. We also want to select enough vertices per bump to provide a smooth look across the pumpkin. I tried a few different values for vertices per bump and found that 10 looks good while still being a small number when multiplied out against the total number of bumps. We don't want too many vertices because this number also impacts the size of the speckles which we'll see at the end, and our target pumpkin has big spots.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">We can compute our total number of vertices from the total number of bumps, and use that size for the combined set. The +1 for <span style=\"font-family: monospace\">numVerts<\/span> is because the first and last vertex around the sphere are shared, which will align the creases with a vertex so they look sharp.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">numPrimaryBumps = 10;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">totalBumps = numPrimaryBumps*2; <span style=\"color: #008013\">% Add in-between creases.<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">vertsPerBump = 10;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px 0px 4px 4px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">numVerts = totalBumps*vertsPerBump+1;<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Next, we need to compute the depth of the creases for both the primary and secondary creases. We'll specify the depth in terms of a unit sphere. To create our pattern vector we'll start with a linear list of values and use <span style=\"font-family: monospace\">mod(x, 2)<\/span> to create the repeating pattern which means we start with values from <span style=\"font-family: monospace\">0<\/span> to the number of <span style=\"font-family: monospace\">bumps*2<\/span>.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">rPrimary = linspace(0,numPrimaryBumps*2,numVerts);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px 0px 4px 4px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">rSecondary = linspace(0,totalBumps*2,numVerts);<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Next, we'll define some depths for the creases. The depth of the primary crease will be the sum of <span style=\"font-family: monospace\">crease_depth<\/span> and <span style=\"font-family: monospace\">crease_depth2<\/span>, so we need to keep that in mind while selecting depth values.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">The bump vectors <span style=\"font-family: monospace\">Rxy_primary<\/span> and <span style=\"font-family: monospace\">Rxy_secondary<\/span> represent the change in radius of our unit sphere in the xy plane. <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mod.html\">mod<\/a> is used to create a repeating pattern between <span style=\"font-family: monospace\">[ 0 2 ]<\/span>. That pattern needs to be shifted to <span style=\"font-family: monospace\">[ -1 1 ]<\/span> so that when the result is squared, it creates a curve.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">crease_depth = .04;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">crease_depth2 = .01;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Rxy_primary = 0 - (1-mod(rPrimary,2)).^2*crease_depth;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Rxy_secondary = 0 - (1-mod(rSecondary,2)).^2*crease_depth2;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Rxy = Rxy_primary + Rxy_secondary;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px 0px 4px 4px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Let's plot the result in polar coordinates so we can get a feel for what it will look like. When plotting, add 1 to the radius to simulate how we'll be adding the pumpkin's core diameter to this crease pattern.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">polarplot(Rxy+1, <span style=\"color: #a709f5\">'LineWidth'<\/span>,2);<\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">rlim([0 1.1])<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_0\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_2.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">The secondary bump looks shallow in this diagram, but that is roughly what I found on the real pumpkin.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Speaking of which, let's take a quick look at what we have so far by multiplying <span style=\"font-family: monospace\">Rxy<\/span> to the <span style=\"font-family: monospace\">X<\/span> &amp; <span style=\"font-family: monospace\">Y<\/span> coordinates of a sphere.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">[ Xsphere, Ysphere, Zsphere ] = sphere(numVerts-1); <span style=\"color: #008013\">% Sphere creates +1 verts<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Xpunkin = (1+Rxy).*Xsphere;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Ypunkin = (1+Rxy).*Ysphere;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">surf(Xpunkin,Ypunkin,Zsphere)<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">shading <span style=\"color: #a709f5\">interp<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">daspect([1 1 1])<\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">camlight<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_1\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_3.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Well, this looks more like a ridgy sports ball than a pumpkin. That's because our target pumpkin has a dimple in the top and bottom.<\/div>\n<h2 style=\"margin: 20px 10px 5px 4px;padding: 0px;line-height: 20px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 20px;font-weight: bold;text-align: left\">Computing a pumpkin dimple<\/h2>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">The dimple on a pumpkin is caused by how the fruit expands off the end of the stem as it grows. We'll use an exponential function to create the dimple as we did for the creases, but across only a single span between -1 and 1. Note also that I'm creating a column vector. That will let us apply the shape to the columns of our sphere matrix to define its vertical profile instead of applying it to the rows which would affect the bump pattern.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">To see how it looks we'll plot this in polar coordinates as well, but this time I'm only showing half the polar chart since I'm computing a single profile.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">dimple = .2; <span style=\"color: #008013\">% Fraction to dimple into top\/bottom<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">rho = linspace(-1,1,numVerts)';<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Rz_dimple = (0-rho.^4)*dimple;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">polarplot(linspace(0,pi,numVerts),Rz_dimple+1, <span style=\"color: #a709f5\">'LineWidth'<\/span>,2);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">rlim([0 1.1])<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">thetalim([0 180])<\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">set(gca,<span style=\"color: #a709f5\">'ThetaZeroLocation'<\/span>,<span style=\"color: #a709f5\">'bottom'<\/span>)<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_2\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_4.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">This looks a bit exaggerated in polar coordinates since the polar chart is perfectly round. When we apply it to the sphere we'll also squish it to simulate that our pumpkin is shorter than it is wide. That will give it a more natural look. I took a bit of a guess that the height of the pumpkin is just 80% of the diameter at the equator.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Computing the <span style=\"font-family: monospace\">Zpunkin<\/span> matrix requires adding bumps across the rows the same way as <span style=\"font-family: monospace\">Xpunkin<\/span> and <span style=\"font-family: monospace\">Ypunkin<\/span> do. It also needs the height to be adjusted, and the dimple applied on the columns. This is done using element-wise multiply <span style=\"font-family: monospace\">.*<\/span> using the row vector <span style=\"font-family: monospace\">Rxy<\/span>, and the column vector <span style=\"font-family: monospace\">Rz_dimple<\/span>.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\"><img decoding=\"async\" loading=\"lazy\" class=\"imageNode\" style=\"vertical-align: baseline;width: 441px;height: 331px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_5.png\" alt=\"\" width=\"441\" height=\"331\" \/><\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">heightratio = .8;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Zpunkin = (1+Rxy).*Zsphere.*(heightratio+Rz_dimple);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><span style=\"color: #008013\">%\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ^bumps\u00a0 ^sphere mesh ^squish 80%\u00a0 ^column vector to apply dimple<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">surf(Xpunkin,Ypunkin,Zpunkin)<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">shading <span style=\"color: #a709f5\">interp<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">daspect([1 1 1])<\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">camlight<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_3\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_6.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">We're getting closer, but the dimple still looks a bit weird. Fortunately, we can stick a stem on there to hide that artifact.<\/div>\n<h2 style=\"margin: 20px 10px 5px 4px;padding: 0px;line-height: 20px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 20px;font-weight: bold;text-align: left\">Designing a Stem<\/h2>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Before starting on the stem, let's take a close look at our target pumpkin's stem:<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\"><img decoding=\"async\" loading=\"lazy\" class=\"imageNode\" style=\"vertical-align: baseline;width: 341px;height: 319px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_7.png\" alt=\"speckled_pumpkin_stem.jpg\" width=\"341\" height=\"319\" \/><\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">You'll notice that every bump on the stem lines up with one of the primary creases in our pumpkin! Turns out this is because the bumps that naturally occur on the stem impact the pumpkin as it grows, constraining it into a crease. For this particular pumpkin it isn't as well defined as with other breeds, and one stem bump seems to be missing top-left, but that's ok. This is MATLAB, so we'll create a perfect pumpkin stem instead.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">To start, we'll create another pattern, similar to the pumpkin creases by re-using <span style=\"font-family: monospace\">rPrimary<\/span>, but offset from the pumpkin bumps by <span style=\"font-family: monospace\">1\/2<\/span> a pattern. That way the outward bumps of the stem line up with the creases in the pumpkin.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Next, we'll create some cylinder height coordinates as <span style=\"font-family: monospace\">Zcyl<\/span> and square it to compute a curved profile so the stem is wider on the bottom than on the top.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Rstem = (1-(1-mod(rPrimary+1,2)).^2)*.05;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">thetac = linspace(0,2,numVerts);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Xcyl = cospi(thetac);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Ycyl = sinpi(thetac);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Zcyl = linspace(0,1,11)'; <span style=\"color: #008013\">% column vector<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px 0px 4px 4px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Rstemz = .7+(1-Zcyl).^2*.6;<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Once we have these base coordinates, compute the cylindrical mesh the same way we did for the pumpkin sphere, but instead apply it to the cylinder coordinates. The row vectors <span style=\"font-family: monospace\">Xcyl<\/span> and <span style=\"font-family: monospace\">Ycyl,<\/span> when multiplied by the column vector <span style=\"font-family: monospace\">Rstemz<\/span> using element-wise multiplication operator \"<span style=\"font-family: monospace\">.*<\/span>\", will implicitly expand into a 2D matrix, perfect for drawing a mesh.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\"><img decoding=\"async\" loading=\"lazy\" class=\"imageNode\" style=\"vertical-align: baseline;width: 482px;height: 360px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_8.png\" alt=\"\" width=\"482\" height=\"360\" \/><\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Xstem = (.1+Rstem).*Xcyl.*Rstemz;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Ystem = (.1+Rstem).*Ycyl.*Rstemz;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px 0px 4px 4px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Zstem = repmat(Zcyl*.15,1,numVerts);<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Let's take a look at our stem using a <span style=\"font-family: monospace\">surf<\/span> plot and fill in the top with a <span style=\"font-family: monospace\">patch<\/span>.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">surf(Xstem,Ystem,Zstem);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">shading <span style=\"color: #a709f5\">interp<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">patch(<span style=\"color: #a709f5\">'Vertices'<\/span>, [Xstem(end,:)' Ystem(end,:)' Zstem(end,:)'],<span style=\"color: #0e00ff\">...<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #a709f5\">'Faces'<\/span>, 1:numVerts, <span style=\"color: #0e00ff\">...<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #a709f5\">'FaceColor'<\/span>,<span style=\"color: #a709f5\">'y'<\/span>,<span style=\"color: #a709f5\">'EdgeColor'<\/span>,<span style=\"color: #a709f5\">'none'<\/span>)<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">lighting <span style=\"color: #a709f5\">gouraud<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">daspect([1 1 1]);<\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">camlight<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_4\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_9.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">This stem has a lot more definition than our target pumpkin. We'll say it's a more perfect stem.<\/div>\n<h2 style=\"margin: 20px 10px 5px 4px;padding: 0px;line-height: 20px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 20px;font-weight: bold;text-align: left\">Putting it all together!<\/h2>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Ok, let's put it all together! In this next code snippet, we'll re-create the pumpkin and give it a new colormap with colors selected off the photo of our target pumpkin. Then we'll add the stem but offset it by the height of our pumpkin. We'll also color the stem and stem cap based on colors also selected from the photo.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">One trick in this code snippet is the creation of a colormap with only 2 colors. The <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/validatecolor.html\"><span style=\"font-family: monospace\">validatecolor<\/span><\/a> function is a new feature from R2020b that will convert valid color strings like web hex codes into RGB colors. This makes it easy to use a common color specifier in MATLAB.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Spunkin = surf(Xpunkin,Ypunkin,Zpunkin,<span style=\"color: #a709f5\">'FaceColor'<\/span>,<span style=\"color: #a709f5\">'interp'<\/span>,<span style=\"color: #a709f5\">'EdgeColor'<\/span>,<span style=\"color: #a709f5\">'none'<\/span>);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">colormap(validatecolor({<span style=\"color: #a709f5\">'#da8e26' '#dfc727'<\/span>},<span style=\"color: #a709f5\">'multiple'<\/span>));<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Sstem = surface(Xstem,Ystem,Zstem+heightratio^2,<span style=\"color: #a709f5\">'FaceColor'<\/span>,<span style=\"color: #a709f5\">'#3d6766'<\/span>,<span style=\"color: #a709f5\">'EdgeColor'<\/span>,<span style=\"color: #a709f5\">'none'<\/span>);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Pstem = patch(<span style=\"color: #a709f5\">'Vertices'<\/span>, [Xstem(end,:)' Ystem(end,:)' Zstem(end,:)'+heightratio^2],<span style=\"color: #0e00ff\">...<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #a709f5\">'Faces'<\/span>, 1:numVerts, <span style=\"color: #0e00ff\">...<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #a709f5\">'FaceColor'<\/span>,<span style=\"color: #a709f5\">'#b1cab5'<\/span>,<span style=\"color: #a709f5\">'EdgeColor'<\/span>,<span style=\"color: #a709f5\">'none'<\/span>);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">daspect([1 1 1])<\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">camlight<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_5\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_10.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Hmm. We didn't spend any time computing where the colors would go, so by default, color is the same as <span style=\"font-family: monospace\">Z<\/span>.<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Our goal is for the depths of the creases to be orange, and the bumps to be yellow, so we'll use <span style=\"font-family: monospace\">hypot<\/span> to compute distance from center as if we had a pumpkin with no dimples on the top and bottom. Let's see how that looks:<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">Cpunkin = hypot(hypot(Xpunkin,Ypunkin),(1+Rxy).*Zsphere); <span style=\"color: #008013\">% As if pumpkin were round with no dimples<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\"><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">set(Spunkin,<span style=\"color: #a709f5\">'CData'<\/span>,Cpunkin); <span style=\"color: #008013\">% Pumpkin CData<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">set(Sstem,<span style=\"color: #a709f5\">'CData'<\/span>,[]); <span style=\"color: #008013\">% Make sure the stem doesn't contribute to auto Color Limits<\/span><\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_6\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_11.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">This looks too crisp due to the sharp boundary between the orange and yellow colors caused by having only 2 colors in the colormap. Fortunately, we still need to add all the speckles which we can do by adding some normally distributed random noise. Selecting the right scale for the noise took a bit of fiddling so it didn't overwhelm the bumps.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">set(Spunkin,<span style=\"color: #a709f5\">'CData'<\/span>,Cpunkin+randn(numVerts)*0.015);<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_7\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_12.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 style=\"margin: 3px 10px 5px 4px;padding: 0px;line-height: 18px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 17px;font-weight: bold;text-align: left\">Pumpkin Polish<\/h3>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">The speckles mitigated the hard color boundary, so with that out of the way, we are almost done. Let's decorate by improving the lighting and material properties to give it more of a waxy light reflectance and add in more ambient light. We'll also turn off the axes and zoom in a bit.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 0 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">daspect([1 1 1])<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">axis <span style=\"color: #a709f5\">off<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">camzoom(1.8)<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">lighting <span style=\"color: #a709f5\">gouraud<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">material([Spunkin Sstem Pstem],[ .6 .9 .3 2 .6 ])<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_8\">\n<div class=\"figureElement eoOutputContent\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"max-width: 560px;width: 100%;padding-bottom: 0px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_13.png\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\"><strong>Armed now with this vital pumpkin knowledge, what sort of gourd can you recreate in MATLAB<\/strong>?<\/div>\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Share your MATLAB Pumpkin images with us in the comments below!<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<p><script type=\"text\/javascript\">\n{ let css = '';\nlet head = document.head || document.getElementsByTagName('head')[0];\nlet style = document.createElement('style');\nhead.appendChild(style);\nstyle.type = 'text\/css';\nif (style.styleSheet) {\n    style.styleSheet.cssText = css;\n} else {\n    style.appendChild(document.createTextNode(css));\n}<\/p>\n<p>}<\/script><a href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx\"><button class=\"btn btn-sm btn_color_blue pull-right add_margin_10\">Download Live Script<\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/10\/grourdsToGraphics.mlx-10-30-23_13.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div>\n<p>Guest Writer: Eric Ludlam<\/p>\n<p>Today's graphics adventure is written by Eric Ludlam, development manager of MATLAB's Charting team. Eric has a whopping 27 years of experience working in MATLAB... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2023\/10\/31\/gourds-to-graphics-the-matlab-pumpkin\/\">read more >><\/a><\/p>\n","protected":false},"author":198,"featured_media":372,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,2],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/402"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/users\/198"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/comments?post=402"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/402\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/402\/revisions\/431"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/media\/372"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/media?parent=402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/categories?post=402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/tags?post=402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}