One a Day One Liners with Python — Week 5
Computational Geometry
This week’s One Liners will be all about points, lines and planes as we have a go at implementing some common computational geometry routines as One Liners.
As always, feel free to leave a comment and check out the repo!
Feb 4, 2023
Generate a grid of rectangles 🟥 🟧 🟨
from argparse import Namespace as NS
r = 4
c = 6
w = 5
h = 10
grid = [[NS(l=j*w, r=j*w+w, t=i*h, b=i*h+h) for j in range(c)] for i in range(r)]
Phew, we made it through the week! I was thinking that I’d come up with something more mind bending for the final Computational Geometry One Liner. However, as simplistic as this one is, of all the One Liners from this week, I’ve used this in practical applications far more often than any of the others. Long live the grid.

Feb 3, 2023
Scale and rotation invariant triangle similarity test 📐
a = 6, 8, 4
b = 7.5, 10, 5
sim = [
sorted([
(t[y]**2+t[z]**2-t[x]**2)/(2*t[y]*t[z])
for x,y,z in [[0,1,2],[1,2,0],[2,0,1]]
])
for t in [a, b]
]
assert sim[0] == sim[1]
Discussion
Here we use the Law of Cosines to compute the inner angles of the triangles a
and b
, which are defined by their side lengths. The tricky bit is in the inner for loop where we unpack x
, y
and z
from the three arrays. These variables are then used to access the side lengths and compute the inner angles.
Using the first set of indices for triangle a
we would arrive at (in radians):
(8^2 + 4^2 - 6^2) / (2 * 8 * 4) = 0.6875
And the second set of indices would give us:
(4^2 + 6^2 - 8^2) / (2 * 4 * 6) = -0.25
… and so on.
If all three inner angles of two triangles are the same, then they are said to be similar. By computing all three and sorting the results, we can then compare them. The sorting is important because the side length ratios may not be consistent in the description of each triangle.
Feb 2, 2023
Determine if two rectangles overlap ⧉
from argparse import Namespace as NS
a = NS(l=0, t=0, r=10, b=10)
b = NS(l=4, t=4, r=12, b=12)
intersects = \
max(min(a.l, a.r), min(b.l, b.r)) < min(max(a.l, a.r), max(b.l, b.r)) and \
max(min(a.t, a.b), min(b.t, b.b)) < min(max(a.t, a.b), max(b.t, b.b))
Discussion
Whether or not this qualifies as a One Liner is debatable, but it’s been an especially long day and I’m gonna go easy on myself and roll with it.
Feb 1, 2023
Find the intersection of two rectangles 🚦∩
from argparse import Namespace as NS
a = NS(l=0, t=0, r=3, b=2)
b = NS(l=1, t=1, r=5, b=3)
intersection = NS(l=max(a.l, b.l), t=max(a.t, b.t), r=min(a.r, b.r), b=min(a.b, b.b))
Discussion
This works pretty well, but it will return erroneous results in the event that the two rectangles don’t overlap at all. It would be better to check first if the two rectangles overlapped. Perhaps a test for overlap would make a nice One Liner for tomorrow 🤔
Jan 31, 2023
Find the centroid of a regular 2D polygon ⏺️
from math import cos, tau, sin
r = 5
n = 5
t = tau / n
pentagon = [(cos(t * i) * r, sin(t * i) * r) for i in range(0, n)]
centroid = [sum(col) / float(len(pentagon)) for col in zip(*pentagon)]
Discussion
Following on from yesterday’s One Liner and also building on our discovery of the zip
function from a couple weeks ago, we can easily sum and average each of the axis to find the center of a regular 2D polygon.
Jan 30, 2023
Generate geometry for an n-sided regular 2D polygon ▲ ⬣
from math import cos, tau, sin
r = 5
n = 6
t = tau / n
hexagon = [(cos(t * i) * r, sin((t * i) * r) for i in range(0, n)]
Discussion
Change n
to 3 and get a triangle, to 8 an octagon and so on… In this snippet, r
is the radius, n
is the number of sides and t
is the constant angle between each consecutive point. That is, tau
or 2 * π, divided by the number of sides.
Jan 29, 2023
Determine if a point is within a plane 👉🏻 ✈️
from argparse import Namespace
p = Namespace(x=10, y=10)
plane = Namespace(x1=0, x2=20, y1=0, y2=20)
point_in_plane = p.x >= plane.x and p.x <= plane.x2 and p.y >= plane.y1 and p.y <= plane.y2
Discussion
Nothing too clever about this, but it works! I’m not so sure about the use of Namespace
for declaring the geometric types, but for sake of simplicity they get the job done here. In real world situations these types would most likely be represented with classes.
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Build awareness and adoption for your tech startup with Circuit.