I'm trying to do an activity where I need do put a number in cm, and the program has to return it with km, m, and cm.
An example: 270004 cm = 2km, 700m, 4cm // 100cm = 0km, 1m, 0cm.
I have done this code, but, sometimes I get negative numbers, and sometimes I get strange numbers.
Maybe is why I use the PHP_ROUND_HALF_UP??
What can I do to solve it?
//I have an HTML form to introduce the number value
$num = 123456789;
$km = $num/1000;
$km = round($km, 0, PHP_ROUND_HALF_UP);
echo "km: ".$km."<br>";
$subtraction = ($km * 1000) - $num;
$m = $subtraction / 100;
$m = round($m, 0, PHP_ROUND_HALF_UP);
echo "m: ".$m."<br>";
$subtraction2 = ($m * 100) - $subtraction;
$cm = $subtraction2;
echo "cm: ".$cm."<br>";
Honestly, your code seems to be a bit over complicated :P Ever heard of modulo division? Comes in handy in here!
<?php
$x = 123456789323; // centimeters
$km = floor($x / 100000); // amount of "full" kilometers
$rkm = $x % 100000; // rest
$m = floor($rkm / 100); // amount of "full" meters"
$cm = $rkm % 100; // rest
echo $x . ' cm = ' . $km . ' kilometers ' . $m . ' meters and ' . $cm . 'centimeters' . PHP_EOL;
$cm = 234459891345;
echo sprintf('km: %02d m: %02d cm: %02d', ($cm/100000),($cm/100%100), $cm%100);
Related
I need to validate that an inputted number is a valid number based on my stepping rules and round up to the nearest valid number if not. These numbers will change but one example would be:
$min = 0.25;
$step = 0.1
$qty = 0.75 // user input
so these would be valid inputs:
0.75
0.85
0.95
But these should round:
0.76 (to 0.85)
0.80 (to 0.85)
I thought I could use modulus somehow but not getting the calculation correct.
if (($qty % min) / $step == 0)) {
echo "good";
}
I've tried some variations of math that are likely very wrong
$step = 0.1;
$min = 0.25;
$qty = .85;
$h = ($qty / $min) / $step;
echo $h;
$j = mround($qty, $min-$step);
echo $j;
function mround($num, $parts) {
if ($parts <= 0) { $parts = 1; }
$res = $num * (1/$parts);
$res = round($res);
return $res /(1/$parts);
}
I think you can use fmod to do this.
$new = $original + ($step - fmod($original - $minimum, $step));
Example on 3v4l.org
currently i am making some "zoom in / zoom out" logic in php,
and now i have some problem with scaling value and reverting it back to original value in php (if applicable to js it would be awesome).
here is the codes
$scale = 1;
$currentScale = 1;
$position = 50;
$step = 5; // or 10
for($index = 1; $index <= $step; $index++){
echo('-----------------------------------------------------<br>');
$currentScale = $scale / $index;
$position = $position * $currentScale;
$originalPosition = (($scale / $currentScale) * $position) * (($scale / $currentScale) / $scale);
echo('current scale : ' . $currentScale . '<br>');
echo('current position : ' . $position . '<br>');
echo('original position : ' . $originalPosition . '<br>');
}
the goal is to make the $originalPosition value same as the first $position value (on every scale), i have tried many formula but no luck, hope you guys can help.
thanks in advance.
rule $originalPosition not stored before loop
ok, i think i found out the solution, i'll leave this code here in case somebody facing same problem
$scale = 1;
$currentScale = 1;
$position = 80; // change it to any value
$step = 5;
echo('original position : ' . $position . '<br>');
for($i = $step; $i >= 1; $i--){
echo('-----------------------------------------------------<br>');
$currentScale = ($scale / $step) * $i;
$position = $position * $currentScale;
$originalPosition = $position;
$maxLoop = $step - $i;
if($maxLoop){
for($j = $maxLoop; $j >= 0 ; $j--){
$tempScale = $scale - (($scale / $step) * $j);
$originalPosition = $originalPosition / $tempScale;
}
}
echo('max rescale loop : ' . $maxLoop . '<br>');
echo('current scale : ' . $currentScale . ' ( ' . $currentScale * 100 . '% )<br>');
echo('current position : ' . $position . '<br>');
echo('original position : ' . $originalPosition . '<br>');
}
exit;
here is the result, the original position stays the same as initial value
So i want to load a gpx file and get all the cordinates and calculate the total distance traveled, but im stuck with this code:
<?php
$xml = simplexml_load_file("data/example.gpx");
echo $xml->metadata->author->name;
echo "</br>";
echo "</br>";
echo "</br>";
$lon1=0;
$lat1=0;
$lon2=0;
$lat2=0;
$alt1=0;
$alt2=0;
$dist=0;
$brr = count($xml->trk->trkseg);
$brf = 0;
for($i = 0; $i<$brr; $i++){
$br = count($xml->trk->trkseg[$i]->trkpt);
$brf= $brf + $br;
for($j = 0; $j<$br;$j++){
$lat2= (float) $xml->trk->trkseg[$i]->trkpt[$j]['lat'];
$lon2= (float) $xml->trk->trkseg[$i]->trkpt[$j]['lon'];
$alt2= (float) $xml->trk->trkseg[$i]->trkpt[$j]->ele;
$lon2 = $alt2 * cos($lat2) * sin($lon2);
$lat2 = $alt2 * sin($lat2);
$alt2 = $alt2 * cos($lat2) * cos($lon2);
if ($j==0){ //this is just for the first time because first points are not set
$lat1=$lat2;
$lon1=$lon2;
$alt1= $alt2;
}
$cdist = sqrt(pow(($lat2-$lat1),2) + pow(($lon2-$lon1),2) + pow(($alt2-$alt1),2));
$dist = $dist + $cdist;
$lat1=$lat2;
$lon1=$lon2;
$alt1=$alt2;
}
}
echo 'Distance = '.$dist;
echo '</br>';
echo 'number of coordinates = '.$brf;
?>
as result for distance i get the number 4592.6244157763 instead of 4.10km (~~4100), but coordinates are good, it goes through all coordinates. (also, the gpx file is from endomondo if it matters)
Fixed the code, in case anyone needs it, here it is:
<?php
$xml = simplexml_load_file("data/vladantd.gpx");
echo $xml->metadata->author->name;
echo "</br>";
echo "</br>";
echo "</br>";
$lon1=0;
$lat1=0;
$lon2=0;
$lat2=0;
$alt1=0;
$alt2=0;
$dist=0;
$brr = count($xml->trk->trkseg);
$brf = 0;
for($i = 0; $i<$brr; $i++){
$br = count($xml->trk->trkseg[$i]->trkpt);
$brf= $brf + $br;
for($j = 0; $j<$br;$j++){
if($j==0)
{
$j=1;
$g=1;
}
$lat2= (float) $xml->trk->trkseg[$i]->trkpt[$j]['lat'];
$lon2= (float) $xml->trk->trkseg[$i]->trkpt[$j]['lon'];
$alt2= (float) $xml->trk->trkseg[$i]->trkpt[$j]->ele;
$lon2 = $alt2 * cos($lat2) * sin($lon2);
$lat2 = $alt2 * sin($lat2);
$alt2 = $alt2 * cos($lat2) * cos($lon2);
if ($g==1){
$lat1=$lat2;
$lon1=$lon2;
$alt1= $alt2;
$j=0;
$g=0;
}
$cdist = sqrt(pow(($lat2-$lat1),2) + pow(($lon2-$lon1),2) + pow(($alt2-$alt1),2));
$dist = $dist + $cdist;
$lat1=$lat2;
$lon1=$lon2;
$alt1=$alt2;
}
}
echo 'Distance = '.$dist;
echo '</br>';
echo 'number of coordinates = '.$brf;
?>
okay, so the files that i use, every first trkpt in every trkseg doesnt have an altitude, so if j==0 then set j to 1, so it skips that first point (it sets points 1 time every few seconds so it shouldnt make a big difference (in my case) and at the same time i've set another variable g to 1, so that we can at the end of this current loop, return j back to 0 so the j++ increases it to 1 instead of skipping the point and increasing it to 2, rest should be clear, if you have any questions, just ask ^^
EDIT:
The code isnt working good. The longer the distance, the bigger the mistake it makes, for 8.1km it shows 9km, tested it out on a few gpx files and noticed. Sorry, if someone has a solution, would mean a lot!
Ok, I don't even know where to start with this one! I'll try and explain what I want to achieve, and we'll go from there....
I have a list of dates each with an associated number, say from 20-100. What I want to do is to output the date in a shade which represents the associated number. So 20 would display in a light blue and 100 in a dark blue. My code so far looks like this...
dateArray = Array('2001-01-01'=>30, '2001-02-01'=>40, '2001-03-01'=>50, '2001-04-01'=>60, '2001-05-01'=>70, '2001-06-01'=>80, '2001-07-01'=>90, '2001-08-01'=>90, '2001-09-01'=>80, '2001-10-01'=>70, '2001-11-01'=>60, '2001-12-01'=>50)
$maxNum = max($dateArray);
$minNum = min($dateArray);
foreach($dateArray AS $date => $num){
$lightest = 'rgb(204,204,255)';
$darkest = 'rgb(0, 0, 179)';
///magic that converts $num into $shade goes here///
echo "<span style='color:$shade'>$date</span><br>"
}
Any ideas? Thanks
I would do something like that :
$dateArray = Array('2001-01-01'=>30, '2001-02-01'=>40, '2001-03-01'=>50, '2001-04-01'=>60, '2001-05-01'=>70, '2001-06-01'=>80, '2001-07-01'=>90, '2001-08-01'=>90, '2001-09-01'=>80, '2001-10-01'=>70, '2001-11-01'=>60, '2001-12-01'=>50)
// get max and min values
$maxNum = max($dateArray);
$minNum = min($dateArray);
// set rgb values for max and min
$lightest = array(204, 204, 255);
$darkest = array(0, 0, 179);
foreach($dateArray AS $date => $num)
{
// get a "delta" where the current num value is
$delta = ($num / $maxNum) - $minNum;
// get a pro-rata values thanks to $delta
$shadesNum = array(
$delta * ($lightest[0] - $darkest[0]) + $darkest[0],
$delta * ($lightest[1] - $darkest[1]) + $darkest[1],
$delta * ($lightest[2] - $darkest[2]) + $darkest[2]
);
echo "<span style='rgb(".implode(',', $shadesNum).")'>$date</span><br>";
}
Some languages have a "lerp" function - linear interpolation. Quite useful.
My suggestion:
for ($x1=20; $x1<=100; $x1+=10)
echo $x1 . ": " . getshade($x1) . "<br />\n";
function getshade($num) {
$rlight = 204;
$glight = 204;
$blight = 255;
$rdark = 0;
$gdark = 0;
$bdark = 179;
$lightnum = 20;
$darknum = 100;
$k01 = ($num-$lightnum)/($darknum-$lightnum); // 0 to 1
$rshade = ilerp($rlight, $rdark, $k01);
$gshade = ilerp($glight, $gdark, $k01);
$bshade = ilerp($blight, $bdark, $k01);
return "rgb($rshade,$gshade,$bshade)"; }
function lerp($start, $end, $k01) { // linear interpolation
return $k01*$end + (1.0-$k01)*$start; }
function ilerp($start, $end, $k01) { // integer lerp
return intval($k01*$end + (1.0-$k01)*$start); }
EDIT: Same thing but better:
$rgblight = [204,204,255];
$rgbdark = [0,0,179];
$numlight = 20;
$numdark = 100;
for ($x1=20; $x1<=100; $x1+=10)
echo $x1 . ": " . getshade2($x1, $numlight, $numdark, $rgblight, $rgbdark) . "<br />\n";
function getshade2($num, $numlight, $numdark, $rgblight, $rgbdark) {
$k01 = ($num-$numlight)/($numdark-$numlight);
for ($i1=0; $i1<3; $i1+=1)
$rgb[$i1] = ilerp($rgblight[$i1], $rgbdark[$i1], $k01);
return "rgb({$rgb[0]},{$rgb[1]},{$rgb[2]})"; }
Tracking devices have the data sent in the $GPRMC which is not what I use in my Code would be googling for a PHP converting method to decimal format no avail.
Just got the solution to this had to boil my head on the content of $GPRMC
sample format e.g $GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03
Where:
RMC Recommended Minimum sentence C
123519 Fix taken at 12:35:19 UTC
A Status A=active or V=Void.
4807.038,N Latitude 48 deg 07.038' N
01131.000,E Longitude 11 deg 31.000' E
022.4 Speed over the ground in knots
084.4 Track angle in degrees True
230394 Date - 23rd of March 1994
003.1,W Magnetic Variation
*6A The checksum data, always begins with *
And the code:
$gps = $_REQUEST['gps'];
if($gps){
$buffer = $gps;
if(substr($buffer, 0, 5)=='GPRMC'){
$gprmc = explode(',',$buffer);
$data1['lattitude_decimal'] = DMStoDEC($gprmc[3],'lattitude');
$data2['longitude_decimal'] = DMStoDEC($gprmc[5],'longitude');
$data = 'http://maps.google.com/maps?q='.$data1['lattitude_decimal'].','.$data2['longitude_decimal'].'+(PHP Decoded)&iwloc=A';
print_r($data);
echo "\n\n";
}
}
function DMStoDEC($dms, $longlat){
if($longlat == 'lattitude'){
$deg = substr($dms, 0, 2);
$min = substr($dms, 2, 8);
$sec = '';
}
if($longlat == 'longitude'){
$deg = substr($dms, 0, 3);
$min = substr($dms, 3, 8);
$sec='';
}
return $deg+((($min*60)+($sec))/3600);
}
?>
Hope this will help someone
That's the typical post request:
POST /RoyS/?acct=1234&dev=null&gprmc=$GPRMC,132201,A,3128.7540,N,14257.6714,W,000.0,000.0,290314,,*e HTTP/1.1" 200 33 "-" "-"
The line should be $gps = $_REQUEST['gprmc'];. Are you sure about this line?
if(substr($buffer, 0, 5)=='GPRMC') {
Shouldn't it be:
if(substr($buffer, 1, 5)=='GPRMC') {
?
and you definitely ignored NWSE letters!
formula acá
list($dato1, $dato2, $dato3, $lat, $dato5, $lon, $dato7, $velocidad, $dato9, $dato10, $dato11, $dato12) = explode(',', $input_gps);
$resultado_lat = $lat / 100;
list ($latitud_entero, $latitud_decimal) = explode('.', $resultado_lat);
$resultado_lat_minutos = $lat - ($latitud_entero * 100);
$resultado_lat_segundos = ($resultado_lat_minutos / 60);
$resultado_lat_final = $latitud_entero + $resultado_lat_segundos;
if ($dato5 == 'S'){
$resultado_lat_final = $resultado_lat_final * -1;
}
$resultado_lon = $lon / 100;
list ($longitud_entero, $longitud_decimal) = explode('.', $resultado_lon);
$resultado_lon_minutos = $lon - ($longitud_entero * 100);
$resultado_lon_segundos = ($resultado_lon_minutos / 60);
$resultado_lon_final = $longitud_entero + $resultado_lon_segundos;
if ($dato7 == 'W'){
$resultado_lon_final = $resultado_lon_final * -1;
}