Creating a Cairo-Inspired Graphics Library for MetaTrader 5 (Part 2): Points, Contours and the Path

Why this article was written
Part 1 ended with a triangle and a circle drawn by two separate functions. That approach does not scale: every new shape would need its own loops, its own bounds checking and its own edge handling, and none of that code would be reusable.
Part 2 removes that duplication by giving every shape the same description. Once geometry has one representation, a single fill routine in Part 3 can render all of them without knowing what it is drawing.
What is inside
The path model. Three levels, borrowed from Cairo: a point is a floating-point coordinate, a contour is a connected run of points, and a path is a set of contours. A ring is not a special shape — it is one path with an outer and an inner contour.
Path.mqh. The familiar drawing commands, implemented in MQL5:
MoveTo— start a new contourLineTo— extend the current oneClose— join the last point back to the first
Floating-point coordinates. Why anchors are stored as double and not
int. Sub-pixel positions are what make smooth edges possible at all, so this
decision in Part 2 is what Part 3's anti-aliasing depends on.
Bookkeeping. How contours are stored and grown, and how the path keeps track of its own bounds so the fill stage does not have to scan the whole surface.
An honest note on scope
By the end of Part 2 the library can describe geometry but cannot yet fill it. That is on purpose: filling is introduced in Part 3, once the thing it consumes has been properly defined.
State of the library after this part:
Color.mqh Part 1 the ARGB color type, uint 0xAARRGGBB
Surface.mqh Part 1 a pixel buffer bound to one chart object
Path.mqh Part 2 points, contours, MoveTo / LineTo / Close <- todayMore in this series
Building a real 2D renderer for MetaTrader 5 in pure MQL5 — anti-aliased shapes, transparency and gradients, with no DLLs and no external tools. About this series
- Part 1Why the terminal needs its own 2D-rendererSeptember 3, 2026
- Part 2Points, Contours and the Path — you are hereSeptember 10, 2026
- Part 3Edges and the First Filled ShapeSeptember 28, 2026
- Part 4Anti-Aliasing, Coverage and CompositingSeptember 28, 2026
- Part 5Fill Rules, Holes and BordersOctober 1, 2026
- Part 6Curves and Adaptive FlatteningOctober 23, 2026
- Part 7Arcs, Circles and Rounded RectanglesOctober 27, 2026
Continue reading on MQL5.com
The full article with complete source code is published on MQL5.com.


