This blog post explains how computers represent signed integers using two's complement, where negative numbers are stored by inverting all bits of the positive number and adding 1 (e.g. -13 becomes 11110011). This system simplifies arithmetic operations and avoids dual representations of zero.This can be …
The following code implements a Quadtree in Python (see the previous blog post for a description of quadtrees). There are three classes: Point represents a point in two-dimensional space, with an optional "payload" (data structure associating the Point with more information, for example the identity …
The quadtree data structure is a convenient way to store the location of arbitrarily-distributed points in two-dimensional space. Quadtrees are often used in image processing and collision detection. A quadtree works by recursively partitioning a two-dimensional space into regions represented by nodes which are allowed …
The code below, torus.py, defines a class Torus for drawing an SVG image of a torus. The Torus class itself is a subclass of Shape, a more general class for depicting 3D objects in an SVG image, defined in shape.py. A usage example is given …
This is a simple Python function to generate a clock face indicating a specified time. It can be used in the script below to generate 1, 2, 4 or 6 faces indicating random times to teach children how to tell the time. Run this script …
The following code plots a two-dimensional object and its bounding box for several rotations about an arbitrary point. Three types of bounding box are considered: (1) the original bounding box, rotated by the same amount as the object, (2) the bounding box to that bounding …
The recurrence devised by Jean-Michel Muller[1] and described in a slightly modified form by William Kahan in a 2006 monograph[2] provides an interesting example of how the finite precision of floating point arithmetic can lead to wildly incorrect results in some circumstances. Consider the function …
A set of points drawn from a uniform distribution on a two-dimensional domain typically display clustering: For computer graphics applications, it is often useful to obtain a sample of random points such that no one point is closer than some pre-determined distance from any other …
A trie is an ordered tree data structure often used to store word lists, for example, for auto-complete applications. Each node in the tree is associated with a letter; the association of nodes with each other (parents, children, grandchildren, etc.) orders the letters into words. …
The standard way to bin a large array to a smaller one by averaging is to reshape it into a higher dimension and then take the means over the appropriate new axes. The following function does this, assuming that each dimension of the new shape …