home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
299_01
/
plot.c
< prev
next >
Wrap
Text File
|
1989-12-30
|
10KB
|
301 lines
/****************************************************************************/
/* plot.c */
/* (c) by Ronald Michaels. This program may be freely copied, modified, */
/* transmitted, or used for any non-commercial purpose. */
/* this file contains the functions necessary */
/* to display the bp.c error graph */
/****************************************************************************/
#include <stdio.h>
#include <fg.h>
#include <stdlib.h>
#include <dos.h>
#include"plot.h"
#define U(x) (unsigned int)(x) /* type conversion */
void build_grid(void);
void prt_scr(void);
void menu(int);
void title(int);
void printer_msg(int);
int printer_status(void);
void peek(unsigned,unsigned,void *,int);
int x_base = 59;
int y_base = 23;
int x_div = 60;
int y_div = 30;
static fg_line_t graph_line; /* holds the coordinates for graph line */
/****************************************************************************/
/* init_graph */
/* this function initialises graphics and draws grid */
/****************************************************************************/
void init_graph()
{
fg_pbox_t graph_box; /* an array of 4 integers */
fg_line_t grid_line; /* array to hold line coordinates */
int x_coord;
int y_coord;
int i, /* X-axis counter */
j; /* Y-axis counter */
if(fg_init_herc()==FG_NULL){
fputs("UNABLE TO OPEN GRAPHICS DEVICE\n",stderr);
exit (1);
}
title(FG_WHITE);
graph_box[FG_X1] = x_base;
graph_box[FG_Y1] = y_base;
graph_box[FG_X2] = x_base + 10 * x_div;
graph_box[FG_Y2] = y_base + 10 * y_div;
fg_drawbox(FG_WHITE,FG_MODE_SET,~0,FG_LINE_SOLID,
graph_box,fg_displaybox);
/* put in intersections */
for(i=0;i<11;i++){ /* counts along x axis */
for(j=0;j<11;j++){ /* counts along y axis */
x_coord = x_base + x_div * i;
y_coord = y_base + y_div * j;
grid_line[FG_X1] = x_coord - 2;
grid_line[FG_X2] = x_coord + 2;
grid_line[FG_Y1] = y_coord ;
grid_line[FG_Y2] = y_coord ;
fg_drawline(FG_WHITE,FG_MODE_SET,~0,FG_LINE_SOLID,grid_line);
grid_line[FG_X1] = x_coord ;
grid_line[FG_X2] = x_coord ;
grid_line[FG_Y1] = y_coord - 2;
grid_line[FG_Y2] = y_coord + 2;
fg_drawline(FG_WHITE,FG_MODE_SET,~0,FG_LINE_SOLID,grid_line);
}
}
fg_puts(FG_WHITE,FG_MODE_SET,~0,FG_ROT90,20,150,
"error",fg_displaybox);
fg_puts(FG_WHITE,FG_MODE_SET,~0,FG_ROT0,5,5,
"iter",fg_displaybox);
}
/****************************************************************************/
/* title */
/* this function prints title of graph */
/****************************************************************************/
void title(
int color
)
{
fg_puts(color,FG_MODE_SET,~0,FG_ROT0,x_base+10,y_base+10*y_div+10,
"Back Propagation Generalised Delta Rule Learning Program",
fg_displaybox);
}
/****************************************************************************/
/* close_graph */
/* this function clears the screen and resets to text */
/****************************************************************************/
void close_graph()
{
int choice; /* program control choice */
printf("%c",7); /* sound bell */
title(FG_BLACK);
for(;;){
menu(FG_WHITE);
choice = getch();
switch(choice){
case 'p':
case 'P':
/* print screen */
/* replace title */
menu(FG_BLACK);
printer_msg(FG_WHITE);
getch();
printer_msg(FG_BLACK);
title(FG_WHITE);
prt_scr();
title(FG_BLACK);
break;
case 'v':
case 'V':
/* replace title */
menu(FG_BLACK);
title(FG_WHITE);
getch();
title(FG_BLACK);
break;
case 'q':
case 'Q':
fg_term();
return;
default:
break;
}
}
}
/****************************************************************************/
/* set_scales */
/* this function takes the maximum values for X and Y and scales the axes */
/****************************************************************************/
void set_scales(
double error,
int iter,
double *x_scale,
double *y_scale
)
{
double y_max, y_step, y_val;
double x_max, x_step, x_val;
char buff[5]; /* holds the string conversion of scale values */
int i, /* X-axis counter */
j; /* Y-axis counter */
/* find the smallest integer value greater than error */
for(y_max=0.0;y_max<error;y_max++);
*y_scale = y_max;
y_step = y_max/10.0;
for(j=0;j<11;j++){
y_val = ((double)j)*y_step;
sprintf(buff,"%03.1f",y_val);
fg_puts(FG_WHITE,FG_MODE_SET,~0,FG_ROT0,25,y_base+y_div*j-5,
buff,fg_displaybox);
}
/* find the smallest round hundreds value greater than iter */
for(x_max=0.0;x_max<((double)iter);x_max+=100.0);
*x_scale = x_max;
x_step = x_max/10.0;
for(i=0;i<11;i++){
x_val = ((double)i)*x_step;
sprintf(buff,"%3.0f",x_val);
fg_puts(FG_WHITE,FG_MODE_SET,~0,FG_ROT0,x_base+x_div*i-15,5,
buff,fg_displaybox);
}
graph_line[FG_X1] = x_base;
graph_line[FG_Y1] = (int)((error/y_max)*(double)(10*y_div))+y_base;
}
/****************************************************************************/
/* point */
/* this function plots a line from previous value to new value */
/****************************************************************************/
void point(
double error,
int iter,
double x_max,
double y_max
)
{
graph_line[FG_X2] = (int)(((double)iter/x_max)*(double)(10*x_div))+x_base;
graph_line[FG_Y2] = (int)((error/y_max)*(double)(10*y_div))+y_base;
fg_drawline(FG_WHITE,FG_MODE_SET,~0,FG_LINE_SOLID,graph_line);
graph_line[FG_X1] = graph_line[FG_X2];
graph_line[FG_Y1] = graph_line[FG_Y2];
}
/****************************************************************************/
/* menu */
/* this function displays menu */
/****************************************************************************/
void menu(
int color /* color of letters */
)
{
fg_puts(color,FG_MODE_SET,~0,FG_ROT0,x_base+10,y_base+10*y_div+10,
"View Print Quit",
fg_displaybox);
}
/****************************************************************************/
/* printer_msg */
/* this function prints a printer warning */
/****************************************************************************/
void printer_msg(
int color
)
{
fg_puts(color,FG_MODE_SET,~0,FG_ROT0,x_base+10,y_base+10*y_div+10,
"CHECK PRINTER press any key when ready",
fg_displaybox);
}
/****************************************************************************/
/* prt_scr */
/* this function prints out video memory to a Star printer */
/* if you want to print 8 or 9 pin graphics you will need to replace fputc */
/* with a function that does not convert \n to \r\n */
/***********************************************************