{"id":962,"date":"2017-11-28T21:20:23","date_gmt":"2017-11-29T02:20:23","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=962"},"modified":"2017-11-28T21:20:23","modified_gmt":"2017-11-29T02:20:23","slug":"p5-js-understanding-circles","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/11\/28\/p5-js-understanding-circles\/","title":{"rendered":"p5.js: Understanding circles"},"content":{"rendered":"<p>To understand how to code with circles in p5.js, it is necessary to have a foundation of knowledge about the parts of a circle and how they map to code.<\/p>\n<p>The first mapping is the name. Circles are actually called ellipses in p5.js and are produced using the <a href=\"https:\/\/p5js.org\/reference\/#\/p5\/ellipse\">ellipse()<\/a> function, that takes in four (or three) arguments. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/ ellipse(x, y, w, h) \r\n\/\/ h is optional, if left undefined then w will be used for both width and height\r\nellipse(50, 50, 80, 80);<\/pre>\n<p>The first two arguments describe where to place the circle. They are the x and y coordinates of the circle&#8217;s origin point (center). The second two arguments are the width and height of the circle. The final argument, h, is optional. If left out, the ellipse will be a circle and w will be used for both width and height. If w and h values are different, then the ellipse is an oval, not a circle.<\/p>\n<p>As can be seen in the following graphic, some of the parts of a circle are:<\/p>\n<ul>\n<li><strong>central vertex (origin)<\/strong> &#8211; the center point of the circle<\/li>\n<li><strong>circumference<\/strong> &#8211; the length of the outside line of the circle<\/li>\n<li><strong>diameter<\/strong> &#8211; the total distance across the circle<\/li>\n<li><strong>radius<\/strong> &#8211; half of the distance across the circle<\/li>\n<li><strong>sector<\/strong> &#8211; a pie shaped piece of the circle<\/li>\n<li><strong>chord<\/strong> &#8211; a line that cuts across the circle that doesn&#8217;t go through the origin<\/li>\n<li><strong>segment<\/strong> &#8211; portions of the circle created by a chord<\/li>\n<li><strong>arc<\/strong> &#8211; a section of the circumference<\/li>\n<li><strong>tangent<\/strong> &#8211; a line outside the circle that intersects a point on the circle<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone wp-image-968\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/11\/circle_parts-300x300.jpg\" alt=\"\" width=\"320\" height=\"320\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/11\/circle_parts-300x300.jpg 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/11\/circle_parts-150x150.jpg 150w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/11\/circle_parts.jpg 539w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p>When working with ellipses, p5.js allows you to use either degrees or radians, but the default is radians. There is a function called <a href=\"https:\/\/p5js.org\/reference\/#\/p5\/angleMode\">angleMode()<\/a> that allows you to switch either to degrees or radians. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/ Change the mode to DEGREES\r\nangleMode(DEGREES); \r\n\r\n\/\/ Change the mode to RADIANS\r\nangleMode(RADIANS); \r\n<\/pre>\n<p>The following graphic shows the unit circle with both radians and degrees mapped to it, along with Cartesian coordinates. When coding with circles, this is a helpful reference that shows equivalents between radians and degrees.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone wp-image-971 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/11\/unit-circle-chart-radians.jpg\" alt=\"\" width=\"580\" height=\"761\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/11\/unit-circle-chart-radians.jpg 580w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/11\/unit-circle-chart-radians-229x300.jpg 229w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/p>\n<p>The Cartesian point (1, 0), is 0 or 360 degrees (0\u00b0 or 360\u00b0) and it corresponds to 0\u03c0 or 2\u03c0 radians. In p5.js, 2\u03c0 radians is also represented as a constant called <span class=\"description-text\"><a href=\"https:\/\/p5js.org\/reference\/#\/p5\/TWO_PI\">TWO_PI<\/a>, or alternatively as <a href=\"https:\/\/p5js.org\/reference\/#\/p5\/TAU\">TAU<\/a>.<\/span><\/p>\n<p>The Cartesian point (-1, 0), is 180 degrees (180\u00b0) and it corresponds to \u03c0 radians. In p5.js, \u03c0 radians is also represented as a constant called <a href=\"https:\/\/p5js.org\/reference\/#\/p5\/PI\">PI<\/a>.<\/p>\n<p>The Cartesian point (0, 1), is 90 degrees (90\u00b0) and it corresponds to \u03c0\/2 radians. In p5.js, \u03c0\/2 radians is also represented as a constant called <a href=\"https:\/\/p5js.org\/reference\/#\/p5\/HALF_PI\">HALF_PI<\/a>.<\/p>\n<p>45 degrees (45\u00b0) corresponds to \u03c0\/4 radians. In p5.js, \u03c0\/4 radians is also represented as a constant called <a href=\"https:\/\/p5js.org\/reference\/#\/p5\/QUARTER_PI\">QUARTER_PI<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To understand how to code with circles in p5.js, it is necessary to have a foundation of knowledge about the parts of a circle and how they map to code. The first mapping is the name. Circles are actually called ellipses in p5.js and are produced using the ellipse() function, that takes in four (or &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/11\/28\/p5-js-understanding-circles\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">p5.js: Understanding circles<\/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":[47,35],"tags":[45,48,36],"class_list":["post-962","post","type-post","status-publish","format-standard","hentry","category-p5-js","category-processing-language","tag-javascript","tag-p5-js","tag-processing-py"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/962","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=962"}],"version-history":[{"count":10,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/962\/revisions"}],"predecessor-version":[{"id":975,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/962\/revisions\/975"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}