I'm trying to round off following figures:
Case 1:
round( ((4/6) * 100), 2 ) . '%'; = 66.67%
round( ((1/6) * 100), 2 ) . '%'; = 16.67%
round( ((1/6) * 100), 2 ) . '%'; = 16.67%
Total % = 66.67 + 16.67 + 16.67 = 100.01%
Case 2:
round( ((5/11) * 100), 2 ) . '%'; = 45.45%
round( ((3/11) * 100), 2 ) . '%'; = 27.27%
round( ((3/11) * 100), 2 ) . '%'; = 27.27%
Total % = 45.45 + 27.27 + 27.27 = 99.99%
Can anyone tell me how would I make it perfect 100%
Thanks
If a prefect 100 is 100.00, use this case:
round( ((4/6) * 100), 3 ); = 66.667
round( ((1/6) * 100), 3 ); = 16.667
round( ((1/6) * 100), 3 ); = 16.667
round(66.667+16.667+16.667, 2); = 100
echo round( ((5/11) * 100), 3 ); = 45.455
echo round( ((3/11) * 100), 3 ); = 27.273
echo round( ((3/11) * 100), 3 ); = 27.273
echo round(45.455+27.273+27.273, 2); = 100
You can make it more prefect by increasing rounded precision, e.g. use 15 instead of 3 for double values ;).
or
$a1 = (4/6)*100;
$b1 = (1/6)*100;
$c1 = (1/6)*100;
round($a1, 2);
round($b1, 2);
round($c1, 2);
round($a1+$b1+$c1, 2);
$a2 = (5/11)*100;
$b2 = (3/11)*100;
$c2 = (3/11)*100;
round($a2, 2);
round($b2, 2);
round($c2, 2);
round($a2+$b2+$c2, 2);
[PHP Demo]
Related
I'm trying to import a csv file with wp all import set specific functions to set the sale price and the offer price.
Now I would need to call the function that forms the regular price, inside the function of the offer price, to work on the result and maybe apply a percentage discount.
<?php
$GLOBALS['$commissione_paypal_lm'] = 4;
$GLOBALS['$spese_spedizione_moto_lm'] = 10;
$GLOBALS['$spese_spedizione_autocarro_lm'] = 10;
$GLOBALS['$spese_spedizione_varie_lm'] = 7;
$GLOBALS['$pfu_moto_lm'] = 1.50;
$GLOBALS['$pfu_autocarro_lm'] = 4.90;
$GLOBALS['$pfu_varie_lm'] = 2.90;
// First function for regular price,i take variables from csv and global variables
function prezzo_finale( $price = null, $pfu = null, $diametro = null ) {
if ( !empty( $price ) ) {
// strip any extra characters from price
$price = preg_replace("/[^0-9,.]/", "", $price);
$pfu = preg_replace("/[^0-9,.]/", "", $pfu);
//price ommas and dots fix
$price = str_replace(",",".",str_replace(".","",$price));
$pfu = str_replace(",",".",str_replace(".","",$pfu));
// calculate percentage
$percent = 0;
if ($diametro != '') {
$term = term_exists( $diametro, 'pa_diametro', 0 );
if ( $term !== 0 && $term !== null ) {
$percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
}
}
// final price
$prezzo_finale = $price;
if (empty( $percent ) ) {
// Se il campo percentuale è vuoto metto 20% automatico
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
}else if ($percent > 0){
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu + $prezzo_appoggio_finale);
}else{
// Se il campo percentuale è inferiore uguale a zero metto 20% automatico
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
}
// perform calculations
return $prezzo_finale;
}
}
function prezzo_finale_lm( $fifo_ponderato = null, $diametro = null, $settori_codice= null ) {
if ( !empty( $fifo_ponderato ) ) {
// strip any extra characters from price
$fifo_ponderato = preg_replace("/[^0-9,.]/", "", $fifo_ponderato);
//price ommas and dots fix
$fifo_ponderato = str_replace(",",".",str_replace(".","",$fifo_ponderato));
// calculate percentage
$percent = 0;
if ($diametro != '') {
$term = term_exists( $diametro, 'pa_diametro', 0 );
if ( $term !== 0 && $term !== null ) {
$percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
}
}
// final price
// moto = 10; vettura 8; isole extra valutare; paypal 3%;
$prezzo_finale_lm = ($fifo_ponderato);
if ($settori_codice === 'MOTO' || $settori_codice === 'SCOOTER' || $settori_codice === 'CICLOMOTORI'){
$spese_spedizione = $GLOBALS['$spese_spedizione_moto_lm'];
$pfu = $GLOBALS['$pfu_moto_lm'];
}else if ($settori_codice === 'AUTOCARRO'){
$spese_spedizione = $GLOBALS['$spese_spedizione_autocarro_lm'];
$pfu = $GLOBALS['$pfu_autocarro_lm'];
}else{
$spese_spedizione = $GLOBALS['$spese_spedizione_varie_lm'];
$pfu = $GLOBALS['$pfu_varie_lm'];
}
if (empty( $percent ) ) {
// Se il campo percentuale è vuoto metto 20% automatico
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}else if ($percent > 0){
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}else{
// Se il campo percentuale è inferiore uguale a zero metto 20% automatico
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}
// perform calculations
return $prezzo_finale_lm_paypal;
}
}
// Second function for offer price,i would take regular price and work with
function prezzo_offerta_lm() {
if (prezzo_finale_lm()){
return prezzo_finale_lm();
// and for example apply discount as global variable
}else{
}
// perform calculations
}
?>
This is shortcode i use for regular price:
[prezzo_finale_lm({fifo_ponderato[1]},{diametro[1]},{settori_codice[1]})]
Thank you all for any advice.
<?php
// First function for regular price,i take $price variable from imported csv column
function regular_price( $price = null) {
if ( !empty( $price ) ) {
// final price
$prezzo_finale = $price;
// perform calculations as example add markup or paypal commission
return $prezzo_finale;
}
}
// Second function for offer price,where i need to get returned value from first function
function offer_price($price = null)) {
if ( !empty( $price ) ) {
$offer_price = regular_price(); // i need to return here value from first function to perform discount calculation
// perform calculations
return $offer_price;
}
}
?>
Seems simple but can't figure this out:
$goal = 10.000;
$actual = 55.32;
$percentChange = number_format(( $actual / $goal) * 100, 2);
echo $percentChange;
OUTPUT
553.20
WANTED OUTPUT
0.5532
The problem occurs only when output is less than 1 0.XXXX code is working fine from 1 above.
Remove the dot in $goal variable as is taking it as 10 with 4 decimals
Change this:
$goal = 10.000;
$actual = 55.32;
$percentChange = number_format(( $actual / $goal) * 100, 2);
echo $percentChange;
To this:
$goal = 10000;
$actual = 55.32;
$percentChange = number_format(( $actual / $goal) * 100, 4);
echo $percentChange;
PHP with GD I built a quadrangle on the Cartesian plane.
I measured the 4 sides, result = 100 each (the corners are at 90 degrees).
Area calculation formula of Gauss.
https://en.wikipedia.org/wiki/Shoelace_formula
Area doverebbe be 10,000, but my result is 15,000?
Where is the mistake?
Thanks for your help.
$Image = imagecreate(400,400);
imagecolorallocate($Image,0,0,0);
$White = imagecolorallocate($Image,255,255,255);
$p[0] = 100; $p[1] = 200;$p[2] = 200;
$p[3] = 200;$p[4] = 200; $p[5] = 300;$p[6] = 100; $p[7] = 300;
$XY = array(
$p[0], $p[1], // section of the line $p[2], $p[3]
$p[2], $p[3], // section of the line $p[4], $p[5]
$p[4], $p[5], // section of the line $p[6], $p[7]
$p[6], $p[7] // section of the line $p[0], $p[1]
);
imagepolygon($Image, $XY, 4, $White);
// segment
$SegmentLengthA = sqrt(bcpow($p[0]-$p[2],2) + bcpow($p[1]-$p[3],2));
imagestring ($Image , 5 , 10 , 5 , "LunghSegmA = $SegmentLengthA" , $White );
$SegmentLengthB = sqrt(bcpow($p[2]-$p[4],2) + bcpow($p[3]-$p[5],2));
imagestring ($Image , 5 , 10 , 20 , "LunghSegmB = $SegmentLengthB" , $White );
$SegmentLengthC = sqrt(bcpow($p[4]-$p[6],2) + bcpow($p[5]-$p[7],2));
imagestring ($Image , 5 , 10 , 35 , "LunghSegmC = $SegmentLengthC" , $White );
$SegmentLengthD = sqrt(bcpow($p[6]-$p[0],2) + bcpow($p[7]-$p[1],2));
imagestring ($Image , 5 , 10 , 50 , "LunghSegmC = $SegmentLengthC" , $White );
////// perimeter
$Perimeter = $SegmentLengthA + $SegmentLengthB + $SegmentLengthC + $SegmentLengthD;
imagestring ($Image , 5 , 10 , 65 , "Perimeter = $Perimeter" , $White );
// area
$L1 = ($p[0] * $p[3]) + ($p[2] * $p[5]) + ($p[4] * $p[7]);
$L2 = ($p[1] * $p[2]) + ($p[3] * $p[4]) + ($p[5] * $p[6]);
$Area = abs($L1 - $L2) / 2;
imagestring ($Image , 5 , 10 , 80 , "Area = $Area" , $White );
imagepng($Image);
You need to end shoelace formula with initial cordinates.
$L1 = ($p[0] * $p[3]) + ($p[2] * $p[5]) + ($p[4] * $p[7]) + ($p[6] * $p[1]);
$L2 = ($p[1] * $p[2]) + ($p[3] * $p[4]) + ($p[5] * $p[6]) + ($p[7] * $p[0]);
I want to show percentages near or over each parts of the chart.
Do I have to convert coordinates from Polar to Cartesian?
To convert polar coordinates (r, θ) to rectangular coordinates (x, y) I used the following conversion:
x = r * cos( θ )
y = r * sin( θ )
in this graph
$x = $radius * cos( $Arc ); $y = $radius * sin( $Arc );
But results doesn't match (the percentages values aren't any close to each single parts of the chart), where I go wrong?
How can I fix it?
<?php
$W = 500; $H = 500;
$Image = imagecreate($W,$H);
$colors = array();
imagecolorallocate($Image,150,150,150); // grey background
$colors[0] = imagecolorallocate($Image,255,0,0); // red
$colors[1] = imagecolorallocate($Image,100,255,10); // green
$colors[2] = imagecolorallocate($Image,0,0,255); // bleu
$colors[3] = imagecolorallocate($Image,255,255,0); // yellow
$Country = array('England', 'Northern Ireland', 'Scotland', 'Wales');
$Population = array(53012456,1810863,5295403,3063456);
$TotPopulation = array_sum($Population);
$radius = 300; $StarAngle = 0;
foreach($Population as $index => $V )
{
$Arc = (360 * $V) / $TotPopulation;
$Percentage = (100 * $V) / $TotPopulation;
imagefilledarc(
$Image,
$W / 2, $H / 2, // center x,y
$radius,
$radius,
$StarAngle,
($StarAngle + $Arc), // end arc
$colors[$index],
IMG_ARC_PIE
);
$x = $radius * cos($Arc);
$y = $radius * sin($Arc);
imagestring($Image, 15, 5, 30 + $index*15 , 'x= '.number_format($x, 2, '.', '').' y='.number_format($y, 2, '.', '').' Country='.$Country[$index].' %='. number_format($Percentage, 2, '.', '').' ' , $colors[$index]);
$StarAngle += $Arc;
imagestring($Image, 15, 5, 430 + $index*15 , 'Country='.$Country[$index].' Population= $V %='.number_format($Percentage, 2, '.', '').' ' , $colors[$index]);
}
imagestring($Image, 5, 35, 10 , 'The population of the United Kingdom year 2011' , $colors[3]);
imagepng($Image);
You can try the library geo-math-php
composer require rkondratuk/geo-math-php:^1
Convert coordinates polar to cartesian:
<?php
use PhpGeoMath\Model\Polar3dPoint;
$polarPoint1 = new Polar3dPoint(
40.758742779050706, -73.97855507715238, Polar3dPoint::EARTH_RADIUS_IN_METERS
);
$cartesianpoint1 = $polarPoint1->buildCartesian3DPoint();
// Result:
$cartesianpoint1->x;
$cartesianpoint1->y;
$cartesianpoint1->z;
Convert coordinates cartesian to polar:
<?php
use PhpGeoMath\Model\Cartesian3dPoint;
$x = 1001;
$y = 205;
$z = 512;
$cartesianPoint2 = new Cartesian3dPoint($x, $y, $z);
$polarPoint2 = $cartesianPoint2->buildPolar3dPoint();
// Result:
$polarPoint2->lat;
$polarPoint2->lng;
$polarPoint2->radius;
When I'm using PHP's GD image library to draw shapes it always shows hard edges. I've tried to use GD's imageantialias() function but that is for straight lines only.
In order to solve the problem, I've searched some anti-aliasing algorithms and found FXAA works pretty well so I'm going to give it a try. I tried to port the FXAA anti-aliasing filter from the GLSL shader here.
Then, when I finished porting the FXAA shader to PHP, it doesn't give me the correct result. I test the FXAA filter using the imagecolorallocatealpha() example on PHP.net:
<?php
require('fxaa.php');
$size = 300;
$image=imagecreatetruecolor($size, $size);
// something to get a white background with black border
$back = imagecolorallocate($image, 255, 255, 255);
$border = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back);
imagerectangle($image, 0, 0, $size - 1, $size - 1, $border);
$yellow_x = 100;
$yellow_y = 75;
$red_x = 120;
$red_y = 165;
$blue_x = 187;
$blue_y = 125;
$radius = 150;
// allocate colors with alpha values
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);
$red = imagecolorallocatealpha($image, 255, 0, 0, 75);
$blue = imagecolorallocatealpha($image, 0, 0, 255, 75);
// drawing 3 overlapped circle
imagefilledellipse($image, $yellow_x, $yellow_y, $radius, $radius, $yellow);
imagefilledellipse($image, $red_x, $red_y, $radius, $radius, $red);
imagefilledellipse($image, $blue_x, $blue_y, $radius, $radius, $blue);
FXAA::process($image);
// don't forget to output a correct header!
header('Content-Type: image/png');
// and finally, output the result
imagepng($image);
imagedestroy($image);
?>
Here's the original image:
And this is the processed image:
Here's another test image. (The left is FXAA-processed and the right is not.)
The example images' colour are messed up with the background and the edges is smoothened too much. This is not the expected result as I think. I don't understand what's wrong with my code so I seek for your help.
Also, here's the FXAA class I wrote & the original GLSL shader:
<?php
class FXAA {
const FXAA_REDUCE_MIN = 0.0078125;
const FXAA_REDUCE_MUL = 0.125;
const FXAA_SPAN_MAX = 8;
static $w = 0;
static $h = 0;
private static function add($a, $b){
return array($a[0] + $b[0], $a[1] + $b[1], $a[2] + $b[2]);
}
private static function dot($a, $b){
return $a[0] * $b[0] + $a[1] * $b[1] + $a[2] * $b[2];
}
private static function texture2D($img, $pos){
if(($pos[0] >= self::$w || $pos[0] < 0) || ($pos[1] >= self::$h || $pos[1] < 0)){
return array(0, 0, 0, 0);
}
$color = imagecolorat($img, $pos[0], $pos[1]);
$a = ($color >> 24) & 0xFF;
$r = ($color >> 16) & 0xFF;
$g = ($color >> 8) & 0xFF;
$b = $color & 0xFF;
return array($r, $g, $b, $a);
}
public static function process($img){
self::$w = imagesx($img);
self::$h = imagesy($img);
for($x = 0; $x < imagesx($img); $x++){
for($y = 0; $y < imagesy($img); $y++){
$rgbNW = self::texture2D($img,array($x - 1, $y - 1));
$rgbNE = self::texture2D($img,array($x + 1, $y - 1));
$rgbSW = self::texture2D($img,array($x - 1, $y + 1));
$rgbSE = self::texture2D($img,array($x + 1, $y + 1));
$rgbaM = $rgbM = self::texture2D($img,array($x, $y));
$opacity = array_pop($rgbM);
$luma = array(76, 149, 29);
$lumaNW = self::dot($rgbNW, $luma);
$lumaNE = self::dot($rgbNE, $luma);
$lumaSW = self::dot($rgbSW, $luma);
$lumaSE = self::dot($rgbSE, $luma);
$lumaM = self::dot($rgbM, $luma);
$lumaMin = min($lumaM, min(min($lumaNW, $lumaNE ), min($lumaSW, $lumaSE)));
$lumaMax = max($lumaM, max(max($lumaNW, $lumaNE), max($lumaSW, $lumaSE)));
$dir = array(
-(($lumaNW + $lumaNE) - ($lumaSW + $lumaSE)),
(($lumaNW + $lumaSW) - ($lumaNE + $lumaSE))
);
$dirReduce = max(($lumaNW + $lumaNE + $lumaSW + $lumaSE ) * ( 0.25 * self::FXAA_REDUCE_MUL ), self::FXAA_REDUCE_MIN);
$rcpDirMin = 1 / (min(abs($dir[0]), abs($dir[1])) + $dirReduce);
$dir[0] = min(self::FXAA_SPAN_MAX, max(-self::FXAA_SPAN_MAX, $dir[0] * $rcpDirMin));
$dir[1] = min(self::FXAA_SPAN_MAX, max(-self::FXAA_SPAN_MAX, $dir[1] * $rcpDirMin));
$rgbA = self::add(
self::texture2D($img, array($x + $dir[0] * (1 / 3 - 0.5), $y + $dir[1] * (1 / 3 - 0.5))),
self::texture2D($img, array($x + $dir[0] * (2 / 3 - 0.5), $y + $dir[1] * (1 / 3 - 0.5)))
);
$rgbA[0] *= 0.5;
$rgbA[1] *= 0.5;
$rgbA[2] *= 0.5;
$rgbB = self::add(
self::texture2D($img, array($x + $dir[0] * -0.5, $y + $dir[1] * -0.5)),
self::texture2D($img, array($x + $dir[0] * 0.5, $y + $dir[1] * 0.5))
);
$rgbB[0] = $rgbB[0] * 0.25 + $rgbA[0] * 0.5;
$rgbB[1] = $rgbB[1] * 0.25 + $rgbA[1] * 0.5;
$rgbB[2] = $rgbB[2] * 0.25 + $rgbA[2] * 0.5;
$lumaB = self::dot($rgbB, $luma);
if(($lumaB < $lumaMin) || ($lumaB > $lumaMax)){
imagesetpixel($img, $x, $y, imagecolorallocatealpha($img, $rgbA[0], $rgbA[1], $rgbA[2], $opacity));
}
else {
imagesetpixel($img, $x, $y, imagecolorallocatealpha($img, $rgbB[0], $rgbB[1], $rgbB[2], $opacity));
}
}
}
}
}
Original GLSL Shader:
uniform sampler2D tDiffuse;
uniform vec2 resolution;
varying vec2 vUv;
#define FXAA_REDUCE_MIN (1.0/128.0)
#define FXAA_REDUCE_MUL (1.0/8.0)
#define FXAA_SPAN_MAX 8.0
void main() {
vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;
vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;
vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;
vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;
vec4 rgbaM = texture2D( tDiffuse, gl_FragCoord.xy * resolution );
vec3 rgbM = rgbaM.xyz;
float opacity = rgbaM.w;
vec3 luma = vec3( 0.299, 0.587, 0.114 );
float lumaNW = dot( rgbNW, luma );
float lumaNE = dot( rgbNE, luma );
float lumaSW = dot( rgbSW, luma );
float lumaSE = dot( rgbSE, luma );
float lumaM = dot( rgbM, luma );
float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );
float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );
vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );
float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );
dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),dir * rcpDirMin)) * resolution;
vec3 rgbA = 0.5 * (
texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 1.0 / 3.0 - 0.5 ) ).xyz +
texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 2.0 / 3.0 - 0.5 ) ).xyz );
vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * -0.5 ).xyz +
texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * 0.5 ).xyz );
float lumaB = dot( rgbB, luma );
if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {
gl_FragColor = vec4( rgbA, opacity );
} else {
gl_FragColor = vec4( rgbB, opacity );
}
}
The problem with your code is that it operates on a single image, and thus the anti-aliasing operation reads its own output.
Drawing the output of the anti-aliasing operation into a a new image will work.