I'm trying to plot binomial curves using R scripts which are executed by PHP loops. The scripts are taking a very long time to run and I want to improve the algorithm to run faster.
The input values are:
$xmax = 360;
$p = 0.975;
$prvn = 1;
$b = 1.7;
$c = 0.995;
The PHP function called for each loop is:
function cg_graphs_get_binomial($xmax, $p, $prvn = 1, $b = 1.7, $c = 0.99){
$Alert = array();
/*run the Rscript file located in the module root*/
$Rgennloc = "/home/rcstest/www/".drupal_get_path('module', 'cg_graphs')."/Rbinomgenn.R"; //Rscript file location
$Rbinomloc = "/home/rcstest/www/".drupal_get_path('module', 'cg_graphs')."/Rbinomnew.R"; //Rscript file location
for($i = 0; $i <= $xmax; $i++){
exec("Rscript --slave ".$Rgennloc." ".$prvn." ".$i." ".$b, $n);
$ne = explode('[1]', $n[$i]);
$prvn = $ne[1];
exec("Rscript --slave ".$Rbinomloc." ".$prvn." ".$p." ".$c, $alert);
$at = explode('[1]', $alert[$i]);
$Alert[] = trim($at[1]);
}
return $Alert; //return the data array
The first R script called ($Rgennloc) generates the n value, based on the n value of the previous loop, or 1 if it is the first loop. This increments as follows (etc):
1
6
16
32
53
80
The first r script looks like this and runs in relatively short amount of time:
#!/usr/bin/Rscript
#grab args as passed into via CLI
args <- commandArgs(trailingOnly = TRUE)
#R script to generate n value
#implimentation of excel ROUNDDOWN function
ROUNDDOWN <- function(.number, .num_digits){
return(as.integer(.number*10^.num_digits)/(10^.num_digits))
}
#generate n
n <- function(.prvn, .xaxis, .B){
return(.prvn + ROUNDDOWN(.xaxis * exp(1)^.B, 0))
}
#wrapper function
n(as.integer(args[1]), as.integer(args[2]), as.double(args[3]))
When the second script is called, it runs quickly for about the first 20 calls (where n gets to around 1000 and xaxis is 20) but then it starts to slow down.
The second script:
#!/usr/bin/Rscript
# replace '/usr/bin' with actual R executable
args <- commandArgs(trailingOnly = TRUE)
#Critbinom - R implimentation of the excel function
CRITBINOM <- function(.trials, .probability_s, .alpha){
i <- 0
while(sum(dbinom(0:i, .trials, .probability_s)) < .alpha){
i <- i + 1
}
return(i)
}
# Binomdist - R implimentation of the excel function
BINOMDIST <- function(.number_s, .trials, .probability_s, .cumulative){
if(.cumulative){
return(sum(dbinom(0:.number_s, .trials, .probability_s)))
}else{
return(choose(.trials,.number_s)*.probability_s^.number_s*(1-.probability_s)^(.trials-.number_s))
}
}
# Iserror - R version of this, no need for all excel functionality.
ISERROR <- function(.value){
return(is.infinite(.value))
}
# Generate the alert
generate_Alert <- function(.n, .probability_s, .alpha){
critB <- CRITBINOM(.n, .probability_s, .alpha)
adj <- critB-(BINOMDIST(critB, .n, .probability_s,TRUE)-.alpha)/(BINOMDIST(critB, .n, .probability_s,TRUE)-BINOMDIST(critB-1, .n, .probability_s,TRUE))
if(ISERROR(100 * adj / .n)){
return(0)
}else{
adj_value <- (adj / .n)
return(adj_value)
}
}
# Generate the alert for current xaxis position
generate_data <- function(.n, .probability_s, .alpha){
Alert <- generate_Alert(.n, .probability_s, .alpha)
return(Alert)
}
# Call wrapper function generate_data(n, p, alpha)
generate_data(as.integer(args[1]), as.double(args[2]), as.double(args[3]))
The xaxis value may get as high as 360, but the script starts slowing down before xaxis gets to 30. By the time xaxis is at 100 it takes some 30 seconds to complete each loop, it just gets worse from there.
What is the best way of optimizing this? I think its only using 1 core at the moment. I have 2 available but I'm not sure how much difference the second core will make in the long run.
I am using the latest version of R.
Expanding my comment a bit, so this question gets an answer:
A while loop in R is a very unsual construct (I see it only once or twice a year in serious code). It's often an indicator that the code does not follow the spirit of R, but was written by someone with experience from other language (e.g., from the C familiy). while loops are very expensive performance-wise in R and if really needed should be better written in C.
Fortunately, the CRITBINOM function is just a naive re-implementation of qbinom (quantile function of the binomial distribution), which can be used instead. The only difference is in how multiple success probabilities are handled (qbinom is fully vectorized).
I believe a full reimplementation in R (avoiding explicit loops) could get this down to seconds or less, but I don't know PHP.
Related
Say I have this tree:
root
/ \
1 2
/ \ /
3 4 5
/
6
/ \
8 7
/ \
9 12
/
10
\
11
I want to select root, then left, the result will be 3. If right, it will be 2.
If I select 6, then the left will be 10, right will be 12.
I have been using this https://packagist.org/packages/kalnoy/nestedset. For now I have been using recursive from top to bottom to find (querying from database to find next left child), but it is not efficient, as the tree will become bigger.
I have tried using this (to find left):
if (!$child = self::where('parent_id', $this->id)->where('position', 'left')->first()) {
return false;
}
if (!$grandChild = $child->leftChild()) {
return $child;
}
$outerLeftChild = $child->descendants()->where('position', 'left')->whereIsLeaf()->get();
if (count($outerLeftChild) <= 0) {
$outerLeftChild = $child->descendants()->where('position', 'left')->hasChildren()->get();
if (count($outerLeftChild) <= 0) {
return $child;
}
}
return $outerLeftChild->sortBy('_lft')->last();
but sometimes, it will find the wrong node, like if I select root, it might select 10 instead of 3, _lft comes from the package I have no idea what it actually does.
I have level, position (left or right), and parent_id for each node data. Each node will only have 2 nodes max (left & right), but it can also one node or nothing at all.
Can anyone at least tell me the theory for this solution?
I'm currently using a Pepper Robot to take photos that I use to detect object from coco_classes with a yolov3 algorithm (https://github.com/qqwweee/keras-yolo3) located on my Windows 10 computer. I made an app that can be used on Pepper Tablet using responsive (html, bootstrap, php, ...), to select the object you want to recognize. I'm struggling to pass the class name value from php to a python file called yolo_video.py, it's unlucky because I really want the robot to point out the recognized object, so I need to interact with this Python file.
One important thing to know is that I use Anaconda to call the script and have GPU acceleration.
I tried every single command to pass a value from PHP to Python, sometimes it works, but the script do not complete is mission when it's the case (It's really strange because called from a command prompt it's working fine). With no input, the code works fine even when called from php.
I try to use exec() command because it returns an array-like value witch I can use to take back the last printed element of the python script (witch is angle information that need to take the robot to point the object).
here is yolo_detect.php :
<?php
$classe = $_GET ["choice"];
exec("C:/Anaconda/Scripts/activate yolo && cd C:/wamp64/www/app/projetba3/ && python yolo_video.py --image $who", $value);
echo "valeur envoye ".$class;
var_dump ($value)
?>
You can see that i try to call the yolo_video.py after activating my yolo environment with the parameter --image (Because i do the recognition on one image taken by the robot). I wonder if that argument can cause problems ?
Yolo_video.py :
import sys
import argparse
from yolo import YOLO, detect_video
from PIL import Image
def detect_img(yolo):
while True:
img = "C:\wamp64\www\App\projetba3\camImage.png"
try:
image = Image.open(img)
except:
print('Open Error! Try again!')
continue
else:
r_image, angle = yolo.detect_image(image,str(classe)) #img passe dans le réseau de neurone
print(angle)
#r_image.show()
r_image.save("C:\wamp64\www\App\projetba3\camerapepper.png")
break;
yolo.close_session()
FLAGS = None
if __name__ == '__main__':
classe = sys.argv[1]
print(sys.argv[1])
# class YOLO defines the default value, so suppress any default here
parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
'''
Command line options
'''
parser.add_argument(
'--model', type=str,
help='path to model weight file, default ' + YOLO.get_defaults("model_path")
)
parser.add_argument(
'--anchors', type=str,
help='path to anchor definitions, default ' + YOLO.get_defaults("anchors_path")
)
parser.add_argument(
'--classes', type=str,
help='path to class definitions, default ' + YOLO.get_defaults("classes_path")
)
parser.add_argument(
'--gpu_num', type=int,
help='Number of GPU to use, default ' + str(YOLO.get_defaults("gpu_num"))
)
parser.add_argument(
'--image', default=True, action="store_true",
help='Image detection mode, will ignore all positional arguments'
)
'''
Command line positional arguments -- for video detection mode
'''
parser.add_argument(
"--input", nargs='?', type=str,required=False,default='./path2your_video',
help = "Video input path"
)
parser.add_argument(
"--output", nargs='?', type=str, default="",
help = "[Optional] Video output path"
)
FLAGS = parser.parse_args()
if FLAGS.image:
"""
Image detection mode, disregard any remaining command line arguments
"""
print("Image detection mode")
if "input" in FLAGS:
print(" Ignoring remaining command line arguments: " + FLAGS.input + "," + FLAGS.output)
detect_img(YOLO(**vars(FLAGS)))
elif "input" in FLAGS:
detect_video(YOLO(**vars(FLAGS)), FLAGS.input, FLAGS.output)
else:
print("Must specify at least video_input_path. See usage with --help.")
You can see that i use the simple sys.argv[1] to put the value in classe variable. This value is transmitted to another python file yolo.py inside the function yolo.detect_image(image,str(classe)). I added in the function the calculation for the robot position that i need to have. Here is the function :
def detect_image(self, image, classe):
start = timer()
if self.model_image_size != (None, None):
assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
else:
new_image_size = (image.width - (image.width % 32),
image.height - (image.height % 32))
boxed_image = letterbox_image(image, new_image_size)
image_data = np.array(boxed_image, dtype='float32')
print(image_data.shape)
image_data /= 255.
image_data = np.expand_dims(image_data, 0) # Add batch dimension.
out_boxes, out_scores, out_classes = self.sess.run(
[self.boxes, self.scores, self.classes],
feed_dict={
self.yolo_model.input: image_data,
self.input_image_shape: [image.size[1], image.size[0]],
K.learning_phase(): 0
})
print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
thickness = (image.size[0] + image.size[1]) // 300
angle = (0,0)
for i, c in reversed(list(enumerate(out_classes))):
predicted_class = self.class_names[c]
box = out_boxes[i]
score = out_scores[i]
label = '{} {:.2f}'.format(predicted_class, score)
draw = ImageDraw.Draw(image)
label_size = draw.textsize(label, font)
top, left, bottom, right = box
top = max(0, np.floor(top + 0.5).astype('int32'))
left = max(0, np.floor(left + 0.5).astype('int32'))
bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
print(label, (left, top), (right, bottom))
if str(predicted_class)== classe :
ite =+ 1
if ite == 1 :
centresofa = (left+(right-left)/2,top+(bottom-top)/2)
print (centresofa)
anglehor = 55.2*(centresofa[0]/640)
print (anglehor)
if anglehor > 27.2 :
anglehor = anglehor - 27.2
if anglehor < 27.2 :
anglehor = -(27.2 - anglehor)
else :
anglehor = 0
anglever = 44.3*(centresofa[1]/480)
print(anglever)
if anglever < 22.15 :
anglever = 22.15-anglever
if anglever > 22.15 :
anglever = -(anglever-22.15)
else :
anglever = 0
print ("angle horizontal "+str(anglehor)+"angle vertical "+str(anglever))
angle = (anglehor,anglever)
if top - label_size[1] >= 0:
text_origin = np.array([left, top - label_size[1]])
else:
text_origin = np.array([left, top + 1])
# My kingdom for a good redistributable image drawing library.
for i in range(thickness):
draw.rectangle(
[left + i, top + i, right - i, bottom - i],
outline=self.colors[c])
draw.rectangle(
[tuple(text_origin), tuple(text_origin + label_size)],
fill=self.colors[c])
draw.text(text_origin, label, fill=(0, 0, 0), font=font)
del draw
end = timer()
print(end - start)
return image, angle
I should take the values back in the last element of var_dump($values), instead, it just shows me print(sys.argv[1]) value and nothing else (sign that the script stops just after it started). A more strange fact is that his value is "--image" (the argument of my function). How can i solve this big mess ?
How is this exec() function really working with input arguments ???
Python
python yolo_video.py --image $who
sys.argv[0] = yolo_video.py
sys.argv[1] = --image
sys.argv[2] = $who (classname)
I believe you are passing argv[1] "--image" as the class name, when you should be passing argv[2]
https://www.pythonforbeginners.com/argv/more-fun-with-sys-argv
Also, you are trying to use classe as a global variable within detect_img(). This is not a good way to do this and you would have to add a global keyword to make it work.
Change detect_img(yolo) to detect_img(yolo, classe) and pass the actual classe variable to the function via sys.argv[2]
Line 83: detect_img(YOLO(**vars(FLAGS)), sys.argv[2])
PHP
echo "valeur envoye ".$class;
should be
echo "valeur envoye ".$classe;
In a previous Using R, how to reference variable variables (or variables variable) a la PHP[post]
I asked a question about something in R analagous to PHP $$ function:
Using R stats, I want to access a variable variable scenario similar to PHP double-dollar-sign technique: http://php.net/manual/en/language.variables.variable.php
Specifically, I am looking for a function in R that is equivalent to $$ in PHP.
The get( response works for strings (characters).
lapply is a way to loop over lists
Or I can loop over and get the values ...
for(name in names(vars))
{
val = vars[[name]];
I still haven't had the $$ function in R answered, although the lapply solved what I needed in the moment.
`$$` <- function
that allows any variable type to be evaluated. That is still the question.
UPDATES
> mlist = list('four'="score", 'seven'="years");
> str = 'mlist$four'
> mlist
$four
[1] "score"
$seven
[1] "years"
> str
[1] "mlist$four"
> get(str)
Error in get(str) : object 'mlist$four' not found
> mlist$four
[1] "score"
Or how about attributes for an object such as mobj#index
UPDATES #2
So let's put specific context on the need. I was hacking the texreg package to build a custom latex output of 24 models of regression for a research paper. I am using plm fixed effects, and the default output of texreg uses dcolumns to center, which I don't like (I prefer r#{}l, so I wanted to write my own template. The purpose for me, to code this, is for me to write extensible code that I can use again and again. I can rebuild my 24 tables across 4 pages in seconds, so if the data change, or if I want to tweak the function, I immediately have a nice answer. The power of abstraction.
As I hacked this, I wanted to get more than the number of observations, but also the number of groups, which can be any user defined index. In my case it is "country" (wait for it, hence, the need for variable variables).
If I do a lookup of the structure, what I want is right there: model$model#index$country which would be nice to simply call as $$('model$model#index$country'); where I can easily build the string using paste. Nope, this is my workaround.
getIndexCount = function(model,key="country")
{
myA = attr(summary(model)$model,"index");
for(i in 1:length(colnames(myA)))
{
if(colnames(myA)[i] == key) {idx = i; break;}
}
if(!is.na(idx))
{
length(unique(myA[,idx]));
} else {
FALSE;
}
}
UPDATES #3
Using R, on the command line, I can type in a string and it gets evaluated. Why can't that internal function be directly accessed, and the element captured that then gets printed to the screen?
There is no equivalent function in R. get() works for all types, not just strings.
Here is what I came up with, after chatting with the R-bug group, and getting some ideas from them. KUDOS!
`$$` <- function(str)
{
E = unlist( strsplit(as.character(str),"[#]") );
k = length(E);
if(k==1)
{
eval(parse(text=str));
} else {
# k = 2
nstr = paste("attributes(",E[1],")",sep="");
nstr = paste(nstr,'$',E[2],sep="");
if(k>2) {
for(i in 3:k)
{
nstr = paste("attributes(",nstr,")",sep="");
nstr = paste(nstr,'$',E[i],sep="");
}
}
`$$`(nstr);
}
}
Below are some example use cases, where I can directly access what the str(obj) is providing... Extending the utility of the '$' operator by also allowing '#' for attributes.
model = list("four" = "score", "seven"="years");
str = 'model$four';
result = `$$`(str);
print(result);
matrix = matrix(rnorm(1000), ncol=25);
str='matrix[1:5,8:10]';
result = `$$`(str);
print(result);
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14);
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69);
group <- gl(2, 10, 20, labels = c("Ctl","Trt"));
weight <- c(ctl, trt);
lm.D9 <- lm(weight ~ group);
lm.D90 <- lm(weight ~ group - 1); # omitting intercept
myA = anova(lm.D9); myA; str(myA);
str = 'myA#heading';
result = `$$`(str);
print(result);
myS = summary(lm.D90); myS; str(myS);
str = 'myS$terms#factors';
result = `$$`(str);
print(result);
str = 'myS$terms#factors#dimnames';
result = `$$`(str);
print(result);
str = 'myS$terms#dataClasses#names';
result = `$$`(str);
print(result);
After realizing the back-tick can be a bit tedious, I chose to update the function, calling it access
access <- function(str)
{
E = unlist( strsplit(as.character(str),"[#]") );
k = length(E);
if(k==1)
{
eval(parse(text=str));
} else {
# k = 2
nstr = paste("attributes(",E[1],")",sep="");
nstr = paste(nstr,'$',E[2],sep="");
if(k>2) {
for(i in 3:k)
{
nstr = paste("attributes(",nstr,")",sep="");
nstr = paste(nstr,'$',E[i],sep="");
}
}
access(nstr);
}
}
I need to go through an array containing points in a map and check their distance from one another. I need to count how many nodes are within 200m and 50m of each one. It works fine for smaller amounts of values. However when I tried to run more values through it (around 4000 for scalability testing) an error occurs saying that I have reached the maximum execution time of 300 seconds. It needs to be able to handle at least this much within 300 seconds if possible.
I have read around and found out that there is a way to disable/change this limit, but I would like to know if there is a simpler way of executing the following code so that the time it takes to run it will decrease.
for($i=0;$i<=count($data)-1;$i++)
{
$amount200a=0;
$amount200p=0;
$amount50a=0;
$amount50p=0;
$distance;
for($_i=0;$_i<=count($data)-1;$_i++)
{
$distance=0;
if($data[$i][0]===$data[$_i][0])
{
}
else
{
//echo "Comparing ".$data[$i][0]." and ".$data[$_i][0]." ";
$lat_a = $data[$i][1] * PI()/180;
$lat_b = $data[$_i][1] * PI()/180;
$long_a = $data[$i][2] * PI()/180;
$long_b = $data[$_i][2] * PI()/180;
$distance =
acos(
sin($lat_a ) * sin($lat_b) +
cos($lat_a) * cos($lat_b) * cos($long_b - $long_a)
) * 6371;
$distance*=1000;
if ($distance<=50)
{
$amount50a++;
$amount200a++;
}
else if ($distance<=200)
{
$amount200a++;
}
}
}
$amount200p=100*number_format($amount200a/count($data),2,'.','');
$amount50p=100*number_format($amount50a/count($data),2,'.','');
/*
$dist[$i][0]=$data[$i][0];
$dist[$i][1]=$amount200a;
$dist[$i][2]=$amount200p;
$dist[$i][3]=$amount50a;
$dist[$i][4]=$amount50p;
//*/
$dist.=$data[$i][0]."&&".$amount200a."&&".$amount200p."&&".$amount50a."&&".$amount50p."%%";
}
Index 0 contains the unique ID of each node, 1 contains the latitude of each node and
index 2 contains the longitude of each node.
The error occurs at the second for loop inside the first loop. This loop is the one comparing the selected map node to other nodes. I am also using the Haversine Formula.
first of all, you are performing in big O notation: O(data^2), which is gonna be slow as hell , and really, either there are 2 possible solutions. Find a proven algorithm that solves the same problem in a better time. Or if you cant, start moving stuff out of the innner for loop, and mathmatically prove if you can convert the inner for loop to mostly simple calculations, which is often something you can do.
after some rewriting, I see some possiblities:
If $data is not a SPLFixedArray (which has a FAR Better access time, ) then make it. since you are accessing that data so many times (4000^2)*2.
secound, write cleaner code. although the optizmier will do its best, if you dont try either to minize the code (which only makes it more readable), then it might not be able to do it as well as possible.
and move intermediate results out of the loops, also something like the size of the array.
Currently you're checking all points against all other points, where in fact you only need to check the current point against all remaining points. The distance from A to B is the same as the distance from B to A, so why calculate it twice?
I would probably make an adjacent array that counts how many nodes are within range of each other, and increment pairs of entries in that array after I've calculated that two nodes are within range of each other.
You should probably come up with a very fast approximation of the distance that can be used to disregard as many nodes as possible before calculating the real distance (which is never going to be super fast).
Generally speaking, beyond algorithmic optimisations, the basic rules of optimisation are:
Don't any processing that you don't have to do: Like not multiplying $distance by 1000. Just change the values you're testing against from 20 and 50 to 0.02 and 0.05, respectively.
Don't call any function more often than you have to: You only need to call count($data) once before any processing starts.
Don't calculate constant values more than once: PI()/180, for example.
Move all possible processing outside of loops. I.e. precalculate as much as possible.
Another minor point which will make your code a little easier to read:
for( $i = 0; $i <= count( $data ) - 1; $i++ ) is the same as:
for( $i = 0; $i < count( $data ); $i++ )
Try this:
$max = count($data);
$CONST_PI = PI() / 180;
for($i=0;$i<$max;$i++)
{
$amount200a=0;
$amount50a=0;
$long_a = $data[$i][2] * $CONST_PI;
$lat_a = $data[$i][1] * $CONST_PI;
for($_i=0;$_i<=$max;$_i++)
//or use for($_i=($i+1);$_i<=$max;$_i++) if you did not need to calculate already calculated in other direction
{
$distance=0;
if($data[$i][0]===$data[$_i][0]) continue;
$lat_b = $data[$_i][1] * $CONST_PI;
$long_b = $data[$_i][2] * $CONST_PI;
$distance =
acos(
sin($lat_a ) * sin($lat_b) +
cos($lat_a) * cos($lat_b) * cos($long_b - $long_a)
) * 6371;
if ($distance<=0.2)
{
$amount200a++;
if ($distance<=0.05)
{
$amount50a++;
}
}
} // for %_i
$amount200p=100*number_format($amount200a/$max,2,'.','');
$amount50p=100*number_format($amount50a/$max,2,'.','');
$dist.=$data[$i][0]."&&".$amount200a."&&".$amount200p."&&".$amount50a."&&".$amount50p."%%";
} // for $i
It will be better to read I think and if you change the commented out line of the for $_i it will be faster at all :)
I have a rectangular map, stored as multidimensional array (ie $map[row][col]) and I have to track down which squares are seen by a player, placed anywhere on this map.
Player visibility is circular with unknown radius (but given at run-time) and I only need integer solutions.
I know that circumference formula is
x^2 + y^2 <= r^2
but how can I store everything?
I need these values since then I can "reveal" map squares.
The best would be a multidimesional array (ie __$sol[x][y]__).
This is a piece of code that I'm using. It's not corrected since it assumes that vision is a square and not a circle.
Calculating the square
$this->vision_offsets_2 = array();
//visibility given as r^2
$mx = (int)(sqrt($this->viewradius2));
$mxArr = range($mx * -1, $mx + 1);
foreach ($mxArr as $d_row)
{
foreach ($mxArr as $d_col)
{
$this->vision_offsets_2[] = array($d_row, $d_col);
}
}
This is how I apply that
foreach($player as $bot)
{
foreach($visibility as $offset)
{
$vision_row = $offset[0] + $bot[0];
$vision_col = $offset[1] + $bot[1];
if(isset($map[$vision_row][$vision_col]))
{
if( $map[$vision_row][$vision_col] == UNSEEN) {
$map[$vision_row][$vision_col] = LAND; }
}
}
}
Here you can find the bot view: as you can see is a non perfect circle.
How can I track it? By the way, in this example radius^2 is 55, the orange circle is the player, brown squares are visible ones.
Structure
You're already referencing terrain in a grid. Store terrain objects in those grid values. Apply attributes to those objects. Check with something like
$map[$x][$y]->isVisible($player);
You'll need some methods in there for setting vision and tests for checking the user that is passed against a list of users who can see it. While you're at it, setup other related methods in those objects (I see references to land... isLand() and isWater() perhaps?).
You can even have vision cascade within objects such that you only need to move the position of a user and the object takes care of triggering off all the code to set nearby plots of land to visible.
Math
We are given circumference.
double diameter = circumference / 3.14159
double radius = diameter / 2 //Normally done in one step / variable
Now we must know the distance between two points to compare it. Let's use map[4][7] and map[3][9].
int x0 = 4;
int y0 = 7;
int x1 = 3;
int y1 = 9;
double distance = Math.sqrt(
Math.pow(x0 - x1, 2) +
Math.pow(y0 - y1, 2)
);
System.out.println(distance); //2.23606797749979
Test distance > radius.
Testing each square
Put the above in a method: visibleFrom(Square target)
radius should be a globally accessible static variable when comparing.
Your Square object should be able to hand over its coordinates.
target.getX()
target.getY()
Some optimizations can be had
Only checking things for circular distance when they're in the square.
Not checking anything for circular distance when purely along the x or y axis.
Figuring out the largest square that fits inside the circle and not checking boxes in that range for circular distance.
Remember that premature optimization and over optimization are pitfalls.
A function like this would tell you if a map square is visible (using the distance of the centers of the squares as a metric; if you want to define visibility in another manner, which you probably would, things get much more complicated):
function is_visible($mapX, $mapX, $playerX, $playerY, $r) {
return sqrt(pow($mapX - $playerX, 2) + pow($mapY - $playerY, 2)) <= $r;
}
You probably don't really need to store these values since you can easily calculate them on demand.
I think that Bresenham's circle drawing algorithm is what you're looking for.
I don't know exactly what you want, but here's some things that should help you along. As a warning these are untested, but the logic is sound.
//You mentioned circumference, this will find out the circumference but I don't
//think you actually need it.
$circumference_length = 2 * $visibility_range * 3.1415;
//Plug in the player and target coordinates and how far you can see, this will
//tell you if the player can see it. This can be optimized using your object
//and player Objects.
function canSee($player_x, $player_y, $vision_length, $target_x, $target_y){
$difference_x = $target_x - $player_x;
$difference_y = $target_y - $player_y;
$distance = sqrt((pow($difference_x,2) + pow($difference_y, 2));
if($vision < $distance){
return false;
} else {
return true;
}
}
Edit: In response to your clarification, you can use the above function to figure out if you should show the terrain objects or not.
foreach($player as $bot)
{
foreach($terrain_thing as $terrain)
{
//ASSUMING THAT [0] IS ALWAYS X AND [1] IS ALWAYS y, set a third variable
//to indicate visibility
$terrain["is_visible"] = canSee($bot[0], $bot[1], $visibility_range, $terrain[0], $terrain[1])
}
}