brhfl.com

Golfing in Eukleides

Eukleides is decidedly not a golfing language, but when a geometry-related question came up on PPCG, I had to give it a shot. Eukleides can be quite rigid; for starters it is very strongly typed. Functions are declared by what they intend to return, so while set would be the shortest way to declare a function, it can’t really be exploited (unless returning a set is, in fact, desired). Speaking of sets, one thing that is potentially golfable is ‘casting’ a point to a set. Given a point, p, attempting a setwise operation on p will fail because a point does not automatically cast to a set (strict typing). p=set(p) will overwrite point p with a single-item set containing the point that was p. If, however, it is okay to have two copies of the point in the set, p=p.p is three bytes shorter.

If user input is required, the command number("prompt") reads a number from STDIN. The string for the prompt is required, though it can be empty (""). Thus, if more than four such inputs (or empty strings for other purposes) are required, it saves bytes to assign a variable with a one-letter name to an empty string.

Whitespace is generally unavoidable, but I did come to realize that boolean operators do not need to be preceded by whitespace if preceded by a number. So, if a==7or a==6 is perfectly valid. aor a, 7ora, 7 or6 are all invalid, however. This may be an interpreter bug, but for the time being it is an exploitable byte.

Finally, loci. Loci are akin to for-loops that automagically make sets. Unfortunately, they don’t seem to care much for referencing themselves mid-loop, which meant that I couldn’t exploit how short of a construction they are compared to manually creating a set in a for-loop.

This was a fun exercise, and just goes to show that if you poke around at any language enough, you’ll find various quirks that may prove useful given some ridiculous situation or another.


Thoughts on pink pencils

I write a lot. I carry many bags. I’m untidy. I own and use many mechanical pencils. Some of them good, some of them bad, most of them pink. Here are my favorites.

Pentel Sharp Kerry
The Kerry is hands-down the best pencil I own, in a very practical sense. It’s not beautiful; its oddly shiny-and-gridded midsection that looks grippy but is too high to function as such is just gaudy. The pencil was introduced at least 30 years ago, but I can’t imagine that ever looked good. But it’s such a well-engineered, functional pencil that it’s hard to rail on too much. It’s kind of the pencil version of 70s Japanese ‘pocket pens’ in that it has a cap, and it ‘grows’ to a usable size when said cap is posted. A pencil introduces its own challenges, of course, so the cap actually has its own lead advance button (which contains the eraser) that interfaces with the main advance. The cap obviates the need for a retraction method, so the Kerry lives comfortably in a pocket or a bag. I don’t have a bag without a Kerry in it.
Uni Kuru Toga High Grade
Uni’s Kuru Toga is one of the most meaningful innovations in mechanical pencil mechanisms as far as I’m concerned. With a 2.0mm lead, or possibly even down to a 1.3mm, one might sharpen their lead with a purpose-built rasp or a lead pointer. Get much narrower, and your leads are all over the place. I use 4B whenever possible, and 2B otherwise, so this is less of a problem for me, but it’s still nice to find ways to mitigate problems. The Kuru Toga mechanism rotates the lead by a tiny amount every time you press it to paper. This mechanism is a bit ‘spongy’, perhaps, almost like a pencil with a suspension mechanism. I found it very easy to adjust to, but I’ve heard of others taking issue with it. The High Grade has an aluminum grip, which I like the weight and feel of, but others may prefer the rubber grip model.
Pilot Clutch Point
This pencil is fairly maligned, and I understand why. The aforementioned pencils (and the following pencils) all use an internal clutch mechanism, and have a straight lead sleeve. Straight lead sleeves are great for draughting; they slide perfectly along a straightedge. I, personally, don’t like them all that much for writing, however. The internal mechanism brings with it other advantages. Keeping the mechanism protected, and having the lead held straight by the sleeve before the clutch means far less breakage. The Pilot Clutch Point exposes the clutch right at the front of the pencil, and if you don’t treat it with respect, it will jam. Badly. But when it works, it has a nice, pointy mechanism that will hold the shortest bits of lead known to humankind. It may very well be my favorite pencil for everyday writing.
Pentel Sharp Pocket
The baby sister of the Kerry, I suppose? Much thinner, slightly shorter, and with a much more Biro-like cap that doesn’t really extend the pencil whatsoever. Great for attaching to a diary or the like. Works as well as any Pentel does. Very light.
Staedtler 775
I guess the pink one is only available in Korea, but it does exist, and one can find it on eBay, so I say it counts. The 775 is a classic draughting Staedtler. It has a retracting point, but you have to ram it in to get it to do so. This has its ups and downs, being a simple retraction system makes it incredibly steady when engaged. But it’s also hard to disengage, and you risk breaking the lead or bending the sleeve.
Zebra Color Flight
A really cheap pencil, but bonus points for coming in three shades of pink. It also has one other neat trick up its sleeve – much more eraser than the typical mechanical pencil, extended by rotating the advance. The plastic feels cheap, nothing to write home about. But considering how cheap they can be, the Zebra Color Flight pencils are actually pretty nice.


