Rotate arrowheads in radarplot with Canvas - php

I am trying to add arrowheads to my radar/spider plot coded in Canvas. Because I use PHP/SQL to retrieve some database values I echo most of the needed JS to plot it on the Canvas. So far I have drawn the axis (7) and guidelines (5 values).
How can I rotate the arrowheads so they show up correctly; with an angle and at the end of the 7 axis?
function drawArrows(context){
context.beginPath();
context.strokeStyle = "#ccc";
context.lineWidth = 2;
context.save();
<?php
$arrowHoek = 35;
$cHoek = (360/7);
$arrowLength = 10;
for ($i = 1 ; $i < 8 ; $i++) {
$arrow_xleft = round((getCoordinates($i,6,x))-(sin(deg2rad($arrowHoek))*$arrowLength),2);
$arrow_yleft = round((getCoordinates($i,6,y))+(cos(deg2rad($arrowHoek))*$arrowLength),2);
$arrow_xright = round((getCoordinates($i,6,x))+(sin(deg2rad($arrowHoek))*$arrowLength),2);
$arrow_yright = $arrow_yleft;
$arrow_rotation = deg2rad(($cHoek*$i)-$cHoek);
echo "\tcontext.moveTo(";
getCoordinates($i,6,null);
echo ");\n";
echo "\tcontext.lineTo($arrow_xleft, $arrow_yleft);";
echo "\n\tcontext.moveTo(";
getCoordinates($i,6,null);
echo ");\n";
echo "\tcontext.lineTo($arrow_xright, $arrow_yright);\n";
echo "\tcontext.rotate($arrow_rotation);\n";
echo "\tcontext.restore();\n";
echo "\tcontext.save();\n";
}
?>
context.stroke();
}

Seems I worked it out! As found in other topics this uses the save(), translate(), rotate() and restore() functions. If you put this in a seperate Javascript function you can make transformations to individual Canvas parts without messing up any other drawings.
Below is the solution (note this uses PHP to echo the Javascript function).
function drawArrows(context){
context.beginPath();
context.strokeStyle = "#ccc";
context.lineWidth = 2;
<?php
$arrowHoek = 35;
$cHoek = (360/7);
$arrowLength = 10;
for ($i = 1 ; $i < 8 ; $i++) {
$arrow_xleft = -(sin(deg2rad($arrowHoek))*$arrowLength);
$arrow_yleft = (cos(deg2rad($arrowHoek))*$arrowLength);
$arrow_xright = (sin(deg2rad($arrowHoek))*$arrowLength);
$arrow_yright = $arrow_yleft;
$arrow_rotation = deg2rad(($cHoek*$i)-$cHoek);
echo "\tcontext.save();\n";
echo "\tcontext.translate(";
getCoordinates($i,6,null);
echo ");\n";
echo "\tcontext.rotate($arrow_rotation);\n";
echo "\tcontext.moveTo($arrow_xleft, $arrow_yleft);";
echo "\n\tcontext.lineTo(0,0);\n";
echo "\tcontext.lineTo($arrow_xright, $arrow_yright);\n";
echo "\tcontext.restore();\n";
}
?>
context.stroke();
}
Note: The declaration of the context and canvas elements are in a container function.

Related

Can't understand why my php cosine similarity code isn't working

I'm working on a website for my friend's gaming clan. I'm trying to have a php code that takes the player's coordinates, loops over some known locations and print the name of the closest location.
I can't get it to work however. It always seems to want to print "Balota" which is index 1 in the array. I also noticed that the cosine function can't be working as all values are above 1.0.
But I'm completely stuck now. I think I've been beating my head over it for too long and its something right in front of me but I can't see it.
<?php
function DotProd($loc1,$loc2){
return array_sum(array_map(create_function('$a, $b', 'return $a * $b;'), $loc1, $loc2));
}
function CosineSim($loc1,$loc2){
return DotProd($loc2,$loc2)/sqrt(DotProd($loc1,$loc2)*DotProd($loc1,$loc2));
}
function Closest($loc){
$novo = array(11300,14300);
$balota = array(4500,2500);
$zelen = array(2500,5200);
$sever = array(7900,12600);
$vybor = array(4500,8200);
$nwaf = array(4500,10200);
$neaf = array(12100,12500);
$kamensk = array(7870,14600);
$bere = array(12900,10000);
$gorka = array(9600,8900);
$elektro = array(10100,2000);
$cherno = array(6600,2600);
$stary = array(6100,7700);
$novy = array(7000,7700);
$mysk = array(1100,7200);
$locations = array($novo,$balota,$zelen,$sever,$vybor,$nwaf,$neaf,
$kamensk,$bere,$gorka,$elektro,$cherno,$stary,$novy,$mysk);
$sim = 99999999999;
$locat = 0;
for($i = 14; $i >= 0; $i--){
$s = CosineSim($loc,$locations[$i]);
echo "<br>" . $i . " " . CosineSim($loc,$locations[$i]);
if($s < $sim){
$sim = $s;
$locat = $i;
}
}
$items = array("Novo","Balota","Zelenogorsk","Severograd","Vybor","NWAF","NEAF","Kamensk Military","Berezino",
"Gorka","Elektro","Cherno","Stary Sobor","Novy Sobor","Myshkino");
return $items[$locat];
}
$x = $_GET["xpos"];
$y = $_GET["ypos"];
$location = array($x,$y);
echo "<b>You are at " . $x . ":" . $y;
$index = Closest($location);
echo "<br>You're pretty close to ". $index . "<br>";
?>
I am using a distance calculation formula based on the link: https://www.mathwarehouse.com/algebra/distance_formula/index.php
I only changed CosineSim. The rest of the code remains the same. And you don't actually need DotProd. It's not elegant but it works for me.
function CosineSim($loc1,$loc2){
// sum of x coordinates
$x1 = ($loc1[0]-$loc2[0])*($loc1[0]-$loc2[0]);
// sum of y coordinates
$y2 = ($loc1[1]-$loc2[1])*($loc1[1]-$loc2[1]);
$summ = $x1 + $y2;
$sqrt_res = sqrt($summ);
return $sqrt_res;
}
If I enter something like:
You are at 4640:7205
...
You're pretty close to Vybor
Hope this helps!

