Utils

Utility functions can be used across modules. To avoid name conflicts most of them are defined as static class functions (i.e. the class mainly serves as a namespace). Typically this class name is in the plural, e.g. "Points", "Dates" to ensure that existing class names like "Point", "Date" are not in conflict with the namespace.

Cycle

Cycles simplify to switch between values in a cyclic way.

Dates

A tricky problem is to iterate over years, months, and days to label timelines and calendars in a consistent way. This can lead to problems with standard (CET) and summer time (CEST). To illustrate the problem look at the following example. Although march has 31 days the formatted UTC string shows "30.3". Also note that the standard new Date() constructor uses a zero-based month:

The following iterators guarantee that correct labels are generated:

Sets

Unfortunately the common set operations of other languages are missing in JavaScript. Therefore we use a Sets helper class with static methods:

Polygon

An intersection of polygons is needed to compute the overlap of rotated rectangles. We are using the library jspolygon.js but provide a more convenient API that is compatible with arrays of absolute points.

To detect intersection with another Polygon object, the instance method uses the Separating Axis Theorem. It returns false if there is no intersection, or an object if there is. The object contains 2 fields, overlap and axis. Moving the other polygon by overlap on axis will get the polygons out of intersection.

The following triangles show an overlap. Moving the triangle along the red line would remove the overlap.

Canvas not supported

References