{"id":3203,"date":"2013-09-18T07:00:00","date_gmt":"2013-09-18T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/09\/18\/subtractrect-doesnt-always-give-you-the-exact-difference\/"},"modified":"2013-09-18T07:00:00","modified_gmt":"2013-09-18T07:00:00","slug":"subtractrect-doesnt-always-give-you-the-exact-difference","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130918-00\/?p=3203","title":{"rendered":"SubtractRect doesn&#039;t always give you the exact difference"},"content":{"rendered":"<p>\nThe <code>Subtract&shy;Rect<\/code> function takes a source\nrectangle and subtracts out the portion which intersects\na second rectangle,\nreturning the result in a third rectangle.\nBut wait a second,\nthe result of subtracting one rectangle from another need not\nbe another rectangle.\nIt might be an L-shape,\nor it might be a rectangle with a rectangular hole.\nHow does this map back to a rectangle?\n<\/p>\n<p>\nThe documentation for\n<code>Subtract&shy;Rect<\/code> says\nthat the function performs the subtraction when they\n&#8220;intersect completely in either the x- or y-direction.&#8221;\nBut I prefer to think of it as the alternate formulation\noffered in the documentation:\n&#8220;In other words, the resulting rectangle is the bounding box\nof the geometric difference.&#8221;\n<\/p>\n<p>\nI was reminded of this subject when I saw some code\nthat tried to do rectangle manipulation like this:\n<\/p>\n<pre>\n<i>\/\/ Clip rcA to be completely inside rcB.\nRECT rcSub;\n\/\/ rcSub = the part of rcA that stick out beyond rcB\nif (SubtractRect(&amp;rcSub, &amp;rcA, &amp;rcB)) {\n    \/\/ Remove that part from rcA\n    SubtractRect(&amp;rcA, &amp;rcA, &amp;rcSub);\n}<\/i>\n<\/pre>\n<p>\nIf the rectangle <code>rcA<\/code> extends beyond <code>rcB<\/code>\nin more than one direction,\nthen the geometric difference will not be rectangular,\nand the result of\n<code>Subtract&shy;Rect<\/code>\nwill be expanded to the bounding box of the difference,\nwhich means that it will return <code>rcA<\/code> again.\nAnd then the second line will subtract it all out,\nleaving the rectangle empty.\n<\/p>\n<p>\nOops.\n<\/p>\n<p>\nWhat they really wanted was\n<\/p>\n<pre>\n\/\/ Clip rcA to be completely inside rcB.\nIntersectRect(&amp;rcA, &amp;rcA, &amp;rcB);\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Subtract&shy;Rect function takes a source rectangle and subtracts out the portion which intersects a second rectangle, returning the result in a third rectangle. But wait a second, the result of subtracting one rectangle from another need not be another rectangle. It might be an L-shape, or it might be a rectangle with a rectangular [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-3203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The Subtract&shy;Rect function takes a source rectangle and subtracts out the portion which intersects a second rectangle, returning the result in a third rectangle. But wait a second, the result of subtracting one rectangle from another need not be another rectangle. It might be an L-shape, or it might be a rectangle with a rectangular [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=3203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3203\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=3203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}