I am developing the nearest Food Dish application location. I managed to display it based on the location closest to the user, using this plugin great_circle_distance
I want to know how to set the max radius? ex. < 30 Km
Here my code:
_foodByCategories.sort((a, b) {
var distance1 = new GreatCircleDistance.fromDegrees(
latitude1:
model.userLocation == null ? 0.0 : model.userLocation["latitude"],
longitude1: model.userLocation == null
? 0.0
: model.userLocation["longitude"],
latitude2: a.lat,
longitude2: a.long);
double totaldistance1 = distance1.haversineDistance();
var distance2 = new GreatCircleDistance.fromDegrees(
latitude1:
model.userLocation == null ? 0.0 : model.userLocation["latitude"],
longitude1: model.userLocation == null
? 0.0
: model.userLocation["longitude"],
latitude2: b.lat,
longitude2: b.long);
double totaldistance2 = distance2.haversineDistance();
return (totaldistance1 - totaldistance2).toInt();
});
Any answer will appreciated.
If I understand your question correct
_foodByCategories.where((a) {
var distance = new GreatCircleDistance.fromDegrees(latitude1: double.parse(widget.lat), longitude1: double.parse(widget.lng), latitude2: double.parse(a.lat), longitude2: double.parse(a.lng));
var totaldistance = distance.haversineDistance().toStringAsFixed(2);
double distanceDouble1 = double.parse(totaldistance);
return distance <= 30000; // or something like that
}).sort ... // and your sorting code
I can't see any way to do this within the package you are using. You could suggest to the developer to add this functionality in?
Alternatively, you are getting the total distance in your code so you could just do a simple if statement to check if the total distance is greater than your set max radius.
Related
Im trying to write a looping block of code that codes that asks the following following within c++
while object A, or B or C or D == X, Y, Z (coords)
if A meets conditions, move object A East.
If B meets conditions, move object B East.
and so on,
I've pre-established each object coords as a string, so I just need to compare them against a control, Currently, I just have a loop that happens that doesn't recognise which object is triggering it.
The code to read coords and move the object is within a specific library, My tutor suggests using an array and a while loop to achieve this but I'm not sure how to do it even after spending several hours researching it.
I come from a background of unity and unreal engine so something simple like this goes over my head because I want to just use a conditional on the point, rather than react to each of the objects.
I'm very fresh to c# so please try to keep solutions simple thankyou!
EDIT: This is the code I am using at the moment
//Load strings at the start of the game
```string bblocal = (ballblue->GetLocalX, ballblue->GetLocalY, ballblue->GetLocalZ);
string bilocal = (ballindigo->GetLocalX,ballbindigo->GetLocalY, ballindigo->GetLocalZ);
string bvlocal = (ballviolet->GetLocalX,ballviolet->GetLocalY, ballviolet->GetLocalZ);
string bflocal = (ballfawn->GetLocalX,ballfawn->GetLocalY, ballfawn->GetLocalZ);
string bb = bblocal;
string bi = bilocal;
string bv = bvlocal;
string bf = bflocal;```
//For each tick, check the following
```while (bb || bi || bv || vf == (-50, 10, 50)
{
//Turn blue and move it to next point
if blue
{
ballblue->MoveX (0);
ballblue->MoveZ (100);
ballblue->RotateX (-50);
ballblue->RotateZ (50);
}
//Turn Indigo and move it to next point
//Turn Violet and move it to next point
//Turn Fawn and move it to next point
}
With a given class Point like:
public class Point
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
}
You have a brunch of define point:
var A = new Point{X=1, Y=0, Z=0};
var B = new Point{X=0, Y=1, Z=0};
var C = new Point{X=0, Y=0, Z=1};
// Etc
And a refenrece point to compare them with. List all the point you have to check in an array.
var referencePoint = new Point{X=0, Y=0, Z=1};
var pointsToCheck = new []{A, B, C};
An for each element of this array do your validation. You can have access to the point via the variable.
foreach(var p in pointsToCheck){
// Would be nice If Point add IEquatable<Point> with Equals gethashcode
if( p.X == referencePoint.X
&& p.Y == referencePoint.Y
&& p.Z == referencePoint.Z
)
{
//Do something!
p.X ++;
// Break; // if only the first must be moved.
}
}
Query them directly with LinQ
pointsToCheck.Where( p => p.X == referencePoint.X
&& p.Y == referencePoint.Y
&& p.Z == referencePoint.Z) ;
If you have an array of "corner" you point position in it, you will need the IEquatable.
var currentCornerIndex = Array.IndexOf(cornersList, CurrentPoint);
//Will return the next corner:
// if it's the last corner return the first
// if not a corner return the first.
var nextCornerIndex = (currentCornerIndex + 1) % cornersList.Length;
var nextCorner = cornersList[nextCornerIndex];
I've been tasked with implementing a Normal Distribution graph. I was wondering if Chart.js offers this functionality right out of the box or if I will need to extend it. The graph in question is here
Thank you
It is unfortunately not possible with Chart.js, except if you create it by yourself.
But, I found a library called ChartNew.js (Github) that provides a lot of functionalities that are not available on Chart.js :
Chart.js has been completely rewritten since ChartNew.js has been developed; Both code are now completely different. Most of the functionalities inserted in the new version of Chart.js are also available in ChartNew.js
And this library provides a Gaussian Function (also called Normal Distribution) :
To do it, take a look at the sample given in the Github.
I'm sure it will suit you if you change some data.
This Implementation has been done using React. The functions below can still be used in other programming languages built on top of Javascript.
The only two inputs required to plot a Normal Distribution curve will be Mean and Standard deviation
Defining states for mean and standard deviation & states for X and Y arrays
const [bellMean, setBellMean] = useState<number>(12.2036); //example
const [bellStdev, setBellStdev] = useState<number>(0.0008); //example
const [bellXValues, setBellXValues] = useState<(number)[]>([]);
const [bellYValues, setBellYValues] = useState<(number | null)[]>([]);
To Get X values for bell curve (if not using react can get rid of useEffect)
useEffect(() => {
// defining chart limits between which the graph will be plotted
let lcl = bellMean - bellStdev * 6;
let ucl = bellMean + bellStdev * 6;
let ticks = [lcl];
let steps = 100; // steps corresponds to the size of the output array
let stepSize = Math.round(((ucl - lcl) / steps) * 10000) / 10000;
let tickVal = lcl;
for (let i = 0; i <= steps; i++) {
ticks.push(Math.round(tickVal * 10000) / 10000); // rounding off to 4 decimal places
tickVal = tickVal + stepSize;
}
setBellXValues(ticks); //array for X values
}, [bellMean, bellStdev]);
To Get Y values for Bell curve (if not using react can get rid of useEffect)
useEffect(() => {
// Using PDF function from vega-statistics instead of importing the whole library
const densityNormal = (value: number, mean: number, stdev: number) => {
const SQRT2PI = Math.sqrt(2 * Math.PI);
stdev = (stdev == null) ? 1 : stdev;
const z = (value - (mean || 0)) / stdev;
return Math.exp(-0.5 * z * z) / (stdev * SQRT2PI);
};
let YValues = bellXValues.map((item: number) => {
if (bellMean === null || bellStdev === undefined) {
return null;
} else {
const pdfValue = densityNormal(item, bellMean, bellStdev);
return pdfValue === Infinity ? null : pdfValue;
}
});
setBellYValues(YValues); // array for Y values
}, [bellXValues]);
The arrays for X and Y can be fed to labels and data props of chartjs as it is.
How can I loop through a set of Qt QGraphicsItems and find the max X value within the graph items selected. I have a function which seems to work but i recall it not being ideal to set the min value to NULL. The only reason im doing that is because otherwise the value returns something insanely far away. I just don't feel like im doing this the most efficient way. Keep in mind im taking into consideration the width of the actual graph item itself.
float max = NULL;
foreach (QGraphicsItem* item, items) {
if (!item) continue;
if (item->type() == NexusBlockItem::Type) {
float x = item->pos().x() + item->sceneBoundingRect().width()*.5;
if (max == NULL) max = x;
if (x > max) {
max = x;
}
}
}
I have a function which seems to work but i recall it not being ideal
to set the min value to NULL.
It's not actually possible to set a floating point value to NULL, since only pointers can be NULL in C++ and float is not a pointer type. What you're likely actually doing there is initializing max to 0.0f (because NULL is typically defined as 0), but conceptually it doesn't make any sense, and of course it will mess up your result if any of your actual x positions are less than zero.
As far as how to do it better, you have two options that I can think of:
1) Add a separate maximum_value_is_set variable to track when your max variable is not initialized yet.
float max; // not initialized here, because...
bool maximum_value_is_set = false; // we'll keep track of it this way instead
[...]
float x = item->pos().x() + item->sceneBoundingRect().width()*.5;
if ((maximum_value_is_set == false)||(x > max)) {
max = x;
maximum_value_is_set = true;
}
2) or, alternatively, you could just set the default value of max to the smallest possible floating point value:
float max = -FLT_MAX;
[...]
... that way any conceivable x value will be greater than the original value of max.
Btw...
float x = item->pos().x() + item->sceneBoundingRect().width()*.5;
Why the *.5 there? It looks like this code computes the center the the rectangle, but according to your graphic, you are more interested in the right side (in which case I think you'd want to get rid of the *.5).
this code is for your items you shared.
float max = 0;
foreach (QGraphicsItem* item, items) {
if (!item) continue;
if (item->type() == NexusBlockItem::Type) {
float x = item->boundingRect().width();
if (x > max) {
max = x;
}
}
but if your second item or others shifted to more right other than
item->boundingRect().windth(); is not give correct value.
what is the max value of "X"? is it biggest item's width or scene Rectangle's width?
I am facing the following problem:
I am trying to implement a MNB classifier in a sliding window. I implemented a LinkedList of the size of the window and store all instances of the stream which have to be considered in it. When a new instance arrives which doesn't fit in the window anymore the first instance is removed. To remove the corresponding word counts I implemented the following method which basically is the same as trainOnInstanceImpl() by moa just backwards:
private void removeInstance(Instance instToRemove) {
int classIndex = instToRemove.classIndex();
int classValue = (int) instToRemove.value(classIndex);
double w = instToRemove.weight();
m_probOfClass[classValue] -= w;
m_classTotals[classValue] -= w * totalSize(instToRemove);
double total = m_classTotals[classValue];
for (int i = 0; i < instToRemove.numValues(); i++) {
int index = instToRemove.index(i);
if (index != classIndex && !instToRemove.isMissing(i)) {
double laplaceCorrection = 0.0;
if (m_wordTotalForClass[classValue].getValue(index) == w*instToRemove.valueSparse(i) + this.laplaceCorrectionOption.getValue()) {
laplaceCorrection = this.laplaceCorrectionOption.getValue(); //1.0
}
m_wordTotalForClass[classValue].addToValue(index,
(-1)*(w * instToRemove.valueSparse(i) + laplaceCorrection));
}
}
Now if I output the m_wordTotalForClass[classValue] I get different results for a classical MNB over a stream with 3000 instances from instance 2000-3000 as from the sliding window MNB (see above) with a window Size of 1000. And the only differences are that it outputs a 1 instead of a 0 at some points but not always. I guess this has something to do with the laplace correction. Maybe there is a problem with rounding in the if-statement:
if (m_wordTotalForClass[classValue].getValue(index) == w*instToRemove.valueSparse(i) + this.laplaceCorrectionOption.getValue())
so that we not always enter the part where the laplace value is set.
Does anybody have an idea?
I am kind of going crazy as I have been thinking about where the problem may be for the last three days. Any help would be greatly appreciated!
I'm just starting out on some ember.js, and am pretty inexperienced. This is probably a very simple question to answer but I can't seem to get it to work. I'm in the controller for my chart trying to set range using
range: function() {
var dataArr = this.get("rawData");
if (!dataArr) return;
var range = "";
var xmax = -1 * Infinity;
var xmin = Infinity;
var ymax = -1 * Infinity;
var ymin = Infinity;
code to figure out xmin, max etc......
lowerBound = new Date(xmin)
upperBound = new Date(xmax)
range = d3.time.format.utc("%a, %b%e %-I:%M %p")(lowerBound) + " - " + d3.time.format.utc("%a, %b%e %-I:%M %p")(upperBound);
return range
}.observes('display', 'rawData'),
I then try to use this with handlebars {{range}} and it does not work... I dont know if I'm providing enough info but if anyone can help out its much appreciated, thank you!
You need to identify your range property as a property, thusly:
range: function() {
var dataArr = this.get("rawData");
// blah blah, your implementation
return range;
}.property('rawData'),
By specifying rawData as a dependency, Ember will automatically recompute range if rawData changes.