Skip to content

Commit b089e43

Browse files
committed
Added better file handling
For programs that save images or files, added a subroutine to check if the file already exists and to not clobber it if it does.
1 parent 3c29b86 commit b089e43

5 files changed

Lines changed: 364 additions & 311 deletions

File tree

math/graphing/graph_calc.bas

Lines changed: 97 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,151 @@
1-
/*
1+
/*
22
Graphing calculator for parametric equations
33
Use ctrl-p to save a screenshot
44
*/
5-
Option explicit
6-
Option angle degrees
5+
option explicit
6+
option angle degrees
77

88
'initialize vectors
9-
Const MAXPTS = 100
10-
Dim float x(MAXPTS)
11-
Dim float y(MAXPTS)
9+
const MAXPTS = 100
10+
dim float x(MAXPTS)
11+
dim float y(MAXPTS)
1212

1313
'input functions
14-
Dim string funx$
15-
Dim string funy$
16-
Dim float tmin : Dim float tmax
14+
dim string funx$
15+
dim string funy$
16+
dim float tmin : dim float tmax
1717

1818
'set listener for keypress
19-
On KEY keypress
19+
on KEY keypress
2020

2121
ask:
22-
CLS
23-
Line Input "x(t)="; funx$
24-
Line Input "y(t)="; funy$
25-
Input "t range (min,max)"; tmin,tmax
22+
cls
23+
line input "x(t)="; funx$
24+
line input "y(t)="; funy$
25+
input "t range (min,max)"; tmin,tmax
2626

27-
CLS
27+
cls
2828
draw_axes 10
2929
draw_fun funx$, funy$, tmin, tmax
30-
Pause 60000 'pause for a while
31-
End
32-
33-
Sub keypress
34-
Local a$ As string
35-
a$ = Inkey$
36-
Select Case Asc(a$)
37-
Case 16 'ctrl-p for print screen
38-
Local fname$ As string
39-
fname$ = "screenshot-"+Str$(Rnd()*1000,0,0)+".bmp"
40-
Save Image fname$
41-
Case Else
42-
GoTo Ask
43-
End Select
44-
End Sub
45-
46-
47-
Sub fill_points funx$, funy$, numpts, tmin, tmax
48-
Local integer n = numpts
49-
Local float max.x = 0.0
50-
Local float max.y = 0.0
30+
pause 60000 'pause for a while
31+
end
32+
33+
sub keypress
34+
local a$ as string
35+
a$ = inkey$
36+
select case asc(a$)
37+
case 16 'ctrl-p for print screen
38+
local fname$ as string
39+
fname$ = "graph"
40+
check_file fname$
41+
save Image fname$
42+
case else
43+
goto Ask
44+
end select
45+
end sub
46+
47+
sub check_file fn$ as string
48+
local string f$ = dir$(fn$+"*",FILE)
49+
local string ext = ".bmp"
50+
local integer i = 0
51+
do while f$ <> ""
52+
inc i
53+
f$ = dir$()
54+
loop
55+
56+
if i > 0 then
57+
fn$ = fn$+"-"+format$(i)+ext
58+
else
59+
fn$ = fn$+ext
60+
end if
61+
end sub
62+
63+
sub fill_points funx$, funy$, numpts, tmin, tmax
64+
local integer n = numpts
65+
local float max.x = 0.0
66+
local float max.y = 0.0
5167

5268
'generate points
53-
Local float min.t = tmin
54-
Local float max.t = tmax
55-
Local float delta.t = (max.t-min.t)/n
56-
Local float t
57-
Local integer i
58-
For i=0 To n-1
69+
local float min.t = tmin
70+
local float max.t = tmax
71+
local float delta.t = (max.t-min.t)/n
72+
local float t
73+
local integer i
74+
for i=0 to n-1
5975
t = delta.t*i + min.t
6076

61-
x(i) = Eval(funx$)
62-
If Abs(x(i))>max.x Then max.x=Abs(x(i))
77+
x(i) = eval(funx$)
78+
if abs(x(i))>max.x then max.x=abs(x(i))
6379

64-
y(i) = Eval(funy$)
65-
If Abs(y(i))>max.y Then max.y=Abs(y(i))
66-
Next i
80+
y(i) = eval(funy$)
81+
if abs(y(i))>max.y then max.y=abs(y(i))
82+
next i
6783

6884
'catch null graphs
69-
If max.x = 0 Then max.x = 1
70-
If max.y = 0 Then max.y = 1
85+
if max.x = 0 then max.x = 1
86+
if max.y = 0 then max.y = 1
7187

7288
'add scale
7389
x(n) = max.x
7490
y(n) = max.y
75-
End Sub
91+
end sub
7692

