{"id":601,"date":"2017-10-04T10:30:27","date_gmt":"2017-10-04T15:30:27","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=601"},"modified":"2017-10-08T14:32:59","modified_gmt":"2017-10-08T19:32:59","slug":"processing-py-how-to-calculate-the-midpoint-of-a-line","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/10\/04\/processing-py-how-to-calculate-the-midpoint-of-a-line\/","title":{"rendered":"Processing.py: How to calculate the midpoint of a line"},"content":{"rendered":"<p>To calculate the midpoint of a line, use the following formula to calculate the x and y midpoints:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Middle of line\r\n# midpoint = (xM, yM)\r\nxM = (x1 + x2)\/2\r\nyM = (y1 + y2)\/2<\/pre>\n<p>Then the midpoint coordinates <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">(xM, yM)<\/code> can be used to draw an ellipse on the line. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">ellipse(xM, yM, 10, 10)<\/pre>\n<p>Here is a more complete code example, showing this in practice:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">x1 = 50\r\ny1 = 50\r\nx2 = 250\r\ny2 = 250\r\n\r\n# circle_size\r\ncs = 4\r\n\r\ndef setup():\r\n    size(300, 300)\r\n    stroke(0)\r\n     \r\ndef draw():\r\n    fill(0)\r\n    l1 = line_with_points(x1, y1, x2, y2, cs)\r\n     \r\nclass line_with_points(object):\r\n    def __init__(self, x1, y1, x2, y2, cs):\r\n        self.x1 = x1\r\n        self.x2 = x2\r\n        self.y1 = y1\r\n        self.y2 = y2\r\n        self.cs = cs\r\n        \r\n        # Beginning of line\r\n        ellipse(x1, y1, cs, cs)\r\n        # End of line\r\n        ellipse(x2, y2, cs, cs)\r\n        line(x1, y1, x2, y2)\r\n        \r\n        # Middle of line\r\n        xM = (x1 + x2)\/2\r\n        yM = (y1 + y2)\/2\r\n        fill(255)\r\n        ellipse(xM, yM, cs+5, cs+5)<\/pre>\n<p>Which produces:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-635 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/line_midpoint_sm.png\" alt=\"\" width=\"299\" height=\"325\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/line_midpoint_sm.png 299w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/line_midpoint_sm-276x300.png 276w\" sizes=\"auto, (max-width: 299px) 100vw, 299px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To calculate the midpoint of a line, use the following formula to calculate the x and y midpoints: # Middle of line # midpoint = (xM, yM) xM = (x1 + x2)\/2 yM = (y1 + y2)\/2 Then the midpoint coordinates (xM, yM) can be used to draw an ellipse on the line. For example: &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/10\/04\/processing-py-how-to-calculate-the-midpoint-of-a-line\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Processing.py: How to calculate the midpoint of a line<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,46],"tags":[36,4],"class_list":["post-601","post","type-post","status-publish","format-standard","hentry","category-processing-language","category-processing-py","tag-processing-py","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=601"}],"version-history":[{"count":34,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/601\/revisions"}],"predecessor-version":[{"id":637,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/601\/revisions\/637"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}