Basic Maths
Using basic mathematics, coders can find given values to problems that require maths to let the code work correctly. I will be showing you how to work out given equations and also applying the maths to common issues a coder may face when coding.Introduction
Math doesn't have to be hard, tiresome work. It can be easy, and be applied easily to code. An example would be this.function onCreated() { echo(10 + 10); }
This would echo to RC the value of the echo contents. The answer would be 20. If you have RC access, try it yourself.
You can also define the addition in another array.
temp.math = 10 + 10; echo(temp.math);
This would still give the same output, but the code can now be read easier.
More Advanced
Now you are comfortable with using basic maths within your code, lets try some more complex maths.This time, we are going to define out numbers in separate arrays, and add the arrays together.
temp.a = 10; temp.b = 10; echo(temp.a + temp.b);
This would echo the given value when temp.a (10), and temp.b (10), are added together. Getting a little more complex, you could move onto doing something like this...
temp.a = 10; temp.b = 10; temp.c = temp.a + temp.b + 10; echo(temp.c);
This would echo the given value when temp.a and temp.b are added together, and then an additional 10 is added to the answer.
Applying Maths
Now you know how to use simple maths during coding, you can apply the knowledge you have learned. Here is an example of some maths.This will let me find the middle of my GraalControl (screen), and place an image in the centre using maths to locate the centre.
//#CLIENTSIDE function onCreated() { with(findimg(200)) { image = "block.png"; width = 32; height = 32; //Now for the math... temp.placex = GraalControl.width / 2 - width / 2; //The center of the screen, minus the width of the image divided by 2 temp.placey = GraalControl.height / 2 - height / 2; //Same principle x = temp.placex; y = temp.placey; } }
Try it yourself, the image will now be placed exactly in the centre of the screen.
Challenge
No point in learning something, and not putting you to the test! I want you to apply what you have learned and make a basic chat command calculator, rc command calculator or if you're feeling up to it, a GUI calculator. Have fun and keep on coding!Categories
CategoryBasicMath
There are no comments on this page. [Add comment]