home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 December
/
simtel1292_SIMTEL_1292_Walnut_Creek.iso
/
msdos
/
ddjmag
/
ddj8907.arc
/
MURRAY.LST
< prev
next >
Wrap
File List
|
1989-06-01
|
13KB
|
536 lines
_LINE-OF-BEST-FIT_
by William Murray and Chris Pappas
[LISTING ONE]
mfit17.obj : mfit17.c mfit17.h
CL /c /Lp /Zp /Od /G2sw /W2 mfit17.c
mfit17.res : mfit17.rc mfit17.ico mfit17.h mfit17.ptr
RC -r mfit17
mfit17.exe : mfit17.obj mfit17.def mfit17.res
link mfit17, /align:16, NUL, OS2, mfit17
rc mfit17.res
[LISTING TWO]
;MFIT17.DEF for C Compiling
NAME mfit17
EXPORTS About
EXPORTS LineDiaProc
EXPORTS GraphicProc
PROTMODE
HEAPSIZE 2048
STACKSIZE 9216
[LISTING THREE]
#define ID_RESOURCE 10
#define ID_ABOUT 15
#define ID_OK 20
#define ID_CANCEL 25
#define ID_INPUT 30
#define IDM_ABOUT 40
#define IDM_INPUT 45
#define IDM_LINEINPUT 55
#define DM_XAXIS 470
#define DM_YAXIS 475
#define DM_TITLE 480
#define DM_X1 485
#define DM_Y1 490
[LISTING FOUR]
#include <os2.h>
#include "mfit17.h"
POINTER ID_RESOURCE mfit17.ico
POINTER IDP_POINTER mfit17.ptr
MENU ID_RESOURCE
BEGIN
SUBMENU "Mouse_Data",IDM_LINEINPUT
BEGIN
MENUITEM "About...", IDM_ABOUT
MENUITEM "Program Data...", IDM_INPUT
END
END
DLGTEMPLATE ID_ABOUT
BEGIN
DIALOG "",ID_ABOUT,50,300,180,80,FS_DLGBORDER
BEGIN
CTEXT "Mouse Data Input Program",-1,2,60,176,10
CTEXT "by William H. Murray & Chris H. Pappas",-1,2,45,
176,10
CONTROL "OK",ID_OK,75,10,32,14,WC_BUTTON,BS_PUSHBUTTON
|BS_DEFAULT|WS_GROUP|WS_TABSTOP|WS_VISIBLE
END
END
DLGTEMPLATE ID_INPUT LOADONCALL MOVEABLE DISCARDABLE
BEGIN
DIALOG "Mouse Program Information",ID_INPUT,76,247,148,193,
FS_NOBYTEALIGN|FS_DLGBORDER|WS_VISIBLE|
WS_CLIPSIBLINGS|WS_SAVEBITS,FCF_TITLEBAR
BEGIN
CONTROL "Mouse Program Labels",257,2,92,145,72,WC_STATIC,
SS_GROUPBOX|WS_GROUP|WS_VISIBLE
CONTROL "X & Y Maximums",259,2,42,145,42,WC_STATIC,
SS_GROUPBOX|WS_GROUP|WS_VISIBLE
CONTROL "Enter Title:",260,5,133,51,8,WC_STATIC,SS_TEXT
|DT_LEFT|DT_TOP|WS_GROUP|WS_VISIBLE
CONTROL " x-axis:",261,5,118,54,8,WC_STATIC,SS_TEXT
|DT_LEFT|DT_TOP|WS_GROUP|WS_VISIBLE
CONTROL " y-axis:",262,5,102,50,8,WC_STATIC,SS_TEXT
|DT_LEFT|DT_TOP|WS_GROUP|WS_VISIBLE
CONTROL "Mouse Data Points",DM_TITLE,54,132,89,13,
WC_ENTRYFIELD,ES_LEFT|ES_AUTOSCROLL|ES_MARGIN
|WS_TABSTOP|WS_VISIBLE
CONTROL "x - axis label",DM_XAXIS,54,117,89,13,
WC_ENTRYFIELD,ES_LEFT|ES_AUTOSCROLL|ES_MARGIN
|WS_TABSTOP|WS_VISIBLE
CONTROL "y - axis label",DM_YAXIS,54,101,89,13,
WC_ENTRYFIELD,ES_LEFT|ES_AUTOSCROLL|ES_MARGIN
|WS_TABSTOP|WS_VISIBLE
CONTROL " x:",273,25,57,20,8,WC_STATIC,SS_TEXT|DT_LEFT|
DT_TOP|WS_GROUP|WS_VISIBLE
CONTROL " y:",283,75,57,20,8,WC_STATIC,SS_TEXT|DT_LEFT|
DT_TOP|WS_GROUP|WS_VISIBLE
CONTROL "500",DM_X1,50,57,20,15,WC_ENTRYFIELD,ES_LEFT
|ES_AUTOSCROLL|ES_MARGIN|WS_TABSTOP|WS_VISIBLE
CONTROL "400",DM_Y1,100,57,20,15,WC_ENTRYFIELD,ES_LEFT
|ES_AUTOSCROLL|ES_MARGIN|WS_TABSTOP|WS_VISIBLE
CONTROL "OK",ID_OK,17,13,24,14,WC_BUTTON,BS_PUSHBUTTON
|WS_TABSTOP|WS_VISIBLE
CONTROL "Cancel",ID_CANCEL,101,13,34,14,WC_BUTTON,
BS_PUSHBUTTON|WS_TABSTOP|WS_VISIBLE
END
END
[LISTING FIVE]
/* Cached-PS Window Platform for PM Graphics */
/* (c) William H. Murray and Chris H. Pappas, 1989 */
#define INCL_PM
#include <os2.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mfit17.h"
MRESULT EXPENTRY About(HWND,USHORT,MPARAM,MPARAM);
MRESULT EXPENTRY LineDiaProc(HWND,USHORT,MPARAM,MPARAM);
MRESULT EXPENTRY GraphicProc(HWND,USHORT,MPARAM,MPARAM);
#define maxpts 100
HPS hps;
HWND hfrm,hcnt;
QMSG qmsg;
long ptxorg[maxpts];
long ptyorg[maxpts];
long xaxismax=500;
long yaxismax=400;
char title[80]="Mouse Data Points";
char xstring[80]="x - axis label";
char ystring[80]="y - axis label";
POINTL mousepos;
main ()
{
static CHAR pszClassName[]="FirstClass";
HAB hab;
HMQ hmq;
HDC hdc;
SIZEL page;
ULONG flFrameFlags=FCF_SYSMENU|FCF_TITLEBAR|
FCF_SIZEBORDER|FCF_MINMAX|
FCF_ICON|FCF_MENU;
ULONG flFrameStyle=WS_VISIBLE;
hab=WinInitialize(0);
hmq=WinCreateMsgQueue(hab,0);
WinRegisterClass(hab,pszClassName,GraphicProc,
CS_SIZEREDRAW,0);
hfrm=WinCreateStdWindow(HWND_DESKTOP,flFrameStyle,
&flFrameFlags,pszClassName,
"Cached-PS PM Graphics",
0L,NULL,ID_RESOURCE,&hcnt);
hdc=WinOpenWindowDC(hcnt);
hps=GpiCreatePS(hab,hdc,&page,PU_PELS|GPIA_ASSOC);
WinSetWindowPos(hfrm,NULL,10,10,610,470,SWP_SIZE|
SWP_MOVE|SWP_SHOW);
while (WinGetMsg(hab,&qmsg,NULL,0,0))
WinDispatchMsg(hab,&qmsg);
WinDestroyWindow(hfrm);
WinDestroyMsgQueue(hmq);
WinTerminate(hab);
return 0;
}
MRESULT EXPENTRY About(hwnd,messg,parm1,parm2)
HWND hwnd;
USHORT messg;
MPARAM parm1,parm2;
{
switch (messg)
{
case WM_COMMAND:
switch (LOUSHORT(parm1))
{
case ID_OK:
WinDismissDlg(hwnd,TRUE);
return 0;
}
break;
}
return (WinDefDlgProc(hwnd,messg,parm1,parm2));
}
MRESULT EXPENTRY LineDiaProc(hwnd,messg,parm1,parm2)
HWND hwnd;
USHORT messg;
MPARAM parm1;
MPARAM parm2;
{
short X1;
short Y1;
switch (messg)
{
case WM_COMMAND:
switch (LOUSHORT (parm1))
{
case ID_OK:
WinQueryDlgItemText(hwnd,
DM_TITLE,
80,
title);
WinQueryDlgItemText(hwnd,
DM_XAXIS,
80,
xstring);
WinQueryDlgItemText(hwnd,
DM_YAXIS,
80,
ystring);
WinQueryDlgItemShort(hwnd,
DM_X1,
&X1,
1);
WinQueryDlgItemShort(hwnd,
DM_Y1,
&Y1,
1);
xaxismax=(long) X1;
yaxismax=(long) Y1;
WinDismissDlg(hwnd,TRUE);
return 0;
case ID_CANCEL:
WinDismissDlg(hwnd,TRUE);
return 0;
}
break;
}
return WinDefDlgProc (hwnd,messg,parm1,parm2);
}
MRESULT EXPENTRY GraphicProc(hwnd,messg,parm1,parm2)
HWND hwnd;
USHORT messg;
MPARAM parm1,parm2;
{
static LONG ColorDataInfo[]={CLR_NEUTRAL,
RGB_BLACK,
CLR_BACKGROUND,
RGB_WHITE};
static HPOINTER linePtr;
static HWND hmenu;
HPS hgpi;
HDC hdc;
POINTL ptl;
GRADIENTL gradl;
RECTL rcl;
int i,alength,blength;
int lenxstring,lenystring,lentitle,
lenmaxxlabel,lenmaxylabel;
long ptxmax,ptymax,sumx1,sumy1,sumxy1,
sumxsqr1,deno1,numa1,numb1,
sumx2,sumy2,sumxy2,sumxsqr2,
deno2,numa2,numb2;
long ptxscaled[maxpts],ptyscaled[maxpts],maxx;
char maxxlabel[4],maxylabel[4],
astring[10],bstring[10];
double a1,b1,a2,b2,y;
static int j=0;
static int npts=0;
/* Length of various strings */
lentitle=strlen(title);
lenxstring=strlen(xstring);
lenystring=strlen(ystring);
/* Convert maximum x value to a string */
itoa((int)xaxismax,maxxlabel,10);
lenmaxxlabel=strlen(maxxlabel);
/* Convert maximum y value to a string */
itoa((int)yaxismax,maxylabel,10);
lenmaxylabel=strlen(maxylabel);
/* Scale all x values in original array. */
for (i=0;i<npts;i++)
ptxscaled[i]=((ptxorg[i]-100)*xaxismax/400);
/* Scale all y values in original array. */
for (i=0;i<npts;i++)
ptyscaled[i]=((ptyorg[i]-100)*yaxismax/300);
/* Calculate values for screen equation */
sumxsqr1=0;
sumx1=0;
sumy1=0;
sumxy1=0;
for (i=0;i<npts;i++)
{
sumxsqr1+=ptxscaled[i]*ptxscaled[i];
sumx1+=ptxscaled[i];
sumy1+=ptyscaled[i];
sumxy1+=ptxscaled[i]*ptyscaled[i];
}
deno1=(npts*sumxsqr1)-(sumx1*sumx1);
numa1=(sumxsqr1*sumy1)-(sumx1*sumxy1);
numb1=(npts*sumxy1)-(sumx1*sumy1);
/* Calculate values for drawing line on screen */
sumxsqr2=0;
sumx2=0;
sumy2=0;
sumxy2=0;
for (i=0;i<npts;i++)
{
sumxsqr2+=ptxorg[i]*ptxorg[i];
sumx2+=ptxorg[i];
sumy2+=ptyorg[i];
sumxy2+=ptxorg[i]*ptyorg[i];
}
deno2=(npts*sumxsqr2)-(sumx2*sumx2);
numa2=(sumxsqr2*sumy2)-(sumx2*sumxy2);
numb2=(npts*sumxy2)-(sumx2*sumy2);
switch (messg)
{
case WM_CREATE:
linePtr=WinLoadPointer(HWND_DESKTOP,
NULL,IDP_POINTER);
return 0;
case WM_BUTTON1DOWN:
GpiSetMarker(hps,MARKSYM_PLUS);
GpiSetColor(hps,CLR_BLUE);
mousepos.x=(LONG) LOUSHORT(parm1);
mousepos.y=(LONG) HIUSHORT(parm1);
if(mousepos.x>99 & mousepos.x<500 &
mousepos.y>99 & mousepos.y<400)
{
ptxorg[j]=mousepos.x;
ptyorg[j]=mousepos.y;
j++;
npts=j;
GpiMarker(hps,&mousepos);
}
return TRUE;
case WM_MOUSEMOVE:
WinSetPointer(HWND_DESKTOP,linePtr);
return 0;
case WM_BUTTON2DOWN:
GpiSetColor(hps,CLR_RED);
j=0; /*reset pointer */
maxx=0;
/* Slope and intercept for org. and scaled */
a1=(double) numa1/deno1;
b1=(double) numb1/deno1;
a2=(double) numa2/deno2;
b2=(double) numb2/deno2;
/* Starting point for line of best fit */
for (i=99;i<510;i++)
{
y=a2+(b2*i);
if(y>99.0 && y<410.0)
{
ptl.x=i;
ptl.y=(long) y;
break;
}
}
GpiMove(hps,&ptl);
/* Ending point for line of best fit */
for (i=510;i>99;i--)
{
y=a2+(b2*i);
if(y>99.0 && y<410.0)
{
ptl.x=i;
ptl.y=(long) y;
break;
}
}
GpiLine(hps,&ptl);
/* Draw the equation to the screen. */
GpiSetColor(hps,CLR_WHITE);
ptl.x=100;
ptl.y=80;
GpiMove(hps,&ptl);
ptl.x=500;
ptl.y=55;
GpiBox(hps,DRO_FILL,&ptl,0L,0L);
GpiSetColor(hps,CLR_RED);
gcvt(a1,7,astring);
alength=strlen(astring);
gcvt(b1,7,bstring);
blength=strlen(bstring);
ptl.x=300-(LONG)(alength+blength+8)*4;
ptl.y=62;
GpiMove(hps,&ptl);
GpiCharString(hps,4L,"y = ");
GpiCharString(hps,(long) alength,astring);
GpiCharString(hps,3L," + ");
GpiCharString(hps,(long) blength,bstring);
GpiCharString(hps,3L,"(x)");
return TRUE;
case WM_SIZE:
if (hmenu==NULL)
hmenu=WinWindowFromID(WinQueryWindow(hwnd,
QW_PARENT,FALSE),FID_MENU);
return 0;
case WM_COMMAND:
{
switch (COMMANDMSG(&messg)->cmd)
{
case IDM_ABOUT:
if (WinDlgBox(HWND_DESKTOP,hwnd,About,
NULL,ID_ABOUT,NULL))
WinInvalidateRect(hwnd,NULL,FALSE);
return 0;
case IDM_INPUT:
if (WinDlgBox(HWND_DESKTOP,hwnd,LineDiaProc,
NULL,ID_INPUT,NULL))
WinInvalidateRect(hwnd,NULL,FALSE);
return 0;
return 0;
}
}
break;
case WM_PAINT:
hgpi=WinBeginPaint(hwnd,NULL,NULL);
GpiCreateLogColorTable(hgpi,LCOL_RESET,
LCOLF_INDRGB,0L,4L,ColorDataInfo);
GpiErase(hgpi);
/*--------- your routines below ----------*/
GpiSetColor(hgpi,CLR_BLACK);
/* Draw X and Y coordinate axis */
ptl.x=99;
ptl.y=410;
GpiMove(hgpi,&ptl);
ptl.y=99;
GpiLine(hgpi,&ptl);
ptl.x=510;
GpiLine(hgpi,&ptl);
/* Draw Y axis tic marks */
ptl.y=130;
for (i=0;i<10;i++)
{
ptl.x=95;
GpiMove(hgpi,&ptl);
ptl.x=99;
GpiLine(hgpi,&ptl);
ptl.y+=30;
}
/* Draw X axis tic marks */
ptl.x=140;
for (i=0;i<10;i++)
{
ptl.y=99;
GpiMove(hgpi,&ptl);
ptl.y=95;
GpiLine(hgpi,&ptl);
ptl.x+=40;
}
GpiSetCharMode(hgpi,CM_MODE3);
/* Center and print line chart title */
ptl.y=410;
ptl.x=300-((LONG) (lentitle/2)*6);
GpiCharStringAt(hgpi,&ptl,(LONG) lentitle,title);
/* Center and print horizontal axis label */
ptl.y=40;
ptl.x=300-((LONG) (lenxstring/2)*6);
GpiCharStringAt(hgpi,&ptl,(LONG) lenxstring,
xstring);
/* Print horizontal axis maximum value */
ptl.y=82;
ptl.x=490;
GpiCharStringAt(hgpi,&ptl,(long) lenmaxxlabel,
maxxlabel);
/* Print vertical axis maximum value */
ptl.y=400;
ptl.x=70;
GpiCharStringAt(hgpi,&ptl,(long) lenmaxylabel,
maxylabel);
/* Center and print vertical axis label */
ptl.y=240-((LONG) (lenystring/2)*6);
ptl.x=70;
gradl.x=0;
gradl.y=90;
GpiSetCharAngle(hgpi,&gradl);
GpiCharStringAt(hgpi,&ptl,(LONG) lenystring,
ystring);
/*--------- your routines above ----------*/
WinEndPaint(hgpi);
break;
default:
return WinDefWindowProc(hwnd,messg,parm1,parm2);
}
return 0;
}