2019年10月25日金曜日

Writing formulas in both AppInventor and JavaScript

Using MIT App Inventor, you can, of course, write mathematical formulas. However, long formulas tend to be complex to represent. In that case, as written in the previous article, you can write a formula in JavaScript and call it from App Inventor. In the following, I will write some mathematical formulas in these two ways and compare them.

As an example, let's create an application in App Inventor to find the distance and azimuth between two points as shown in Fig.1. With Google Maps, isn't it easy? Yes, that's right. But it's fun to create your own program! This execution example shows that the distance from Tokyo Tower to Mt. Fuji is about 98 km and the azimuth is 250 degrees. The azimuth is shown as the displacement when the north is 0 degrees.


There are several known formulas for calculating distance and azimuth. Among these, referring to literature [1], Fig.2 explains practical mathematical formulas when the earth is regarded as a sphere. Let's write this formula in both JavaScript and App Inventor.


The first is the JavaScript function shown in Fig.3. The arguments of JavaScript trigonometric functions (Math.sin, Math.cos, Math.tan, etc.) must be given an angle converted from latitude and longitude to radians. Note that the entire application is created with App Inventor and this JavaScript function is called from App Inventor.


On the other hand, in Fig.4, the above formula was created as a function of App Inventor. Since the angle given to the trigonometric function of App Inventor is degrees, the latitude and longitude values ​​can be entered as they are. At first glance, it looks a little complicated, but the structure of the formula is clear and bugs are less likely to occur. However, longer formulas are cumbersome because the number of block operations increases and the area occupied by the diagram also increases.


Although it is a common-sense conclusion, the above is summarized as follows:
  • Note that the angle given to trigonometric functions is degrees in App Inventor, but radians in JavaScript (and common programming languages).
  • For fairly complex formulas, you can create it as a function in JavaScript and call it from App Inventor.
  • For relatively simple formulas, writing everything in App Inventor will give you a clearer understanding of the formula structure and fewer bugs.
  • It makes sense to try it out instead of imagining it.
I would like to supplement a little. Thanks to the rich functionality of MIT App Inventor Openstreet Map, expansion as shown in Fig. 5 can be done easily. That is, by clicking on the map, you can get the latitude and longitude of that point. In addition, my above calculation results (distance and azimuth) were completely consistent with those calculated by Openstreet Map! In the figure, red and blue markers indicate Tokyo and Sapporo stations, respectively.


References
[1] Keisan by CASIO (in Japanese)

0 件のコメント:

コメントを投稿