How to find multiple points within a polygon

I am using the following code to test if a point is in a polygon, however I would like to make it work for multipliable points, but when I run it though a loop it fails to run correctly.
Polygon array = (52.62806176021313, 1.0546875),(52.435920583590125, 0.82672119140625),(52.26479561297205, 0.78277587890625),(52.24966453484505, 1.0986328125),(52.37224556866933, 1.34857177734375),(52.63973017532399, 1.46392822265625),(52.73795463681313, 1.25518798828125)
I split it up before hand.
Without Loop
$Latx = 52.5; //Y Point to test if inside
$LonX = 1.1; //X Point to test if inside
$VTX = $LON; //X array of polygon
$VTY = $LAT; //Y array of polygon
$POINTS = count($VTX) - 1; //number of polygon sides
//=====================================================
if (is_in_polygon($POINTS, $VTX, $VTY, $LonX, $LatY)){
echo "Is in polygon ($LatY,$LonX)";
echo "<br>";
}
else {
echo "Is not in polygon ($LatY,$LonX)";
}
echo "<br>"
function is_in_polygon($POINTS, $VTX, $VTY, $LonX, $LatY)
{
$i = $j = $c = 0;
for ($i = 0, $j = $POINTS ; $i < $POINTS; $j = $i++) {
if ((($VTY[$i] > $LatY != ($VTY[$j] > $LatY)) &&
($LonX < ($VTX[$j] - $VTX[$i]) * ($LatY - $VTY[$i]) /
($VTY[$j] - $VTY[$i]) + $VTX[$i]) ) )
$c = !$c;
}
return $c;
}
Trying with a Loop
for ($x=0; $x<=1000; $x++) {
$QU="SELECT lat,lon FROM DBTable LIMIT $x,1";
$RT=mysql_query($QU,$db);
if (!$RT) {
die("Invalid query: " . mysql_error());
}
while ($row = mysql_fetch_assoc($RT)){
$LonX[$x]=$row["lat"];
$LatY[$x]=$row["lon"];
if (is_in_polygon($POINTS, $VTX, $VTY, $LonX[$x], $LatY{$x])){
echo "Is in polygon ($LatY[$x],$LonX[$x])";
echo "<br>";
}
else echo "Is not in polygon ($LatY[$x],$LonX[$x])";
echo "<br>"
}
}

php javascript select