Eukleides

Despite having failed out of geometry in my younger days, it has become my favorite sort of recreational math. I think, back in elhi geometry, too much emphasis was placed on potential practical applications instead of just distilling it down to the reality that this was what math was before there was any sort of formal mathematical system. Geometry is hands-on, it’s playful, and as such I have come to really enjoy playing with it. As much as I enjoy doing constructions with a straightedge and compass, I occasionally poke around to see what tools exist on the computer as well. Recently, I stumbled across a very neat thing: Eukleides.

I’m drawn to Eukleides because it is a language for geometry, and not a mouse-flinging WYSIWYG virtual compass. This seems contradictory given my gushing about geometry being hands-on, and don’t get me wrong – I love a hands-on GUI circle-canvas too1. But sometimes (often times) my brain is in code-mode and it’s easier to express what I’m trying to do in words than to fiddle around with a mouse. And a task like ‘intersecting a couple of circles’ is far more conducive to writing out than, say, laying down an SVG illustration from scratch.

a b c d e bi

There you have one of the first constructions learned by anyone studying geometry – bisecting an angle with three arcs (or, full-blown circles in this case). Angle ∠abc is bisected by segment bbi. Here’s the code:

% Percent signs are comments
a=point(7,0); b=point(0,0) % Semicolons and newlines separate commands
a b c triangle 5,-40°
g=circle(b,3)
d=intersection(g,a.b); e=intersection(g,b.c)
d=d[0]; e=e[0] % Intersections return sets (arrays), extract the points
h=circle(d,3); i=circle(e,3)
bi=intersection(h,i)
bi=bi[1]
label
  a 0°; b 180°; c 40° % Label the points
  d -40° gray; e 90° gray
  bi 20°
  a,b,bi; bi,b,c 1.5 % Make the angle markers
end
draw
  c.b.a
  g lightgray; h gray
  i gray
  bi
  b.bi
end

Note that the code originally used shades of grey, I shifted these around to match my site’s colors when I converted the resulting EPS file to SVG. The code is pretty straightforward: define some points, make an angle of them, draw a circle, intersect segments ab and bc, make some more circles, intersect where the circles meet, and boom – a bisected angle. The language reminds me a bit of GraphViz/DOT – purpose-built for naturally expressing how things will be drawn.

We can actually prove that the construction works without even generating an image file. Removing the draw and label sections, and replacing them with some print (to stdout) statements calling angle measurement functions:

a=point(7,0); b=point(0,0)
a b c triangle 5,-40°
g=circle(b,3)
d=intersection(g,a.b); e=intersection(g,b.c)
d=d[0];e=e[0]
h=circle(d,3); i=circle(e,3)
bi=intersection(h,i)
bi=bi[1]
%%%%%%%% New content starts here:
print angle(a,b,c)
print angle(a,b,c)/2
print angle(a,b,bi)
print angle(bi,b,c)

We get ∠abc, ∠abc/2, ∠abbi, and ∠bcbi. The last three of these should be equal, and:

40
20
20
20

…they are! We can also do fun things like dividing the circumference of a circle by its diameter:

print (perimeter(circle(point(0,0),4))/8)

…to get 3.14159. Very cool. There are a few improvements I would like to see. Notably, while you can label points etc. with their names, I can’t find a way to add arbitrary labels, or even labels based on functions. So you can’t (seemingly) label a line segment with its length, or an angle with its measure. Also, the interpreter wants ISO 8859-1 encoding, and it uses the degree symbol2 (°) to represent angle measures. This gets all flippy-floppy when moving to UTF-8, and in fact if I forget to convert a file to ISO 8859-1, I’ll get syntax errors if I use degree symbols. Finally, it only outputs to EPS; native SVG would be incredibly welcome.

Eukleides is a lot of fun to play with, and it’s worth mentioning that it has looping and conditionals and the like, so it is programmable and not just computational or presentational. Presumably some pretty interesting opportunities are thus opened up.