Other Quiz #11 - Drawing functions

Erel

B4X founder
Staff member
Licensed User
Longtime User
Edit: Don't miss the bonus question in post #11.

Back to high school quiz.

In your code should be a sub with the following signature:
B4X:
Sub Func(x As Double) As Double

The user implements this function. The user enters (directly in the code) any math function they like.

The quiz is to draw the math function.

Some examples (the red graph will be explained soon):
B4X:
Sub Func(x As Double) As Double
   Return x
End Sub

SS-2014-01-26_10.57.37.png


B4X:
Return x * x

SS-2014-01-26_10.58.40.png


B4X:
Return Sin(x) * Cos(x)

SS-2014-01-26_10.59.26.png


B4X:
Return Logarithm (x, cE)

SS-2014-01-26_11.02.50.png


B4X:
Return Abs(x)

SS-2014-01-26_11.05.23.png


The recommended signature of the drawing sub should be:
B4X:
Sub Draw(MinX As Double, MaxX As Double, MinY As Double, MaxY As Double)

As you have probably guessed the red graph is the function derivative.
Note that the only "user" input is the implementation of the Func sub.

6 (out of 10) points - Correctly draw the function.
10 points - Correctly draw the function and its derivative.


You can implement it in B4J or B4A.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
All this was very interesting (but... how much am i a liar? :p)
I will explain again, the following 5 lines code outputs the values of the Sinus function. It should at least be surprising...
B4X:
Dim intervalX = 0.001, y = 0, dy = 1, x As Double
For x = 0 To 2 * cPI Step intervalX
   Log(x & ": " & y)
   y = y + dy * intervalX
   dy = dy - y * intervalX
Next
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I will explain again, the following 6 lines code outputs the values of the Sinus function. It should at least be surprising...
B4X:
Dim intervalX = 0.01, y = 0, dy = 1 As Double
For x = 0 To 2 * cPI Step intervalX
   dy = dy - y * intervalX
   y = y + dy * intervalX
   Log(x & ": " & y)
Next


I was kidding, of course.

I have not read everything (the thread) well.

But this your post means that I have something interesting to read.

A moment: I read it :)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm sorry, but I can not understand what should surprise me (but that's because I'm not exactly an expert on Math Functions!).

I did not write, jokingly, that "this is an interessant thread" because I am an expert, not at all
(to tell the truth, to draw a circle remember that I had a problem with Step!)

Personally I have no interest in this type of argument ("BOOOO, do not read it, then!" :p)

I have a bad way of joking; many people find it irritating. In fact I am one of the sweetest people on the Planet!

One day I will tell you my whole story (probably not in the "Quiz" section :)).

I was joking and I really like the Quiz.
I will attend the next
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Lucas, using tricks in coding is sometimes very important. For example I used the above sinus algorithm, in a old slot machine game, where I added an animation effect of vibrating elements. Since it was in assembly, I chose to avoid a 'straight' calc of sinus, since it would require Taylor's series and it could be less speedy. Nowadays machines are incredibly faster, still it's always great discovering and applying such tricks :)
 
Upvote 0
Top