{"id":304,"date":"2009-06-02T14:35:05","date_gmt":"2009-06-02T21:35:05","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1487"},"modified":"2009-06-02T14:35:05","modified_gmt":"2009-06-02T21:35:05","slug":"calculate-slope-of-a-line","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=304","title":{"rendered":"Calculate Slope of a Line"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ calculate the slope of a line\r\nfunction calculateSlope(x1:Number, y1:Number, x2:Number, y2:Number):Number {\r\n        \/\/ rise over run\r\n\tvar s:Number = (y1 - y2) \/ (x1 - x2);\r\n\t\/*if (x1==x2) {\r\n\t\t\/\/ slope is Infinity or -Infinity\r\n\t}*\/\r\n\treturn s;\r\n}\r\n\r\n\/**\r\n Test it out\r\n*\/\r\nfunction draw(x1:Number, y1:Number, x2:Number, y2:Number):void {\r\n\tgraphics.moveTo(x1, y1);\r\n\tgraphics.lineTo(x2, y2)\r\n\tvar txt:TextField =TextField(addChild(new TextField()));\r\n\ttxt.text = calculateSlope(x1, y1, x2, y2).toFixed(2);\r\n\ttxt.x = x2, txt.y = y2;\r\n}\r\n\r\ngraphics.lineStyle(0,0xFF0000);\r\ndraw (100, 100, 200, 200);\r\n\r\ndraw(100, 100, 200, 150);\r\n\r\ndraw(100, 100, 200, 100);\r\n\r\ndraw(100, 100, 99, 200);\r\n<\/pre>\n<p>This snippet shows how to calculate the slope of a line &#8230; It demos the function by drawing a few lines and showing the corresponding slope of each.<\/p>\n<p>It will draw this:<br \/>\n<img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/slope.gif\" \/><\/p>\n<p>If (x1 == x2), the slope will be -Infinity or Infinity&#8230; depending on where you&#8217;re using this calculation you may want to reset the s variable to something else, return null etc&#8230; I commented it out for simplicity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ calculate the slope of a line function calculateSlope(x1:Number, y1:Number, x2:Number, y2:Number):Number { \/\/ rise over run var s:Number = (y1 &#8211; y2) \/ (x1 &#8211; x2); \/*if (x1==x2) { \/\/ slope is Infinity or -Infinity }*\/ return s; } \/** Test it out *\/ function draw(x1:Number, y1:Number, x2:Number, y2:Number):void { graphics.moveTo(x1, y1); graphics.lineTo(x2, y2) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121,94],"tags":[3,14,4],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-math","category-misc","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}