77-
Sub draw_axes numticks
78-
Local integer mcount = numticks
93+
sub draw_axes numticks
94+
local integer mcount = numticks
7995

8096
'draw axes
81-
Local integer xaxis, yaxis
97+
local integer xaxis, yaxis
8298
xaxis = MM.VRES\2
83-
Line 0,xaxis,MM.HRES,xaxis,, RGB(green)
84-
Text MM.HRES-1,xaxis-1,"x axis", "RB",7
99+
line 0,xaxis,MM.HRES,xaxis,, rgb(green)
100+
text MM.HRES-1,xaxis-1,"x axis", "RB",7
85101

86102
yaxis = MM.HRES\2
87-
Line yaxis,0,yaxis,MM.VRES,, RGB(green)
88-
Text yaxis-1,1,"y axis","RT",7
103+
line yaxis,0,yaxis,MM.VRES,, rgb(green)
104+
text yaxis-1,1,"y axis","RT",7
89105

90106
'draw ticks
91-
Local integer i
92-
Local float dx, dy
107+
local integer i
108+
local float dx, dy
93109
dx = MM.HRES/mcount
94110
dy = MM.VRES/mcount
95-
For i=0 To mcount
96-
Pixel yaxis+1,Int(i*dy),RGB(green)
97-
Pixel Int(i*dx),xaxis+1,RGB(green)
98-
Next i
99-
End Sub
111+
for i=0 to mcount
112+
pixel yaxis+1,int(i*dy),rgb(green)
113+
pixel int(i*dx),xaxis+1,rgb(green)
114+
next i
115+
end sub
100116

101-
Sub draw_fun funx$, funy$, tmin, tmax
102-
Local integer i
103-
Local float nx.pt.x, nx.pt.y
104-
Local float ls.pt.x, ls.pt.y
117+
sub draw_fun funx$, funy$, tmin, tmax
118+
local integer i
119+
local float nx.pt.x, nx.pt.y
120+
local float ls.pt.x, ls.pt.y
105121

106122
'load plotting arrays
107123
fill_points funx$,funy$,MAXPTS,tmin, tmax
108-
Local float max.x = x(MAXPTS)
109-
Local float max.y = y(MAXPTS)
124+
local float max.x = x(MAXPTS)
125+
local float max.y = y(MAXPTS)
110126

111127
'scale plot
112-
Local float o.x = MM.HRES/2
113-
Local float o.y = MM.VRES/2
114-
Local float scl.x = (o.x-2)/max.x
115-
Local float scl.y = (o.y-2)/max.y
128+
local float o.x = MM.HRES/2
129+
local float o.y = MM.VRES/2
130+
local float scl.x = (o.x-2)/max.x
131+
local float scl.y = (o.y-2)/max.y
116132

117-
Text MM.HRES-1,Int(o.y+2), Str$(max.x,0,3),"RT",7
118-
Text Int(o.x+2),1,Str$(max.y,0,3), "LT",7
133+
text MM.HRES-1,int(o.y+2), str$(max.x,0,3),"RT",7
134+
text int(o.x+2),1,str$(max.y,0,3), "LT",7
119135

120136
'draw parametric equations
121137
ls.pt.x = scl.x*x(0) + o.x
122138
ls.pt.y = o.y - scl.y*y(0)
123139

124-
For i=1 To (MAXPTS-1)
140+
for i=1 to (MAXPTS-1)
125141
nx.pt.x = scl.x*x(i) + o.x
126142
nx.pt.y = o.y - scl.y*y(i)
127143

128-
Line Int(ls.pt.x),Int(ls.pt.y), Int(nx.pt.x),Int(nx.pt.y),1,RGB(blue)
144+
line int(ls.pt.x),int(ls.pt.y), int(nx.pt.x),int(nx.pt.y),1,rgb(blue)
129145

130146
ls.pt.x = nx.pt.x
131147
ls.pt.y = nx.pt.y
132148

133-
Pause 10
134-
Next i
135-
End Sub
149+
pause 10
150+
next i
151+
end sub

math/graphing/read.me

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ title: Parametric Graphing Calculator
22
date: 2025-12-03
33
program: graph_calc.bas
44
screenshot: cardiod.bmp
5-
description: A simple graphing calculator which plots parametric equations of the form x(t) and y(t). The plot area is automatically scaled to fit the plot while remaining centred at the origin. Hitting Ctrl-p saves a screenshot with a randomized filename.
5+
description: A simple graphing calculator which plots parametric equations of the form x(t) and y(t). The plot area is automatically scaled to fit the plot while remaining centred at the origin. Hitting Ctrl-p saves a screenshot named "graph.bmp", if one exists it increments the filename.
66

0 commit comments

Comments
 (0)