i need a help
in php i have a select box like this
<?php
$perpage = 5;
$total = 128;
$num = ceil( $total / $perpage );
$i = 1;
echo "<FORM NAME=\"form1\">";
echo "Select <SELECT NAME=\"select\" onChange=\"goto(this.form)\" SIZE=\"1\" >";
echo "<OPTION VALUE=\"\">----Select Page----";
for($i = 1; $i <= $num; $i++)
{
$sel = $i;
$goto = ($i - 1) * $perpage;
if($goto == 0) { $goto = ''; }
echo "<OPTION VALUE=\"http://localhost/CI_doctrine/blog/filmnews/" . $goto . "\">" . $i . "";
}
echo "</SELECT> Page";
echo "</FORM>";
?>
javascript code is here
function goto(form) {
var index = form.select.selectedIndex;
if (form.select.options[index].value != "0") {
window.location = form.select.options[index].value;
form.select.selected = form.select.options[index].value;
}
}
the code is working fine but i want to change the selected option to set the selected number after the page redirection but here iam getting the "select page" as the selected option
any help appreciated.
thank you from your friend.
Once you redirect, that page is unloaded and a new page loaded (even if it has the same items). When the new page loads, you want to do:
window.onload = function() {
var i, options = form.select.options, curUrl = window.location.href;
for (i = 0; i < options.length; i++) {
if (options[i].value == curUrl) {
form.select.selectedIndex= i;
break;
}
}
}
This will select the current URL. Make sure the URLs in the select options are full URLs (including http://). Also, window.onload is the DOM1 way. You should probably use a real library to deal with this.
Alternatively, you can also select the right input in PHP using the same basic approach.

Gaussian Chart with jpgraph

I´m trying to graph a gaussian chart in php. I found the jpgraph library, but I don´t know how to use the $x, $y data. Please somebody who knows how to declare the axis. Thanks. This is my code:
while ($row = pg_fetch_assoc($sql))
{
$ydata = array();
$xdata = array();
$ydata[] = abs(($row['grade']-$miu)/$sigma);
$xdata[] = 0;
for ($i = 0; $i <= 60; $i+=10) // put z value
{
$xdata[] = 0 + $i;
$ydata[] = 0 + $i;
}
$graph = new Graph(600,400,"auto");
$graph->SetScale("linlin");
$lplot = new LinePlot($ydata,$xdata);
$lplot->SetColor("blue");
$lplot->SetWeight(2);
$graph->Add($lplot);
// Add data to X coordinate
$graph->xaxis->SetTickLabels($xdata);
// Display the graph
$graph->Stroke();
}
I already resolved it.
while ($row = pg_fetch_assoc($sql))
{
$z[$i] = ($row['grade'] - $miu)/$sigma; // $z => arreglo con notas normalizadas
$ordenada[$i] = (1/($sigma*sqrt(pi())))*(exp(-0.5*(($row['grade']-$miu)*($row['grade'] - 57))/(12*12)));
$i++;
}
//rest of code
$p1 = new LinePlot($ordenada);
$graph->Add($p1);

Dynamically including multiple files within php for image descriptions

Working on implementing image descriptions to a php run gallery and can't seem to figure out how to call each text file for each individual image. I would need to place a div in the code for the image and then call the include.
//total number of images
$total = 77;
//max number of thumbnails per page
$max = 9;
//what image do we want to start from?
$startcount = $_GET["start"];
//if there is not a defined starting image, we start with the first
if(empty($startcount))
{
$startcount = 0;
}
//start off the loop at 1
$loop = 1;
//start the loop
while($loop <= $max)
{
//for the picture labels
$num = $startcount + $loop;
if($num > $total)
{
$num = $num - 1;
break;
}
// Add class="last" to every third list item
if(is_int($num / 3))
{
$last = ' class="last"';
}
else
{
$last = "";
}
//the code for the image
echo '
<li'.$last.'><img src="images/portfolio/thumbs/pic-'.$num.'-thumb.jpg" width="256" height="138" alt="Thumbnail of image '.$num.'" /><div>'.$num.'</div></li>';
I see that I can call the text files by number using '.$num.' (I have 77 individual text files with a phrase in each) but how do I tell it to call the files?
Assuming the description files are named something like "description_$num.txt", you can simply use readfile.
echo "<li${last}><a href='images/portfolio/pic-${num}.jpg' rel='milkbox[gall1]'>
<img src='images/portfolio/thumbs/pic-${num}-thumb.jpg' width='256'
height='138' alt='Thumbnail of image ${num}'/>
</a><div>";
readfile("description_${num}.txt");
echo '</div></li>';
Note that you don't "call" files in PHP, you either include them (which interprets them as scripts) or read them (which dumps them to the output).
For each file you need to do something like that:
<?php
$f=fopen($file,'r');
$data='';
while(!feof($f))
$data.=fread($f,$size);
fclose($f);
// do whatever you want with the file content.
?>
I made use of file_get_contents, and split the output a bit so you can more easily modify it:
<?php
$total = 77;
$max = 9;
$startcount = $_GET["start"];
$loop = 1;
if(empty($startcount)) $startcount = 0;
while($loop <= $max)
{
$num = $startcount + $loop;
if($num > $total)
{
$num--;
break;
}
$last = ($num % 3 == 0 ? ' class="last"' : '');
$output = "<li $last>";
$output .= "<a href=\"images/portfolio/pic-$num.jpg\" rel=\"milkbox[gall1]\">";
$output .= "<img src=\"images/portfolio/thumbs/pic-$num-thumb.jpg\" width=\"256\" height=\"138\" alt=\"Thumbnail of image $num\">";
$output .= "</a>";
$output .= "<div>";
$output .= file_get_contents("textfile-" . $num . ".txt"); // assuming this is where you want the phrase
$output .= "</div>";
$output .= "</li>";
echo $output;
}
?>

Categories