Category Archives: Math

Perlin noise generator in PHP

Generating Perlin noise requires heavy calculation and PHP (in my opinion) is not the best language for this task but still it can be useful.
The algorithm is an adaptation form this pseudo code.
The class seems to work but I’m not if it works 100% correctly. The tricky part was to deal with the types because [...]

Point in triangle

A function in PHP to check if a point is in triangle.
 
 
function fAB($t, $x, $y) {
return ($y – $t[’y1′])*($t[’x2′] – $t[’x1′])
-($x – $t[’x1′])*($t[’y2′] – $t[’y1′]);
}
 
function fBC($t, $x, $y) {
return ($y-$t[’y2′])*($t[’x3′]-$t[’x2′])
[...]

Pythagorean triple in PHP

Pythagorean triple is every integer a, b and c for which a2 + b2 = c2 or in other words the a, b, and c are the sides of a right triangle. To generate triples in PHP, you can use the following function:
 
function CalcTriple($n){
$res = array();
[...]

Prime numbers in PHP

This is quite fast (but unfortunately memory consuming) algorithm for generating all prime numbers up no n:
 
 
$n = 100; //this is the upper limit
 
for ($i = 2; $i <=$n; $i++){
$primes[] = $i;
}
 
foreach ($primes as $num){
if ($num != 1){
[...]