Bresenham's line algorithm can be used to determine which points in a raster grid to plot in order to achieve a appropriate visual approximation of a line segment.
The algorithm covers the rasterization of a line defined by the origin and endpoints in a coordinate space where the origin is in the upper left. Integer coordinates are presumed to map to pixel centers. Notably, the basic form of the algorithm only covers one octant of the circle: the one where the line has increasing X and Y coordinates but a negative slope with an absolute value less than 1. All other octants can be derived as simple transformations of this basic octant.
In psuedocode, this basic form looks like:
void DrawLine(Point origin, Point endpoint, Bitmap surface) {
deltaX = endpoint.X - origin.X
deltaY = endpoint.Y - origin.Y
error = 0
// Note the below fails for completely vertical lines.
deltaError = absoluteValue(deltaY / deltaX)
Y = origin.Y
for (X from origin.X to endpoint.X) {
surface.PlotPixel(X, Y)
error = error + deltaError
if (error >= 0.5) {
++Y;
error -= 1.0
}
}
}
The Rosetta Code website has a collection of concrete implementations in a variety of languages.
You may also be interested in Wu's line algorithm, which allows for anti-aliasing.