Computers are marvelous inventions, and Just Basic is a wonderful program for creating computer simulations. Today, let's pretend the date is 5 billion years ago and the Earth does not have a satellite. I am going to be Master of the Solar System and create a (simulated) satellite.
The first thing I will do is create a window for graphics and fill it with a dark blue color. Everyone knows space is black, but dark blue is so much prettier. In the center of this window I will place a blue ball (the Earth!). That was easy, but it's pretty blah. We need to have a satellite orbiting my new work of art.
Easier said than done, you're thinking. How can we show a satellite orbiting a point on our window? Enter "posxy" and a little trigonometry.
When you type "HOME" for your graphic display, the cursor is centered in your window for graphics or graphicbox. We can get the numeric values for this position using the command "posxy X Y" (or A B, or num1 num2, if you like). "HOME" positions the cursor and "posxy X Y" gets the X and Y coordinates as a numerical value. Now that we know the center of the container, also the center of the Earth I just created, we can begin putting a satellite in orbit and we won't need any portion of the NASA budget to accomplish this trick.
In this simulation, I've chosen 250 as the radius for the Lunar Orbit, but you can choose any value you like. (You can be Master of the Solar System too!). The position of the Lunar satellite will be 250 units (call them miles, kilometers, or pixels if you like) from the Earth, but where will that be on our GUI? Obviously it will be somewhere right, left, up, or down from "HOME," but we don't know where. If the satellite is going to be in motion, we must calculate a new position for X and Y every time the display is updated. Let's use this code as an example.
'Lunar Orbit DEMO
'by Welo, 040606
NOMAINWIN
WindowWidth=600
WindowHeight=600
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)
OPEN "Lunar Orbit" FOR GRAPHICS_nsb AS #g
PRINT #g, "trapclose quit"
[newLocation] '<< Start here to draw new window
PRINT #g, "DISCARD"
PRINT #g, "FILL darkblue" '<< erases previous drawing
PRINT #g, "HOME ; DOWN"
PRINT #g, "BACKCOLOR cyan" '<< set color for Earth
PRINT #g, "CIRCLEFILLED 40" '<< draw the Earth
theta=theta-1 '<< move satellite this many degrees and draw again
GOSUB [newSatellite] '<< Calculate position and draw new satellite
CALL pause '<< delay between redrawing
GOTO [newLocation] '<< Go back and start over
WAIT
' Subs and Functions go below here
SUB pause
TIMER 50, [continue] '<< Pause between redrawing window
WAIT
[continue]
TIMER 0
END SUB
SUB quit handle$ '<< quit in sub avoids "branch label not found" error
CLOSE #g
END
END SUB
[newSatellite]
PRINT #g, "posxy x y" '<< gets the current cursor position
rad=(theta/57.29577951)
x=(x-(sin(rad)*250)) 'Trig for circle, radius 250
y=(y+(cos(rad)*250)) 'to calculate new x, y position
PRINT #g, "BACKCOLOR white"
PRINT #g, "PLACE "; x; " "; y '<< New position to print
PRINT #g, "CIRCLEFILLED 10"
PRINT #g, "FLUSH"
RETURN
Just as there is no UP or DOWN in space, we can begin with any value for a simulated orbit. In this example, we begin with the HOME position, then calculate where the Moon would be if it were on the end of a 250 unit string. This is pretty straight forward using the SIN() and COS() functions for a right triangle, and a hypotenuse of 250 units.
We calculate the X and Y coordinates and print the Moon at that location on the screen. Now we have to make it move!
Theta is Greek for any unknown angle, and while this angle is surely unknown to me, the Worbles can calculate X and Y when we tell them "theta=theta-1", but first we must convert theta from degrees to radians, because Worbles think in radian units of measure. The first calculation uses zero for theta, because every program begins with all values at zero until a new value is assigned during execution. The next calculation uses 359, the next 358, etc, because those Worbles are smart enough to know there are only 360 degrees in a circle. If we want our Moon to travel clockwise, instead of counter-clockwise, we simply change "theta=theta-1" to "theta=theta+1". With each pass through the loop, the Moon will advance to a new location by one degree. Who knew this "Master of the Solar System" stuff could be so simple?
The speed of our Moon is controlled by two factors, the change in theta (try theta=theta+5, for example) and the delay inserted by SUB pause. Because a major portion of execution is spent in SUB pause of this program, I placed the exit routine in SUB quit to avoid a "branch label not found" error if the user attempts to exit while execution is waiting in the pause routine. Remember, SUBs can see other SUBs, but they cannot see labels within the main program.
Now that you know how simple this "Master of the Solar System" stuff can be, as well as a little basic trigonometry, go out and create your own Solar System, this one is mine