This example shows how to create screens and plot an draw on them. It has been used to generate the two first figures of the documentation as well as a plot of the same sin(x) and x*sin(x) using lines instead of points.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
void adjustFunction(unsigned int *points, int nPpoints, int maxValY, double xMin, double xMax, double yMin, double yMax, double (*func)(double));
double xsin(double x);
int main()
{
int scr, scr2, i;
int vector[800];
if ((scr =
mgsCreateScreen(800, 600,
"800 x 600 Screen", 0x204020, NULL)) < 0)
exit(EXIT_FAILURE);
mgsLine(scr, 5, 80, 795, 80, 3, MGS_CWHITE);
mgsLine(scr, 80, 5, 80, 595, 3, MGS_CWHITE);
mgsPuts(scr, 718, 577,
"(799, 599)");
puts("Press S to continue");
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
mgsLine(scr, 0, 299, 800, 299, 4, MGS_CGREEN);
mgsLine(scr, 399, 0, 399, 600, 4, MGS_CGREEN);
mgsLine(scr2, 0, 299, 800, 299, 4, MGS_CGREEN);
mgsLine(scr2, 399, 0, 399, 600, 4, MGS_CGREEN);
adjustFunction(vector, 800, 600, -10.0, 10.0, -1.1, 1.1, sin);
for(i = 0; i < 800; i++)
for(i = 1; i < 800; i++)
mgsLine(scr2, i - 1, vector[i - 1], i, vector[i], 1, MGS_CBLUE);
adjustFunction(vector, 800, 600, -10.0, 10.0, -10.1, 10.1, xsin);
for(i = 0; i < 800; i++)
for(i = 1; i < 800; i++)
mgsLine(scr2, i - 1, vector[i - 1], i, vector[i], 1, MGS_CRED);
puts("\nDrawing x * sin(x)\n");
puts("Drawing sin(x)\n");
puts("Press S to continue");
}
void adjustFunction(unsigned int *points, int nPoints, int maxValY, double xMin, double xMax, double yMin, double yMax, double (*func)(double))
{
int i;
double x, y, xdif, ydif;
maxValY --;
nPoints--;
xdif = xMax - xMin;
ydif = yMax - yMin;
for (i = 0; i <= nPoints; i++) {
x = xMin + (double)i * xdif / (double)nPoints;
y = func(x);
points[i] = maxValY - (y - yMin) * (double)maxValY / ydif;
}
}
double xsin(double x)
{
return x * sin(x);
}