Rendering - Lines
A line is another simple graphics primitive that can be drawn on WOWCube device screens with WOWCube API.
In geometry, a line can be defined as a straight one-dimensional figure that has no thickness and extends endlessly in both directions.
In computer graphics, however, a line is a segment of a geometrical line that connects two points in a shortest way, i.e. straight.
So in order to draw a line, we would want to provide coordinates of two points A and B, line thickness and the color of a line.
The coordinates define screen positions of each point.
Despite the classical definition, lines in computer graphics have non-zero thickness. A line drawing algorithm approximates a line segment on a discrete grid of pixels which WOWCube screen is. So in order for a line to be visible, it must be at least 1 pixel thick. You may think of it as of a width of a brush you use to draw the line.
The color of a line is defined as RGB value.
The RGB color model is an additive color model in which the red, green and blue primary colors of light are added together in various ways to reproduce a broad spectrum of colors. The name of the model comes from the first letters of the three primary colors: Red, Green and Blue
Actual values of each primary color range from 0 to 255, the range that a single 8-bit byte can offer. Since higher value means brighter color, the (0,0,0) represents no color, or so-called transparent color, (1,1,1) gives solid black, while (255,255,255) gives white.
The example
In order to demonstrate line rendering, previous example - Rendering - Points will be slightly modified.
The algorithm stays the same, the only difference is that instead of rendering of stars as points, short line segments will be drawn.
First off, let's declare an array of 50 stars, almost exactly as in previous example, but with two extra properties, prevx and prevy. We need to points, A and B, to draw a line that connects them, so (prevx,prevy) will be a point A and (x, y) will be a point B.
Then, we need to initialize this array with initial values. To do that, we use addStar() function in the InitializeResources():
The function that adds a start looks like this:
In the function, we initialize initial velocity vector (vx,vy) with a random value to ensure that our stars will be shooting at random directions. Then we set initial positions of both points to (0,0) and add it to the array.
Pay attention at the numStars variable. It is used to make sure the array will never be overloaded, even if AddStar() function is called more than 50 times.
The last step is to animate the stars. See the renderStarfield() function that is called every time the screen is rendered:
We iterate though the array of stars changing star position slightly outwards the center of a screen till the star is visible. Then we move it back to its initial position and repeat the process again.
In order to actually draw a line for each star, WOWCube API function GFX_drawLine is used: