A simple BASIC program for constructing three-dimensional graphs

A simple BASIC program for constructing three-dimensional graphs

Computer Methods and Programs in Biomedicine 26 (1988) 283-288 283 Elsevier CPB 00906 Section II. Systems and programs A simple BASIC program for ...

328KB Sizes 5 Downloads 92 Views

Computer Methods and Programs in Biomedicine 26 (1988) 283-288

283

Elsevier CPB 00906

Section II. Systems and programs

A simple BASIC program for constructing three-dimensional graphs N e i l D. B e r m a n Division of Cardiology, Department of Medicine, University of Toronto, and Toronto Western Hospital Toronto, Ontario, Canada

This report describes a simple program for plotting three-dimensional curves and thus allowing a visual representation of the simultaneous interaction of three variables. Graphs can be displayed on the screen or printed using a graphics printer. The original data can be stored, printed, recalled and edited, as can the graph parameters. Completed graphs can also be stored for later review or printing. The program as outlined can accommodate up to 10 curves of up to 50 points each, but these limits can be easily increased. The program is written in BASIC, thus allowing it to be readily modified. As listed, the complete source code has 357 lines and occupies about 12 kilobytes when saved in compressed binary format. BASIC; Drug interactions; Three-dimensional graph plotting

1. Introduction We have been engaged in studies of the effects of antiarrhythmic drug combinations on the electrophysiologic parameters of cardiac cells [1]. In the course of these studies, we felt the need for a simple computer program that would enable us to construct three-dimensional graphs both to more readily visualize the effects of these drug combinations and also to provide us with a means of presenting the data graphically. This need was particularly apparent when we were studying drugs that differed greatly in their relative potencies (N.D. Berman and J.E. Loukides, unpublished observations). In these studies, the concentrations of the two drugs studied separately were markedly different and the drug combinations contained unequal concentrations of the two drugs. It was very difficult in the context of the two-dimensional graph to decide how to compare the curves Correspondence: N.D. Berman, Toronto Western Hospital, 399 Bathurst Street,

Toronto, Ontario M5T 2S8, Canada.

for the individual drugs and the combinations. Unfortunately, we were not able to find a suitable program for our needs. A recent publication concerning the plotting of quadratic equations [2] stimulated me to write the program presented here. This program was written in BASIC so as to be easily modifiable for our future needs and those of anyone else who may choose to use it. The areas for potential modification will be alluded to in the course of discussing the program. I will also illustrate its application to some of our previous data. The program consists of only 357 lines of source code and occupies 12 kilobytes of memory when stored in compressed binary format. It will run on any computer running the equivalent of DOS 2.0 or higher, with the capacity for high-resolution graphics and any BASIC interpreter that recognizes the appropriate graphic commands. The p r o gram executes rapidly when run with a BASIC interpreter on an AT-type computer. On a PC-type computer, program speed was still adequate but, if large quantities of data need to be incorporated

0169-2607/88/$03.50 © 1988 Elsevier Science Publishers B.V. (Biomedical Division)

284 into single graphs, the program should be compiled prior to use (using t h e / E option).

2. The program

2.1. Initializationa n d m e n u The program is initialized in lines 10-75 (Fig. 1) In line 50, the angles between the 3 axes and the true horizontal are defined. These particular angles were selected as providing an adequate separation between the X and Y axes while allowing them to be sufficiently long. The angle between the X and Y axes can be easily altered by redefining the value of AX and AY but this may lead to a need to shorten the axes to avoid extending below the b o t t o m of the screen. Line 55 defines the value of P A D , the conversion factor from degrees to radians. The following line uses this factor to convert the angles defined earlier. In line 65, the origin is defined. Other points could be used with appropriate modification of other variables. Line 75 establishes the arrays which have been dimensioned in this program to allow for up to 10 separate curves (C) of up to 50 points each (N). These limits can be readily varied. There are two arrays set up for the individual points. The first identifies each point in terms of its actual values OX, OY, and OZ, and the second contains the scaled values X, Y, and Z. The program then goes on to present the menu illustrated in Fig. 2. The menu is self-explanatory and the various options will be discussed in the order in which they appear on the menu.

