{"id":56,"date":"2012-08-03T11:59:48","date_gmt":"2012-08-03T16:59:48","guid":{"rendered":"http:\/\/www.playwithlua.com\/?p=56"},"modified":"2012-08-03T11:59:48","modified_gmt":"2012-08-03T16:59:48","slug":"points","status":"publish","type":"post","link":"http:\/\/www.playwithlua.com\/?p=56","title":{"rendered":"Points"},"content":{"rendered":"<p>I&#8217;ve been doing a lot of writing games in L&ouml;ve. Writing games usually involves a lot of code that deals with positions and directions and velocities. To make this easier to write, I made a little library to represent a point in a two-dimensional coordinate space.<\/p>\n<p>This is a little different for posts here because I&#8217;m not going to go into the code too much; I&#8217;m just going to show some examples of how to use it. Really, the motivation here is that I want to make other posts with code examples that use this, and so I should document it. Next up, my &#8220;map&#8221; class.<\/p>\n<p>If you just want the code, the canonical repository is <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/github.com\/randrews\/point.lua');\"  href=\"http:\/\/github.com\/randrews\/point.lua\">here<\/a>.<\/p>\n<p><!--more--><\/p>\n<h2>Basic stuff<\/h2>\n<p>First, the library is in one file, that I call <code>point.lua<\/code>. You can require it like any other module:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">local<\/span> point <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">require<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #ff6666;\">'point'<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>To create a point, you can call <code>point.new<\/code>:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">.<\/span>new<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Or use the shorthand of just calling the module:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Getting the components of the point is easy enough:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span>p<span style=\"color: #66cc66;\">.<\/span>x <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span>p<span style=\"color: #66cc66;\">.<\/span>y <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>It also intelligently handles equality:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\np2 <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span>p <span style=\"color: #66cc66;\">==<\/span> p2<span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>And can be converted to a string:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #0000aa;\">tostring<\/span><span style=\"color: #66cc66;\">&#40;<\/span>p<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #ff6666;\">&quot;(2, 3)&quot;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">print<\/span><span style=\"color: #66cc66;\">&#40;<\/span>p<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- prints (2, 3)<\/span><\/pre><\/div><\/div><\/div>\n\n<h2>Arithmetic<\/h2>\n<p>Points can be added and subtracted:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\np2 <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p <span style=\"color: #66cc66;\">+<\/span> p2 <span style=\"color: #66cc66;\">==<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p2 <span style=\"color: #66cc66;\">-<\/span> p <span style=\"color: #66cc66;\">==<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">-<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">-<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>They can also be multiplied, by points or numbers:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p <span style=\"color: #66cc66;\">*<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">-<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">==<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">-<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p <span style=\"color: #66cc66;\">*<\/span> <span style=\"color: #cc66cc;\">2<\/span> <span style=\"color: #66cc66;\">==<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">6<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<h2>Features for tile maps<\/h2>\n<p>Points whose components are both integers, like coordinates on a tile map, have some useful methods:<\/p>\n<h3>adjacent<\/h3>\n<p>Returns whether two points are next to each other, not counting diagonals:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p<span style=\"color: #66cc66;\">:<\/span>adjacent<span style=\"color: #66cc66;\">&#40;<\/span>point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">not<\/span> p<span style=\"color: #66cc66;\">:<\/span>adjacent<span style=\"color: #66cc66;\">&#40;<\/span>p<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<h3>ortho<\/h3>\n<p>Returns whether two points are on the same row or column:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p<span style=\"color: #66cc66;\">:<\/span>ortho<span style=\"color: #66cc66;\">&#40;<\/span>point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">not<\/span> p<span style=\"color: #66cc66;\">:<\/span>ortho<span style=\"color: #66cc66;\">&#40;<\/span>point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<h3>toward<\/h3>\n<p>If two points are on the same row or column, returns a new point representing the direction you have to move to get closer to the second one. For example, for these three points:<\/p>\n<pre>\r\n..........\r\n..........\r\n...A...B..\r\n..........\r\n..........\r\n..........\r\n...C......\r\n..........\r\n..........\r\n\r\n<\/pre>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">a<span style=\"color: #66cc66;\">:<\/span>toward<span style=\"color: #66cc66;\">&#40;<\/span>b<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- returns point(1, 0)<\/span>\nb<span style=\"color: #66cc66;\">:<\/span>toward<span style=\"color: #66cc66;\">&#40;<\/span>a<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- returns point(-1, 0)<\/span>\n&nbsp;\na<span style=\"color: #66cc66;\">:<\/span>toward<span style=\"color: #66cc66;\">&#40;<\/span>c<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- returns point(0, 1) assuming +y is downward<\/span>\n&nbsp;\nb<span style=\"color: #66cc66;\">:<\/span>toward<span style=\"color: #66cc66;\">&#40;<\/span>c<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- raises an error!<\/span><\/pre><\/div><\/div><\/div>\n\n<h2>Comparisons<\/h2>\n<p>Comparing two points means comparing both components:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">p <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\np2 <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">5<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">6<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\np3 <span style=\"color: #66cc66;\">=<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p <span style=\"color: #66cc66;\">&lt;<\/span> p2 <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p <span style=\"color: #66cc66;\">&lt;=<\/span> p3 <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> p2 <span style=\"color: #66cc66;\">&gt;<\/span> p3 <span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<h2>Other things<\/h2>\n<p>There are a few constants defined:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> point<span style=\"color: #66cc66;\">.<\/span>up <span style=\"color: #66cc66;\">==<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">-<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">assert<\/span><span style=\"color: #66cc66;\">&#40;<\/span> point<span style=\"color: #66cc66;\">.<\/span>east <span style=\"color: #66cc66;\">==<\/span> point<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Up, down, left, right, north, south, east, and west are all there, and they assume that going downward means an increase in Y (which is how L&ouml;ve coordinates work).<\/p>\n<p>I&#8217;ll probably be making changes and additions to the class periodically as I need things. The canonical repository is <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/github.com\/randrews\/point.lua');\"  href=\"http:\/\/github.com\/randrews\/point.lua\">here<\/a> and I&#8217;ll try to update this post when I add stuff.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been doing a lot of writing games in L&ouml;ve. Writing games usually involves a lot of code that deals with positions and directions and velocities. To make this easier to write, I made a little library to represent a point in a two-dimensional coordinate space. This is a little different for posts here because [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/56"}],"collection":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=56"}],"version-history":[{"count":0,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}