1
1
2
2
' Another look at Trig functions.bas SmallBASIC 0.12.2 [B+=MGA] 2016-05-01
3
3
' inspired by PeterMaria's simple code for Atan2 on Aurels' forum BasicPro
4
+ ' 2017-09-23 Modified to run again on Android
4
5
5
6
' Here is another effort in a continuing series to demystify Trig functions:
6
7
10
11
' angles to the horizontal line, lengths of the sides and hypotenuse of the right
11
12
' and the COS, SIN and TAN ratios
12
13
13
- sub drawXit
14
- local c, i
15
- ' for mouse clicks
16
- ' mx > mxborder=xmax-60
17
- ' my < myborder=30
18
- c = rgb (190 , 0 , 0 )
19
- rect xmax - 60 , 10 step 51 , 21 , 0 filled
20
- rect xmax - 60 , 10 step 50 , 20 , c filled
21
- for i = 0 to 2
22
- line xmax - 40 + i , 15 , xmax - 33 + i , 25 , 15
23
- line xmax - 40 + i , 25 , xmax - 33 + i , 15 , 15
24
- next i
25
- end
26
-
27
14
sub ThickArc (xCenter, yCenter, arcRadius, dAngleStart, dAngleEnd, rThick )
28
- local rAngle, rAngleStart , rAngleEnd , x1 , y1 , Stepper
15
+ local rAngle, rAngleStart , rAngleEnd , x1 , y1 , Stepper
29
16
' draws an Arc with center at xCenter, yCenter, radius from center is arcRadius
30
-
17
+
31
18
' for SmallBASIC angle 0 degrees is due East and angle increases clockwise towards South
32
-
33
- ' THIS SUB IS SETUP TO DRAW AN ARC IN CLOCKWISE DIRECTION
34
-
19
+
20
+ ' THIS SUB IS SETUP TO DRAW AN ARC IN CLOCKWISE DIRECTION
21
+
35
22
' dAngleStart is where to start Angle in degrees
36
23
' so make the dAngleStart the first ray clockwise from 0 degrees that starts angle drawing clockwise
37
-
24
+
38
25
' dAngleEnd is where the arc ends going clockwise with positive degrees
39
26
' so if the arc end goes past 0 degrees clockwise from dAngleStart
40
27
' express the end angle as 360 + angle
41
-
28
+
42
29
' rThick is the radius of the many,many tiny circles this will draw to make the arc thick
43
30
' so if rThick = 2 the circles will have a radius of 2 pixels and arc will be 4 pixels thick
44
31
if arcRadius < 1 then pset xCenter, yCenter : exit func
45
32
rAngleStart = rad (dAngleStart ) : rAngleEnd = rad (dAngleEnd )
46
33
if int (rthick ) = 0 then Stepper = 1 / (arcRadius *pi ) else Stepper = rThick / (arcRadius * pi / 2 )
47
34
for rAngle = rAngleStart to rAngleEnd step Stepper
48
35
x1 = arcRadius * cos (rAngle ) : y1 = arcRadius * sin (rAngle )
49
- if int (rThick ) < 1 then
50
- pset xCenter + x1 , yCenter + y1
51
- else
36
+ if int (rThick ) < 1 then
37
+ pset xCenter + x1 , yCenter + y1
38
+ else
52
39
circle xCenter + x1 , yCenter + y1 , rThick filled
53
40
fi
54
41
next
55
42
end
56
43
57
44
sub ThickLine (x1, y1, x2, y2, rThick )
58
45
local length, stepx , stepy , dx , dy , i
59
-
46
+
60
47
' x1,y1 is one endpoint of line
61
48
' x2,y2 is the other endpoint of the line
62
10000
- ' rThick is the radius of the tiny circles that will be drawn
49
+ ' rThick is the radius of the tiny circles that will be drawn
63
50
' from one end point to the other to create the thick line
64
51
' Yes, the line will then extend beyond the endpoints with circular ends.
65
52
@@ -74,7 +61,7 @@ sub ThickLine(x1, y1, x2, y2, rThick)
74
61
end if
75
62
end
76
63
77
- ' ============================== Main
64
+ ' ============================== Main
78
65
const thick = 2
79
66
const arc_radius = 100
80
67
const hor_color = rgb (30 ,30 ,30 )
@@ -86,38 +73,32 @@ const white = rgb(255,255,255)
86
73
cx = xmax / 2 : cy = ymax / 2
87
74
88
75
while 1
89
- if asc (inkey ) = 27 then end else pen on ' did user press esc to quit?
90
76
cls
91
- ' draw eXit sign
92
- drawXit
93
-
77
+
94
78
' draw horizontal through center of screen
95
79
line 0 , cy , xmax , cy , hor_color
96
-
80
+
97
81
' get mouse
98
82
mx = pen (4 ) : my = pen (5 ) ' get mouse location
99
-
100
- ' check if eXit was clicked, if so then end
101
- if mx > xmax - 60 and my < 30 and pen (3 ) then end
102
-
103
- ' draw our Color Coded Trig Triangle
83
+
84
+ ' draw our Color Coded Trig Triangle
104
85
color cos_color
105
86
ThickLine cx, cy , mx , cy , thick
106
87
color sin_color
107
88
ThickLine mx, cy , mx , my , thick
108
89
color hyp_color
109
90
ThickLine cx, cy , mx , my , thick
110
-
91
+
111
92
stepx = abs (cx - mx ) : stepy = abs (cy - my )
112
93
hyp = ( (stepx ^ 2 + stepy ^ 2 ) ^.5 )\1
113
-
94
+
114
95
' to draw angle need to do some math
115
96
' dAng = mouse angle to 0 degrees due East
116
97
' other Angles: StartA, EndA and reportA are for the Trig Ratios of triangle
117
98
dAng = ( deg (atan ( (my - cy ) / (mx - cx ) ) )+.5 ) \ 1
118
99
if mx < cx then dAng = dAng + 180
119
100
if my < cy and mx > cx then dAng = dAng + 360
120
- if dAng <= 90 then
101
+ if dAng <= 90 then
121
102
startA = 0 : endA = dAng : reportA = dAng
122
103
elif dAng <= 180
123
104
startA = dAng : endA = 180 : reportA = 90 - (dAng - 90 )
@@ -128,7 +109,7 @@ while 1
128
109
fi
129
110
color ang_color
130
111
ThickArc cx, cy , arc_radius , startA , endA , thick
131
-
112
+
132
113
' report all numbers color coded
133
114
color ang_color
134
115
locate 0 , 0 : ? " yellow Angle (degrees) ~ " ; reportA
@@ -167,5 +148,5 @@ while 1
167
148
fi
168
149
showpage
169
150
delay 100
170
- pen off
171
151
wend
152
+
0 commit comments