I am trying to find a php equivalent of processing's "map" function so I can re-map a number from one range to another. Does anything exist? Is it called something different?
http://processing.org/reference/map_.html
For example to map a value from 0-100 to 0-9.
map(75, 0, 100, 0, 9);
There is no native function for doing this, but it's easy to create:
function map($value, $low1, $high1, $low2, $high2) {
return ($value / ($high1 - $low1)) * ($high2 - $low2) + $low2;
}
This is untested, but you should hopefully get the idea.
Thanks for this great function!
I would personally improve a bit this function by adding simple error checking, to avoid dividing by 0, which would make PHP have a fatal error.
function map($value, $low1, $high1, $low2, $high2) {
if ($low1 == $high1)
return $low1;
return ($value / ($high1 - $low1)) * ($high2 - $low2) + $low2;
}
Related
I need to return few values from rust function. Tried to declare function which returns an array
$ffi = FFI::cdef('float get_arr()[2];', './target/release/libphp_rust.dylib');
$array = $ffi->get_arr();
But got an error:
PHP Fatal error: Uncaught FFI\ParserException: function returning array is not allowed at line 1 in /array.php:3
It seems PHP FFI can't work with arrays directly. So I found another solution.
I created C-array from PHP, then passed pointer to it to Rust code and then populated it with Rust function:
$ffi = FFI::cdef('bool get_arr(float (*res)[2]);', './target/release/libphp_rust.dylib');
$array = $ffi->new('float[2]');
$result = $ffi->get_arr(FFI::addr($array));
if ($result) {
var_dump($array);
} else {
//... something went wrong
}
#[no_mangle]
pub extern fn get_arr(array_pointer: *mut [f32;2]) -> bool {
let res = unsafe {
assert!(!array_pointer.is_null());
&mut *array_pointer
};
res[0] = 0.1;
res[1] = 0.2;
return true;
}
This solutions seems to work correct but i have some doubts about it:
Is passing pointers to FFI safe enough and what problems may I face with this in future?
Are Rust arrays fully C-compatible so that I'm able to assign value to it directly by index?
I there better way to achieve what I need? Or maybe are there some good practices about passing complex data structures with FFI?
Thanks
The rules surrounding this are still up in the air, so your example is questionably safe. This should be ok, but requires nightly features:
#![feature(maybe_uninit_extra)]
#![feature(ptr_as_uninit)]
// Make sure you use `extern "C"`. `extern` alone means `extern "Rust"`.
#[no_mangle]
pub extern "C" fn get_arr(array_pointer: *mut [f32; 2]) -> bool {
let fat: *mut [f32] = array_pointer;
let res = unsafe { fat.as_uninit_slice_mut().unwrap() };
res[0].write(0.1);
res[1].write(0.2);
true
}
On the stable channel it's just less elegant:
// Make sure you use `extern "C"`. `extern` alone means `extern "Rust"`.
#[no_mangle]
pub extern "C" fn get_arr(array_pointer: *mut [f32; 2]) -> bool {
assert!(!array_pointer.is_null());
unsafe {
let res = array_pointer as *mut f32;
res.add(0).write(0.1);
res.add(1).write(0.2);
}
true
}
I've been thinking about ways to program for exteme distances, in the game Hellion for example, orbits could be near to scale in the ranges of millions of kilometers. However there was a common glitch where movement would be very choppy the further you were from the object you were orbiting. I might be wrong in my speculation to why that was, but my best guess was that it was down to loss of precision at that distance.
As a little exercise I've been thinking about ways to solve that problem and what I currently have is a a pretty basic unit staged distance system.
class Distance
{
public const MAX_AU = 63018.867924528;
public const MAX_MM = 149598000000000;
private $ly = 0;
private $au = 0;
private $mm = 0;
public function add(Distance $add): Distance
{
$distance = new Distance();
$distance->mm = $this->mm + $add->mm;
if ($distance->mm > self::MAX_MM) {
$distance->mm-= self::self::MAX_MM;
$distance->au++;
}
$distance->au+= $this->au + $add->au;
if ($distance->au > self::MAX_AU) {
$distance->au-= self::self::MAX_AU;
$distance->ly++;
}
$distance->ly+= $this->ly + $add->ly;
return $distance;
}
}
I put in the addition method, which is written as though by hand. I didn't want to use arbitary precision because it would have been too extreme for calculating the smaller distances a player would normally interact with.
My question is; is this how something like this is normally done? and if not, could someone please explain what is wrong with this (inefficient for example), and how it could be done better?
Thanks
PS. I am aware that in the context of a game, this is normally handled with sub-grids, but to simulate how objects in orbit would drift apart, thats what this is for.
You can use the BCMath functions. BCMath supports numbers of any size and precision.
Example:
$mm = "149598000000000";
$au = "63018.867924528";
$sum = bcadd($mm,$au,10);
echo $sum;
//149598000063018.8679245280
I'm trying to get percentage of two numbers in my app (laravel based) but i don't get the right numbers.
Code:
$pdts->price = 123.234
$pdts->newprice = 90.500
{{number_format($pdts->newprice / $pdts->price * 100, 0) } }%
it returns -73%, it should return -27%. How do I correct this?
Update To those still didn't get it
Guys I did defined my goal / what i'm looking for it should return -27%. How do I correct this? so I was looking to get -27% as the result and thanks to commenters I did find my solution.
I don't understand all this vote-downs and continued comments.
This is not really a programming question, it's more a math question.
But to get the result you want you need to calculate the difference in percentage.
//(123.234-90.500)/123.234*100
- {{number_format(($pdts->price - $pdts->newprice) / $pdts->price * 100, 0) } }%
// -27%
Try this one,
$value = ($pdts->price-$pdts->newprice) / $pdts->price * 100;
$percentage = (int)($value).'%';
Here, percentage variable will have your required value.
How can I do hashing url paramaters in Laravel?
I know the Hash::make method, but that's a method for passwords (those hashes are not very url-friendly).
Does Laravel have a beter alternative, so I can hash parameters like http://url?key=2jd8dka72
you can use Laravel Encrypt function for that .
put
use Illuminate\Support\Facades\Crypt;
in header section and than use Crypt::encrypt($param) to encrypt param and Crypt::decrypt($param) to decrypt it.
Just add base64 encoding to make it more friendly looking.
use Hash;
...
$id = 15;
$key = base64_encode(Hash::make($id));
echo "http://someurl?send_mail_to_user=$id&key=$key";
When you check it:
use Hash;
...
$keyDecoded = base64_decode($request->key);
if(Hash::check($request->id, $keyDecoded)) {
// checked
}
Another way is to use some complicated function like a large number at another base. But it is not secured (just security through obscurity):
echo base_convert($id * 250 + 5675675, 10, 33); // converts 15 to 4q18q
echo ((base_convert('4q18q', 33, 10) - 5675675) / 250); // converts back, but this one is not being used
// checking:
if(base_convert($request->id * 250 + 5675675, 10, 33) === $request->key) {
// checked
}
I have a question regarding Map tile server and coordinates conversion in Google Maps using the Google Maps Utility library.
My tile server accesses a database with thousands of gps coordinates (lat,lng), and for every (lat,lng) point, checks if the point is inside the geographical bounds of the tile; if it does, coordinates conversion (WGS84 -> Mercator -> X,Y offset inside the tile) and paints the corresponding pixel inside the tile, using the GoogleMapsUtility Library.
In terms of code, I do the following:
$point = GoogleMapUtility::getOffsetPixelCoords((float)$row['lat'], (float)$row['lng'], $zoom, $X, $Y);
which calls the getOffsetPixelCoords function (and in turn the functions below) from the library:
public static function getOffsetPixelCoords($lat,$lng,$zoom, $X, $Y)
{
$pixelCoords = GoogleMapUtility::getPixelCoords($lat, $lng, $zoom);
return new Point(
$pixelCoords->x - $X * GoogleMapUtility::TILE_SIZE,
$pixelCoords->y - $Y * GoogleMapUtility::TILE_SIZE
);
}
public static function getPixelCoords($lat, $lng, $zoom)
{
$normalised = GoogleMapUtility::_toNormalisedMercatorCoords(GoogleMapUtility::_toMercatorCoords($lat, $lng));
$scale = (1 << ($zoom)) * GoogleMapUtility::TILE_SIZE;
return new Point(
(int)($normalised->x * $scale),
(int)($normalised->y * $scale)
);
}
private static function _toNormalisedMercatorCoords($point)
{
$point->x += 0.5;
$point->y = abs($point->y-0.5);
return $point;
}
Ok, now the results. For a zoom level<13 it works great, below is an example of a tile in Zoom level 11:
Image1
However, for a tile in zoom level >13, the following happens:
Image2
Which is so strange... the pixels seem to be perfectly aligned ? At first I thought it is a decimal resolution problem, but the resolution of the data is quite good (stored as double in a mysql database, for example, 35.6185989379883, 139.731994628906, and in php floats and doubles are the same thing...)
Could someone help me on how to fix this problem?
Thanks in advance...
Why do you use type casting on the result of the database query? In the example of the book googlemapsutility it's not there?