mgsLib  1.3
Mermaja's Graphic Screen. A simple C library to build Windows graphic applications from console programs.
functions.c

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>
#include "mgsLib.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];
// Create a demo screen
if ((scr = mgsCreateScreen(800, 600, "800 x 600 Screen", 0x204020, NULL)) < 0)
exit(EXIT_FAILURE);
// Draw two arrows to show height and width
mgsLine(scr, 5, 80, 795, 80, 3, MGS_CWHITE);
mgsLine(scr, 80, 5, 80, 595, 3, MGS_CWHITE);
mgsRegPolygonVC(scr, 3, 80, 0, 80, 9, MGS_CWHITE);
mgsRegPolygonVC(scr, 3, 80, 599, 80, 590, MGS_CWHITE);
mgsRegPolygonVC(scr, 3, 0, 80, 9, 80, MGS_CWHITE);
mgsRegPolygonVC(scr, 3, 799, 80, 790, 80, MGS_CWHITE);
// Fit the text size and write the corner coordinates
mgsSetFont(scr, 20, 0xFFC0C0, MGS_TRANSPARENT);
mgsPuts(scr, 5, 5, "(0, 0)");
mgsPuts(scr, 5, 577, "(0, 599)");
mgsPuts(scr, 735, 5, "(799, 0)");
mgsPuts(scr, 718, 577, "(799, 599)");
// Fit the text size and write the height and width
mgsSetFont(scr, 30, 0xFFA0A0, MGS_TRANSPARENT);
mgsPuts(scr, 380, 88, "800");
mgsPuts(scr, 90, 286, "600");
// Show the created image
// Go to the next stage after pressing S
puts("Press S to continue");
while(!mgsKeyPressed('S'));
// Destroy the first demo screen
// Create two new screens to plot the functions
if ((scr = mgsCreateScreen(800, 600, "mgsLib Demo - Using Points", mgsRGBColor(200, 255, 200), NULL)) < 0)
exit(EXIT_FAILURE);
if ((scr2 = mgsCreateScreen(800, 600, "mgsLib Demo - Using Lines", mgsRGBColor(200, 200, 255), NULL)) < 0)
exit(EXIT_FAILURE);
// Draw the x and y axis on both screens
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);
// Calculate the values for sin(x) between -10.,0 and 10.0 an
// adjusted (y axis) between -1.1 and 1.1
adjustFunction(vector, 800, 600, -10.0, 10.0, -1.1, 1.1, sin);
// Plot the points on one screen
for(i = 0; i < 800; i++)
mgsPoint(scr, i, vector[i], MGS_CBLUE);
// and draw lines on the other
for(i = 1; i < 800; i++)
mgsLine(scr2, i - 1, vector[i - 1], i, vector[i], 1, MGS_CBLUE);
// Calculate the values for x*sin(x) between -10.,0 and 10.0 an
// adjusted (y axis) between -10.1 and 10.1
adjustFunction(vector, 800, 600, -10.0, 10.0, -10.1, 10.1, xsin);
// Plot the points on one screen
for(i = 0; i < 800; i++)
mgsPoint(scr, i, vector[i], MGS_CRED);
// and draw lines on the other
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");
// Update both screens
// And wait for S to be first released -remember we pressed it before-
// and then pressed to exit.
while(mgsKeyPressed('S'));
puts("Press S to continue");
while(!mgsKeyPressed('S'));
}
// Adjust a funcion defined on an interval to the screen coordinates and width(nPoints) and height(maxValY)
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;
// We calculate the intervals once. We adjust input from 0, nPoints - 1 to xMin, xMax
// and output from yMin, yMax to maxValY - 1, 0
maxValY --;
nPoints--;
xdif = xMax - xMin;
ydif = yMax - yMin;
for (i = 0; i <= nPoints; i++) {
// Calculate the x corresponding to each i
x = xMin + (double)i * xdif / (double)nPoints;
// Calculate y = f(x) where f is the given function
y = func(x);
// Calculate the ploting value corresponding to y
points[i] = maxValY - (y - yMin) * (double)maxValY / ydif;
}
}
// Simply the x * sin(x) function
double xsin(double x)
{
return x * sin(x);
}