2.2. Data input Any format can be used for data input. The most important requirement is to incorporate a means of correcting data that has been entered improperly. A full-screen approach is used for data input. Values are first entered into a string variable to allow the program to discriminate between zeros and null entries. Because the program can make this distinction, the editing phase can accept carriage returns to leave values unchanged but still allow the input of a change to a value of zero.

10 20 30

KEY OFF CLS DEF INT C, I-L, N

50

AX=-8: AY=5:AX=-90

55 60 65 75

RAD:3.141593/180 AX=AX*RAD: AY=AY*RAD: AZ=AZ*RAD H0=40: V0=150 DIM C(10),N(50),0X(10,50),0Y(10,50), 0Z(10,50) ,X(I 0,50) ,Y(10,50) ,Z( I0, 50) Fig. 1. Listing of the initialization part of the program.

Axis parameters can also be edited as illustrated in Section 3 (Figs. 6 and 7).

2.3. Data storage One is given the option of storing data a n d / o r the actual graph. D a t a is stored in a sequential file with an extention DAT. Graphs are stored in a file with an extention PIC. In order to store the graph, it is first drawn on the screen and then the screen memory is dumped into a file. In either case, the program first checks to see if a file of the same name already exists (by opening it in the ' A ' m o d e and measuring its length) and allows the option of overwriting it or selecting a different name under which to perform the current save. Both data and graphs that have been previously saved can be recalled. Recall is essentially the reverse of the storage procedure.

2.4. Displays The program allows for the display of data either recently entered or recalled from a file. The data displayed can be edited, as can the axis parameters. The other option is to display a graph. If there is data in memory, the program will con-

I .ENTER NEW DATA

2.STORE DATA/GRAPH 3.RECALL DATA/GRAPH 4.D I SPLAY DATA/GRAPH 5.PRINT DATA/GRAPH 6.CLEAR FOR NEW GRAPH 7 .QUIT SELECT NUMBER ? Fig. 2. The menu.

285

struct the graph based on the most recent edited version of this data. If one has recalled a curve rather than data, then there will be no data in memory and the program will display the recalled

FOR CURVE 0

POI NT

A

B

1

0

0

10

5

curve. The data display utilizes the same format as the initial data entry and requires no further elaboration.

3 4 5 6

0 0 0 0

20 30 40 50

10 15 20 25

For plotting, the raw values of the points are scaled and put into the X, Y, Z array. The axes are first drawn and labelled, the labels are positioned using a L O C A T E command, the arguments of which require modification if one alters either the axis length or angles are altered. The curves are then plotted by taking the points from the X, Y, Z array and calculating the location of each point using the appropriate subroutine. A small circle is used to identify each point and then a line is drawn from the preceding point to the one just placed. When the line is complete, its label is placed relative to the last point drawn. The value of a style variable alternates between solid and broken curves. If desired, a third pattern can be introduced (longer dashes separated by longer spaces), allowing alternation between three different patterns of curves. When the graph is complete, the program waits for any key-press to return to the menu.

FOR CURVE 10 P01NT A

B

EFFECT

1 2 34 5 6

0 10 20 30 40 50

10 15 20 25 30 55

FOR CURVE 20 POINT A

B

EFFECT

1 2 3 4 5 6

0 10 20 30 40 50

20 25 30 35 40 45

FOR CURVE 30 POI NT A

B

EFFECT

1

30

0

30

2

30

I0

35

2

0

10 10 10 10 10 10

20 20 20 20 20 20

EFFECT 0

2.5. Printing

3

4 5

30 30

30 40

45 50

