{"id":1042,"date":"2004-03-26T11:41:00","date_gmt":"2004-03-26T17:41:00","guid":{"rendered":"http:\/\/www.lordandrei.com\/blog\/?p=1042"},"modified":"2004-03-26T11:41:00","modified_gmt":"2004-03-26T17:41:00","slug":"math-is-fun","status":"publish","type":"post","link":"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/","title":{"rendered":"Math is fun?"},"content":{"rendered":"<p><font size=\"-1\">Cross posted in macosxdev and my home journal<\/font><\/p>\n<p>Warning, this is all math and geometry&#8230; turn around and back away slowly if you value your life \ud83d\ude42<\/p>\n<p>I am posting this mainly for external validation. I think I have the concept right but my geometry is just a little rusty.<\/p>\n<p><!--more CUT: Read on if you want to check my work and see some real live code.-->I am currently working on a resize algorithm. I have a maximum box size of W x H.<br \/>\nNow, if given a box that is W+Y x H+Z where Y and Z are some arbitrary overage to the Width and Height, I need to scale this box to (no larger than) W x H while retaining the boxes original ratio.<\/p>\n<p>For the purpose of illustration W > H<br \/>\nMy box can not be bigger than 80&#215;40 and I&#8217;ve been handed a box 100&#215;60.<\/p>\n<p>My theory is: to scale this correctly I need to find the greater of Y and (Z * W\/H)<\/p>\n<p>I have to apply a &#8220;max size ratio&#8221; because the fact that the overage is equal in length doesn&#8217;t necessarily mean it is equal in relation to the length of the side&#8217;s maximum. In my example both sides have an overage of 20 but the height is 50% over the height max while the width is only 25%. By applying the max size ratio, the % differences equal out so an equal percent scale would bring both sides within the needed parameters.<\/p>\n<p>Once the greater is discovered, a scaleRatio can be computed by:<br \/>\nsr = side \/ (side + side&#8217;s overage)<\/p>\n<p>This scaleRatio can now be applied to each side of the given box.<\/p>\n<p>As for the code. Yes, this is in objective-C. I don&#8217;t know if I am reinventing some wheel that is in one of the frameworks. I had a problem and I did what I can to solve it. I&#8217;m curious if this solution is viable or if people have other solutions. Also, wanted to post the thinking and code for an algorithm for people who are interested in seeing such things.<\/p>\n<pre>typedef struct _NSSize {\rfloat width;\rfloat height;\r} NSSize;\rextern NSSize  kMaxSize;\r\r-(NSSize) scaleToMaxSize:(NSSize)someSize\r{\rNSSize    retSize = someSize;\r\rif ((someSize.height > kMaxSize.height) || (someSize.width > kMaxSize.width)) {\rfloat    heightOverage = someSize.height - kMaxSize.height;\rfloat    widthOverage = someSize.width - kMaxSize.width;\rfloat    scaleRatio=0.0;\r\rif (heightOverage * (kMaxSize. width * kMaxSize.height) > widthOverage) {\rscaleRatio = kMaxSize.height \/ someSize.height;\r} else {\rscaleRatio = kMaxSize.width \/ someSize. width;\r}\r\rif (scaleRatio > 0.0) {\rretSize.heigth *= scaleRatio;\rretSize.width *= scaleRatio;\r}\r\rreturn retSize;\r}<\/pre>\n<p>Thanks.<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-1042\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Facebook (Opens in new window)<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-1042\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Twitter (Opens in new window)<\/span><\/a><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-1042\" class=\"share-pinterest sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Pinterest (Opens in new window)<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Tumblr (Opens in new window)<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Cross posted in macosxdev and my home journal Warning, this is all math and geometry&#8230; turn around and back away slowly if you value your life \ud83d\ude42 I am posting this mainly for external validation. I think I have the concept right but my geometry is just a little rusty.<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-1042\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Facebook (Opens in new window)<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-1042\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Twitter (Opens in new window)<\/span><\/a><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-1042\" class=\"share-pinterest sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Pinterest (Opens in new window)<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2004\/03\/26\/math-is-fun\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Tumblr (Opens in new window)<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[1],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1X6ba-gO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/posts\/1042"}],"collection":[{"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/comments?post=1042"}],"version-history":[{"count":0,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/posts\/1042\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/media?parent=1042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/categories?post=1042"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/tags?post=1042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}