Gw Temp

Menu

Tutorial - 'Square Root Algorithm' by voxvam

An item about RPGMaker 2000 posted on

Blurb

Voxvam explains an algorithm in Rm2k!

Body

-----Basic Information About This Algorithm-----
Knowledge Requirements: Common Events, Call Event, Change Variable, Fork Conditions, Cycle, Break Cycle
Pros: Runs very fast, can handle values from 0 to 999999, one of the three variables needed can be reused as it is a dummy variable.
Cons: Obviously cannot do negative numbers, almost always will round down (except when the value is 1 less than a perfect square, then it will round up), RM2K doesn't allow decimals at all, so accuracy is an issue.
Example: The square root of 100 is 10, the square root of 99 is 10, but the square root of every number from 80 to 98 is 9.

-----What Am I Going To Use This For-----
Well there are two key uses for this algorithm. One is for use in complex equations like calculating damage in a cbs. The other is probably more common, it's the calculation of distance between two sets of points.

-----I Want To Calculate Distance, Now What-----
Well the best way I can think of for this is to use the distance equation, which is a modification of the Pythagorean Theorem (for right angle triangles, A˛+B˛=C˛). The distance equation is as follows:
- Find out the X and Y values of the two things you wish to find the distance between.
- Find out the difference of the X values and then square that value.
- Find out the difference of the Y values and then square that value.
- Add those two values together.
- Use this algorithm to square root the value you just got.
- The answer from this algorithm is a close approximation to the actual distance between the two things.

-----Why Use This Algorithm, Whynot Use That Short Way-----
That is a good question, but there is a good answer. This way is faster.
Example: Lets take a look at the worst case scenario, finding the square root of 999,999. Now using the short way you would have to cycle 999 times to find the answer. That's quite a lot really. The number of cycles that my method requires for that same number is only 13.

-----Okay, You Convinced Me, Now What-----
Well lets start then.

You will need a total of 3 variables:
- Value To Square Root : I think the name is obvious. For this example lets call it Value.
- Square Root Answer : Yet another obvious variable name. For this example lets call it Answer.
- Temp Variable : I bet you can't guess what this is :) What that, you think for this example I'll call it Bob, naw... lets call it Temp instead.

Now to the coding of the common event.

- Create a new common event called Square Root. Make the event start condition set to Call.

From this point on I will use pseudo-code (looks like RM2K code but it's not):

- Change Variable Answer, Set to Value
- Fork: Is Value less than 0
- Show Message "Error, cannot find the square root of a negative number."
- Else
- Fork: Is Value greater or equal to 2
- Change Variable Answer, Divide by 2
- Start Cycle
- Change Variable Temp, Set to Answer
- Change Variable Answer, Set to Value
- Change Variable Answer, Divide by Temp
- Change Variable Answer, Add by Temp
- Change Variable Answer, Divide by 2
- Fork: Is Answer equal to Temp
- Break Cycle
- End Fork
- Change Variable Temp, Add by 1
- Fork: Is Answer equal to Temp
- Break Cycle
- End Fork
- Change Variable Temp, Subtract by 1
- End Cycle
- End Fork
-Change Variable Temp, Set to 0
- End Fork

That's it for the common event. Now all you have to do is set Value, call this common event, and the answer will be found in Answer.

Note: Temp is set to zero before and after calling this common event. So you can use it for other purposes. Just remember that when you call this event whatever was in Temp is lost.

That's all folks.

If you have a comment/question/suggestion, please feel free to email me at voxvam@shaw.ca or catch me on aim (signon-name: voxvam).

Good luck and have fun now :)