Again, one has the option of printing either the data or the graph. Printing data utilizes the same format as the screen display of data. The only changes required are L P R I N T in place of PRINT, TAB commands in place of L O C A T E commands, and a change in maximum line number from 24 to

6

30

50

55

FOR CURVE 40 POI NT A

B

EFFECT

1 2

40 40

0 10

40 45

66.

3 4

40 40

20 30

50 55

Printing the graph is more complicated and similar to graph storage. The graph is first displayed on screen and the screen is then dumped to the printer. The approach used is a modification

5 6

40 40

40 50

60 65

PO I NT

A

B

EFFECT

Fig. 3. Data printout demonstrating the interaction of 2 hypothetical drugs, A and B where the potency of the effect (E) of A is twice the potency of B. Axes A and B represent concentrations of the 2 drugs respectively. The individual curves are labelled in terms of the concentrations of A in the drug combination.

1

50

0

50

2

50

10

3

50 50 50 50

20 30

55 60

30

FOR CURVE 50

4 5 6

20

40

65

40

70

50

75

286 of a previously published p r o g r a m [3] and is suitable for an IBM graphics printer or any other printer recognizing similar escape sequences. Each pixel becomes 4 dots and the graph is rotated 90 degrees while printing to use a full sheet of standard-size paper. Figs. 3 - 7 were originally printed on an Epson LX-80 printer (Epson American, Torrance, CA). A delay was incorporated to prevent filling the printer buffer as this causes a 'device timeout' error * The delay used is optimal for an A T c o m p u t e r using an 8 M H z clock frequency and a printer with a 2 kilobyte buffer. The error is avoided without a n y slowing of the printer. Smaller delays would suffice for slower computers or printers with larger buffers and larger delays would be required for a compiled program. 2.6. Subroutines

