How to print output of .bas file to text

1k views Asked by At

I am trying to print coordinate outputs of a program to a text file in order to use it in another program but I don't really know anything about GWBASIC and its my first time using MS-DOS. I need it to open a text file named plot.txt and print output there and save it without actually plotting on GWBASIC. Here is the program which I found in an old magazine.

810 REM   MAKE A GLOBULAR
12 REM
14 R0=20: R2=R0*R0: R3=R2*R0
16 P1=3.14159265#
18 C0=P1*P1*R3/4
20 R1=R0/SQR(2)
22 XM=512: YM=512
24 X2=XM/2: Y2=YM/2: S=5
26 INPUT "HOW MANY STARS ";T
27 RANDOMIZE TIMER
28 CLS: REM  CLEAR SCREEN
30 FOR I=1 TO T
32 C=C0*RND: R=R1
34 REM
36 REM   NOW FIND R
38 FOR K=1 TO 5
40 GOSUB 100
42 R=R+(C-C1)/D
44 NEXT K
46 REM  3-DIMENSIONAL PLACE
48 X=RND-.5
50 Y=RND-.5
52 Z=RND-.5
54 S1=SQR(X*X+Y*Y+Z*Z)
56 IF S1>.5 THEN GOTO 48
58 REM  POINT IS NOW IN SPHERE
60 R=R*S1: X=X*R: Y=Y*R: Z=Z*R
62 GOSUB 200
64 NEXT I
66 END
68 REM
100 REM  NEWTON-RAPHSON ITERATION
105 A=R/R0
110 C1=ATN(A)*.5*R3
115 A=1+A*A
120 C1=C1+R*.5*R2/A
125 C1=P1*(C1-R*R2/(A*A))
130 D=4*P1*R*R/(A*A*A)
135 RETURN
140 REM
200 REM  2-DIMENSIONAL PLOT
203 SCREEN 9
205 X=X*S+X2: Y=Y*S+Y2
210 IF X<0 OR Y<0 THEN 225
215 IF X>=XM OR Y>=YM THEN 225
220 PSET(X,Y)
225 RETURN
230 REM  ------------------------
240 REM  APPEARED IN ASTRONOMICAL
250 REM  COMPUTING, SKY & TELE-
260 REM  SCOPE, APRIL, 1986
270 REM  ------------------------
2

There are 2 answers

1
John Coleman On BEST ANSWER

Here is a Python 3 paraphrase:

#globular.py
#Python paraphrase of model.bas from
#http://www.skyandtelescope.com/wp-content/uploads/model.bas

from math import pi, sqrt, atan
from random import uniform, random

#Global variables:

r0 = 20.0
r2 = r0**2
r3 = r0**3
c0 = pi**2*r3/4
r1 = r0/sqrt(2)

def NRI(c,r):
    #Newton-Raphson Iteration
    a = r/r0
    c1 = atan(a)*0.5*r3
    a = 1+a**2
    c1 += r*0.5*r2/a
    c1 = pi*(c1-r*r2/a**2)
    d = 4*pi*r**2/a**3
    return (c1,d)

def makeStars(t):
    stars = []
    for i in range(t):
        c = c0*random()
        r = r1
        for k in range(5):
            c1,d = NRI(c,r)
            r += (c-c1)/d
        while True:
            x = uniform(-0.5,0.5)
            y = uniform(-0.5,0.5)
            z = uniform(-0.5,0.5)
            s1 = sqrt(x**2 + y**2 + z**2)
            if s1 <= 0.5: break
        r *= s1
        x *= r
        y *= r
        z *= r
        stars.append((x,y,z))
    return stars

def starsToFile(t,fname):
    stars = makeStars(t)
    f = open(fname,'w')
    for star in stars:
        print(*star, sep = ', ',file = f)
    f.close()

I skipped the part about printing x and y and instead wrote a function makeStars to return a list of (x,y,z) tuples, as well as a related function which takes such an output and sends it to a text file. This last function is the only thing that used Python 3 instead of Python 2. If you are using Python 2 you can import Python 3's print function from the future.

Typing starsToFile(100,'stars.txt') in the Python shell gave me a text file which begins:

-0.32838465248713156, -0.3294895266926551, -1.2963580524762535
14.20224408569865, 1.4434961933043464, 6.450969593697097
1.6525937589658193, -0.24447292610082685, 1.0543647986350608
1.5707528567123823, 5.190972598268825, -2.0054790217091134

I don't have good 3-d scatter-plot graphing at my finger tips, but here is a screen shot of 50 points generated by the function and plotted using a computer algebra system called Derive:

enter image description here

Final remark: I wonder if there is a typo in the source code. The line

C0=P1*P1*R3/4

strikes me as suspicious since it is fairly rare in mathematics for pi to appear squared -- though it does happen. Maybe there should be only 1 factor of pi there (which would then have the effect of setting C0 proportional to the volume of the sphere of radius R0). On the other hand, I don't know exactly what is happening here, so I left it in. If the results seem problematic, you could maybe experiment with that line.

3
Marged On

If you want a copy of the calculated coordinates simply add these lines:

1 OPEN "PLOT.TXT" FOR OUTPUT AS #1
65 CLOSE #1
221 PRINT #1, X + "," + Y

The program will work as before but in addition to this it outputs the coordinate to a file named plot.txt

Put them in an image with 640x350 size (that size is demanded by SCREEN 9) and you get the same result.