Friday, March 8, 2013

Polar Plotter-Example (1)


This program plots polar functions of the form
radius = f(angle)
Program settings are made for iPad. To run on iPhone/iPod Touch change:
W = 320
H = 460
U = 20

change line 160 to plot other polar functions
160 DEF FN F(T) = 5
160 DEF FN F(T) = T / (2 * PI)
160 DEF FN F(T) = 5 * SIN(4 * T)
160 DEF FN F(T) = 4 * SIN(6 * T) + 4

to understand math behind polar plotting I recommened Wikipedia: Go Wiki

full program:

10 REM POLAR PLOT PROGRAM
20 REM INIT
30 SCREEN 1
40 W = 768
50 H = 1024-20
60 U = 40
70 XC = W / 2
80 YC = H / 2
90 DEF FN MX(X) = XC + X * U
100 DEF FN MY(Y) = YC - Y * U
110 S = 1 / U
120 PI = 3.1415926
130 REM DRAW AXIS
140 GOSUB 1000
150 REM SET PARAMETERS
160 DEF FN F(T) = 5*SIN(6*T)-1
170 C = 3
180 TS = 0
190 TF = 2 * PI
200 GPRINT "r = 5 sin(6 theta)-1", 10, 80, C
210 REM PLOT
220 GOSUB 2000
230 SLEEP 15000
240 END
1000 REM [DRAW AXIS]
1010 CLS
1020 N = INT(XC / U)
1030 FOR I = 1 TO N
1040 CIRCLE XC, YC, I * U, 15, 0
1050 NEXT I
1060 LINE XC, 0, XC, H, 1
1070 LINE 0, YC, W, YC, 1
1080 RETURN
2000 REM [PLOT]
2010 T = TS
2020 R = FN F(T)
2030 X = R * COS(T)
2040 Y = R * SIN(T)
2050 XX = FN MX(X)
2060 IF XX < 0 THEN XX = 0
2070 YY = FN MY(Y)
2080 IF YY < 0 THEN YY = 0
2090 MOVETO XX, YY
2100 T = T + S
2110 R = FN F(T)
2120 X = R * COS(T)
2130 Y = R * SIN(T)
2140 XX = FN MX(X)
2150 IF XX < 0 THEN XX = 0
2160 YY = FN MY(Y)
2170 IF YY < 0 THEN YY = 0
2180 LINETO XX, YY, C
2190 IF T <= TF THEN 2100
2200 FRAME 0, 0, W-1, H-1, 0
2210 RETURN




No comments:

Post a Comment