ADDITIUE i~0!FFECT ~, e,.~ 50 ~ , , ~ -~ , , , e " . ~ , . . ~ 4@ ~-~..e,.,30 e,.e'" 10 ~ ' ~ ~ ~ ' ~ - ~ . . e ,'° 10 ,.F.'~'''I A ,.,.~,r . ~ , , ,,'~'' ,e ~,l--'V 100 v , o ~ - -@ B 100 Fig. 4. Graph print-out of the data in Fig. 2. FOR CURVE M PO I NT

The program contains a n u m b e r of subroutines which are utilized at various different times. In addition to those already mentioned above, they include subroutines for drawing the unit marks on the axis and clearing the variables in preparation for recall or entry of new data.

PROP

APD

0 0 0 0 0

277 241 213 190 180 169

FOR CURVEM/P POI NT MEX

PROP

APD

1 2

O 3. I

3 6.25 4 12.5 5 25 6 50 7 100 FOR CURVE P

0 .38

.75 1.5 3.1 6.25 12.5

275 256

POINT

MEX

PROP

APD

1

0 0 0 0 0 0 0

0 .75 1.5 3.1 6.25 12.5 25

278 254 236 219 201 185 181

1 3 4 5 6 2

MEX

0 12.5 25 50 100

6.25

0

3. Application The application of this p r o g r a m is illustrated by examining the effects of two hypothetical drugs. D r u g A has an effect E such that one unit A causes one unit of E. D r u g B is postulated to have only half the p o t e n c y of drug A when the effect E is examined. If the effects of A and B are directly additive in combination, the results of combining concentrations of A up to 50 units with concentrations of B up to 50 units can be seen in Fig. 3, which consists of the data print-out from this program. The overall effect of these drug combinations can be more readily grasped b y examining the graph created by the p r o g r a m (Fig. 4). We examined the effects of two drugs, mexiletine and propranolol, singly and in combination, • In order to see this message, the ON ERROR line (line 3 5 2 ) must first be removed from the program

2 3 4 5 6 7

242 225 205 192 190

Fig. 5. Data printout showing the effect of mexiletine (M) and propranolol (P), singly, and in combination (M/P) on the duration of the cardiac cell action potential (APD). Mexiletine concentrations (MEX) and propranolol concentrations (PROP) are in laM and APD in ms.

287

the effects of combinations of the two drugs in an 8 " 1 ratio of me~letine to propranolol. The results in terms of the duration of the action potential (APD) can be seen in Fig. 5, which is a g ~ n a direct print-out from the program. Examination of the numbers alone p r o ~ d e s little insight as to how combining the two drugs alters their effect. It is also difficult to visualize how one might best plot these results in a standard two-dimensional graph format. When plotted by the program (Fig. 6), it becomes apparent that the effect of the combination is essentially the same as the effect of mexiletine alone, the presence of propranolol in the combination making no significant contribution to the final effect. This becomes even more apparent when the data are replotted after changing the range of the propranolol a ~ s to a m a ~ m u m of 25 (rather than having the two drug axes equal in

~URAT!ON APD 3#-

.~.,M,'P

~

,~,~'-"~"'~

.../,~ MEX

0

""" "~" - :~SF ~

Fig. 6. Graph print-out of the data in Fig. 5.

on the parameters of the intracellular action potential of canine cardiac cells (N.D. Berman and J.E. Lou~des, unpublished observations). Examining the effect on one parameter, the rate of rise of the action potential, we determined that propranolol was 8 times as potent as mexiletine in depressing this parameter. Thus, we examined the effects of mexiletine in a concentration range of 3.1 to 100/zM and propranolol in a concentration of 0.75 to 25 /~M. We compared these results to

3U~'~TIOil

A~B 3~,

~

M

...........

~MP /

"" . . . . . . . . . . . . . . . . . . . . .

P ,.,,~ E~( .,.-,r"' 0

: IO~

,-,"" --~"

., , . - ' " ' " 0 "-" PROP 25

Fig. 7. The same data as in Fig. 6 replotted with the range of the propranolol axis changed from 0-100 to 0-25/~M.

range) (Fig. 7).

4. Discussion The program as presented here has a number of functions that we have found quite useful. It enables one to store and plot data points that have 3 components. It can plot these points in a three-dimensional graph format, allowing one to readily visualize an interaction between two variables and their result. The program was written for my personal use and as such, incorporates error-trapping only to the extent necessary to avoid invalid data entries (e.g. variables beyond the range of the axes). Additional modifications would be advisable if the program were to be used by people not familiar with its construction. For example, as written, only upper-case input is recognized. Recognition or conversion of lower-case input could be readily added. There is no provision for trapping function keys or control sequences, as this would increase the size of the program and I felt these were unlikely to be entered accidentally. Finally, the data display, both on screen and paper, is adequate for my needs but its appearance could be improved with the use of the P R I N T U S I N G command. The figures used to illustrate this report repre-

288 sent the direct o u t p u t f r o m the p r o g r a m a n d are useful for visualizing the i n t e r a c t i o n b e t w e e n two variables. T h e y are a d e q u a t e for the p r o d u c t i o n of slides for p r e s e n t a t i o n s b u t less t h a n ideal for publication. However, they d o p r o v i d e a useful original which can b e t r a c e d b y an artist a n d s u b s e q u e n t l y labelled in a m o r e f o r m a l fashion, resulting in suitable illustrations for p u b l i c a t i o n s .

5. Availability A p r o g r a m listing is available f r o m the a u t h o r on request.

References [1] G.H. Burke, J.E. Loukides and N.D. Berman, Effects of simultaneous administration of mexiletine and quinidine on the electrophysiologic parameters of canine Purkinje fibers, J. Cardiovasc. Pharrnacol. 8 (1986) 1138-1143. [2] G. Haroney, Graphing quadratic surfaces, BYTE 11 (1986) 215-224. [3] D. Conklin, PC Graphics: Charts, Graphs, Games and Art on the IBM PC, p. 150 (New York NY: Wiley, 1983).