{"id":263,"date":"2015-07-13T09:15:39","date_gmt":"2015-07-13T13:15:39","guid":{"rendered":"https:\/\/blogs.mathworks.com\/graphics\/?p=263"},"modified":"2015-07-13T09:15:39","modified_gmt":"2015-07-13T13:15:39","slug":"graphicssmoothing-and-alignvertexcenters","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/graphics\/2015\/07\/13\/graphicssmoothing-and-alignvertexcenters\/","title":{"rendered":"GraphicsSmoothing and AlignVertexCenters"},"content":{"rendered":"<div class=\"content\"><h3>GraphicsSmoothing and AlignVertexCenters<\/h3><p>In R2014b we introduced a new property on the figure named <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/examples\/using-graphics-smoothing.html\">GraphicsSmoothing<\/a>. When this is set to on, all of the graphics drawn into that figure are drawn using a technique called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Multisample_anti-aliasing\">Multisample antialiasing<\/a>. This can really improve how the graphics look on a low res monitor.<\/p><p>Let's draw a bunch of random line segments, first with GraphicsSmoothing set to 'off'.<\/p><pre class=\"codeinput\">rng <span class=\"string\">default<\/span>\r\nfigure(<span class=\"string\">'GraphicsSmoothing'<\/span>,<span class=\"string\">'off'<\/span>)\r\n\r\nx = randn(2,100);\r\nx(3,:) = nan;\r\ny = randn(2,100);\r\ny(3,:) = nan;\r\nplot(x(:),y(:))\r\ntitle(<span class=\"string\">'GraphicsSmoothing = off'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/msaa_question_01.png\" alt=\"\"> <p>And then with GraphicsSmoothing set to 'on'.<\/p><pre class=\"codeinput\">set(gcf,<span class=\"string\">'GraphicsSmoothing'<\/span>,<span class=\"string\">'on'<\/span>)\r\ntitle(<span class=\"string\">'GraphicsSmoothing = on'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/msaa_question_02.png\" alt=\"\"> <p>The lines that were drawn with GraphicsSmoothing obviously look smoother. If we compare a zoomed in region we can see why.<\/p><p>\r\n<table>\r\n<tr>\r\n<td align='center'><b>GraphicsSmoothing='off'<\/b><\/td>\r\n<td align='center'><b>GraphicsSmoothing='on'<\/b><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/zoom_gs_off.png\"><\/td>\r\n<td><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/zoom_gs_on.png\"><\/td>\r\n<\/tr>\r\n<\/table>\r\n<\/p><p>We can see that when GraphicsSmoothing is off, the pixels that make up the lines are all the same color. When GraphicsSmoothing is on, the lines are made up of pixels of a number of different colors. These colors are made by mixing the color of the line with the background color. The proportion of the two colors that are mixed together is proportional to how much of that pixel is covered by the line. This tricks your eye into seeing a smooth line instead of one that \"jumps\" from pixel to pixel.<\/p><p>But there is an issue here. Let's start over with a bunch of vertical lines.<\/p><pre class=\"codeinput\">close\r\nfigure(<span class=\"string\">'GraphicsSmoothing'<\/span>,<span class=\"string\">'off'<\/span>)\r\nx = repmat(1:75,[3 1]);\r\ny = repmat([-1; 1; nan],[1 75]);\r\nh = line(x(:),y(:));\r\nylim([-4 4])\r\ntitle(<span class=\"string\">'GraphicsSmoothing = off'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/msaa_question_03.png\" alt=\"\"> <p>When we turn on GraphicsSmoothing this time, we'll notice that some of the lines look darker than others.<\/p><pre class=\"codeinput\">set(gcf,<span class=\"string\">'GraphicsSmoothing'<\/span>,<span class=\"string\">'on'<\/span>)\r\ntitle(<span class=\"string\">'GraphicsSmoothing = on'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/msaa_question_04.png\" alt=\"\"> <p>This is happening because some of the lines go exactly through a column of pixels and other lines fall in between two columns. The ones that fall in between set two pixels in each row to a light shade, while the ones that go exactly through a column set one pixel in the row to a dark shade.<\/p><p>Your computer has an adjustment in the display settings called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gamma_correction\">gamma correction<\/a>. If this is not set accurately, then the difference between these two cases can be very visible.<\/p><p>But there is another way to deal with this. The line object has a property named AlignVertexCenters. If we set this to on, then the vertices of the lines will be adjusted so that they land exactly on the pixels. This will make all of the lines look the same.<\/p><pre class=\"codeinput\">h.AlignVertexCenters = <span class=\"string\">'on'<\/span>;\r\ntitle(<span class=\"string\">'AlignVertexCenters = on'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/msaa_question_05.png\" alt=\"\"> <p>So should we always set AlignVertexCenters to on? No, I'm afraid it's not that simple. One thing you'll notice is that the spacing between the lines looks uneven in the GraphicsSmoothing=off and AlignVertexCenter=on cases. That's because the distance between the centers of the lines is a little more than 5 pixels. This means that when they get snapped to the pixel grid, you get a mix of 4 pixel gaps and 5 pixel gaps. In the GraphicsSmoothing=on case thing don't look at \"gap toothed\" because the shading fills in the edges of the wider gaps.<\/p><p>But there's another important reason that you might not want to use AlignVertexCenters. Let's look at another case.<\/p><pre class=\"codeinput\">clf\r\nang = linspace(0,2*pi,30)';\r\nx = repmat([cos(ang); nan],[1 12]) + repmat(3*(1:12),[31 1]);\r\ny = repmat([sin(ang); nan],[1 12]);\r\nh = line(x(:),y(:));\r\naxis <span class=\"string\">equal<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/msaa_question_06.png\" alt=\"\"> <p>Those circles look nice and round, but if we turn AlignVertexCenters, they don't look as good, do they?<\/p><pre class=\"codeinput\">h.AlignVertexCenters = <span class=\"string\">'on'<\/span>;\r\ntitle(<span class=\"string\">'AlignVertexCenters = on'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/graphics\/2015\/msaa_question_07.png\" alt=\"\"> <p>For smooth shapes like these circles, we don't want to use AlignVertexCenters because distorts the shape by moving each of the vertices to the nearest pixel.<\/p><p>In general, you only want to use AlignVertexCenters when you're drawing something that's only made up of a relatively small number of horizontal or vertical lines.<\/p><p>Antialiasing is a particularly interesting corner of computer graphics, and I'll be talking about it more in future posts. Do you have any questions about it that you'd like me to address?<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_d2e817552cb3460c8bea8437f2f44442() {\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='d2e817552cb3460c8bea8437f2f44442 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d2e817552cb3460c8bea8437f2f44442';\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 2015 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_d2e817552cb3460c8bea8437f2f44442()\"><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; R2015a<br><\/p><\/div><!--\r\nd2e817552cb3460c8bea8437f2f44442 ##### SOURCE BEGIN #####\r\n%% GraphicsSmoothing and AlignVertexCenters\r\n% In R2014b we introduced a new property on the figure named\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/examples\/using-graphics-smoothing.html GraphicsSmoothing>. When this is set to on, all of the graphics drawn into\r\n% that figure are drawn using a technique called <https:\/\/en.wikipedia.org\/wiki\/Multisample_anti-aliasing Multisample\r\n% antialiasing>. This can really improve how the graphics look on a low res\r\n% monitor. \r\n%\r\n% Let's draw a bunch of random line segments, first with GraphicsSmoothing\r\n% set to 'off'.\r\nrng default\r\nfigure('GraphicsSmoothing','off')\r\n\r\nx = randn(2,100);\r\nx(3,:) = nan;\r\ny = randn(2,100);\r\ny(3,:) = nan;\r\nplot(x(:),y(:))\r\ntitle('GraphicsSmoothing = off')\r\n\r\n%%\r\n% And then with GraphicsSmoothing set to 'on'.\r\nset(gcf,'GraphicsSmoothing','on')\r\ntitle('GraphicsSmoothing = on')\r\n\r\n%%\r\n% The lines that were drawn with GraphicsSmoothing obviously look smoother.\r\n% If we compare a zoomed in region we can see why.\r\n%\r\n% <html>\r\n% <table>\r\n% <tr>\r\n% <td align='center'><b>GraphicsSmoothing='off'<\/b><\/td>\r\n% <td align='center'><b>GraphicsSmoothing='on'<\/b><\/td>\r\n% <\/tr>\r\n% <tr>\r\n% <td><img decoding=\"async\" src=\"..\/zoom_gs_off.png\"><\/td>\r\n% <td><img decoding=\"async\" src=\"..\/zoom_gs_on.png\"><\/td>\r\n% <\/tr>\r\n% <\/table>\r\n% <\/html>\r\n%\r\n% We can see that when GraphicsSmoothing is off, the pixels that make up\r\n% the lines are all the same color. When GraphicsSmoothing is on, the\r\n% lines are made up of pixels of a number of different colors. These colors\r\n% are made by mixing the color of the line with the background color. The\r\n% proportion of the two colors that are mixed together is proportional to\r\n% how much of that pixel is covered by the line. This tricks your eye into\r\n% seeing a smooth line instead of one that \"jumps\" from pixel to pixel.\r\n\r\n%%\r\n% But there is an issue here. Let's start over with a bunch of vertical\r\n% lines.\r\nclose\r\nfigure('GraphicsSmoothing','off')\r\nx = repmat(1:75,[3 1]);\r\ny = repmat([-1; 1; nan],[1 75]);\r\nh = line(x(:),y(:));\r\nylim([-4 4])\r\ntitle('GraphicsSmoothing = off')\r\n\r\n%%\r\n% When we turn on GraphicsSmoothing this time, we'll notice that some of\r\n% the lines look darker than others.\r\nset(gcf,'GraphicsSmoothing','on')\r\ntitle('GraphicsSmoothing = on')\r\n\r\n%%\r\n% This is happening because some of the lines go exactly through a column\r\n% of pixels and other lines fall in between two columns. The ones that\r\n% fall in between set two pixels in each row to a light shade, while the ones that\r\n% go exactly through a column set one pixel in the row to a dark shade.\r\n%\r\n% Your computer has an adjustment in the display settings called\r\n% <https:\/\/en.wikipedia.org\/wiki\/Gamma_correction gamma correction>. If\r\n% this is not set accurately, then the difference between\r\n% these two cases can be very visible.\r\n\r\n%%\r\n% But there is another way to deal with this. The line object has a\r\n% property named AlignVertexCenters. If we set this to on, then the\r\n% vertices of the lines will be adjusted so that they land exactly on the\r\n% pixels. This will make all of the lines look the same.\r\nh.AlignVertexCenters = 'on';\r\ntitle('AlignVertexCenters = on')\r\n\r\n%%\r\n% So should we always set AlignVertexCenters to on? No, I'm afraid it's not that\r\n% simple. One thing you'll notice is that the spacing between the lines\r\n% looks uneven in the GraphicsSmoothing=off and AlignVertexCenter=on cases.\r\n% That's because the distance between the centers of the lines is a little \r\n% more than 5 pixels. This means that when they get snapped to the pixel \r\n% grid, you get a mix of 4 pixel gaps and 5 pixel gaps. In the\r\n% GraphicsSmoothing=on case thing don't look at \"gap toothed\" because the\r\n% shading fills in the edges of the wider gaps. \r\n%\r\n% But there's another important reason that you might not want to use \r\n% AlignVertexCenters. Let's look at another case.\r\nclf\r\nang = linspace(0,2*pi,30)';\r\nx = repmat([cos(ang); nan],[1 12]) + repmat(3*(1:12),[31 1]);\r\ny = repmat([sin(ang); nan],[1 12]);\r\nh = line(x(:),y(:));\r\naxis equal\r\n\r\n%%\r\n% Those circles look nice and round, but if we turn AlignVertexCenters, \r\n% they don't look as good, do they?\r\nh.AlignVertexCenters = 'on';\r\ntitle('AlignVertexCenters = on')\r\n\r\n%%\r\n% For smooth shapes like these circles, we don't want to use\r\n% AlignVertexCenters because distorts the shape by moving each of the\r\n% vertices to the nearest pixel.\r\n%\r\n% In general, you only want to use AlignVertexCenters when you're drawing\r\n% something that's only made up of a relatively small number of horizontal \r\n% or vertical lines.\r\n%\r\n% Antialiasing is a particularly interesting corner of computer graphics,\r\n% and I'll be talking about it more in future posts. Do you have any\r\n% questions about it that you'd like me to address? \r\n##### SOURCE END ##### d2e817552cb3460c8bea8437f2f44442\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/feature_image\/graphics_smoothing_thumbnail.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>GraphicsSmoothing and AlignVertexCentersIn R2014b we introduced a new property on the figure named GraphicsSmoothing. When this is set to on, all of the graphics drawn into that figure are drawn... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/graphics\/2015\/07\/13\/graphicssmoothing-and-alignvertexcenters\/\">read more >><\/a><\/p>","protected":false},"author":89,"featured_media":267,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/263"}],"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=263"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":272,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/263\/revisions\/272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/media\/267"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}