Find if point(lattitude,longtitude) is inside area - geolocation

I have points(lat,long) coordindates and i have area leftTop(lat,long) rightBottom(lat,long) , i need to check if my points is inside area.
It's not correct just to check if point coordinate is more than leftLat, and less rightLat.
I have corners
leftLat=81.49021937827182
leftLng=38.979793936014175
rightLat=-0.5414380758487521
rightLng=173.9797962829470

function rayCrossesSegment($point, $a, $b)
{
$px = $point['lng'];
$py = $point['lat'];
$ax = $a['lng'];
$ay = $a['lat'];
$bx = $b['lng'];
$by = $b['lat'];
if ($ay > $by) {
$ax = $b['lng'];
$ay = $b['lat'];
$bx = $a['lng'];
$by = $a['lat'];
}
// alter longitude to cater for 180 degree crossings
if ($px < 0) {
$px += 360;
};
if ($ax < 0) {
$ax += 360;
};
if ($bx < 0) {
$bx += 360;
};
if ($py == $ay || $py == $by) {
$py += 0.00000001;
}
if (($py > $by || $py < $ay) || ($px > max($ax, $bx))) {
return false;
}
if ($px < min($ax, $bx)) {
return true;
}
$red = ($ax != $bx) ? (($by - $ay) / ($bx - $ax)) : INF;
$blue = ($ax != $px) ? (($py - $ay) / ($px - $ax)) : INF;
return ($blue >= $red);
}

Related

Highcharts Showing Uncaught TypeError: Cannot read properties of undefined (reading 'chart')

I've had this HighCharts spider chart working fine for a while now, but we upgraded to the latest HighCharts code and I noticed that the mouseovers are no longer working. My PHP code looks like this:
// Create a new Highchart
$chart = new Highchart();
$chart->includeExtraScripts();
$chart->chart->renderTo = "control_maturity_spider_chart";
$chart->chart->polar = true;
$chart->chart->type = "line";
$chart->chart->width = 1000;
$chart->chart->height = 1000;
$chart->title->text = "Current vs Desired Maturity by Control Family";
$chart->title->x = -80;
$chart->pane->size = "80%";
$chart->xAxis->categories = $categories;
$chart->xAxis->tickmarkPlacement = "on";
$chart->xAxis->lineWidth = 0;
$chart->yAxis->gridLineInterpolation = "polygon";
$chart->yAxis->lineWidth = 0;
$chart->yAxis->min = 0;
$chart->yAxis->max = 5;
$chart->yAxis->tickInterval = 1;
$chart->tooltip->shared = true;
$chart->tooltip->pointFormat = '<span style="color:{series.color}">{series.name}: <b>{point.y}</b><br/>';
$chart->legend->align = "center";
$chart->legend->verticalAlign = "top";
$chart->legend->layout = "vertical";
// Draw the Current Maturity series
$chart->series[0]->name = $escaper->escapeHtml($lang['CurrentControlMaturity']);
$chart->series[0]->data = empty($categories_current_maturity_average) ? [] : $categories_current_maturity_average;
$chart->series[0]->pointPlacement = "on";
// Draw the Desired Maturity series
$chart->series[1]->name = $escaper->escapeHtml($lang['DesiredControlMaturity']);
$chart->series[1]->data = empty($categories_desired_maturity_average) ? [] : $categories_desired_maturity_average;
$chart->series[1]->pointPlacement = "on";
$chart->credits->enabled = false;
echo "<figure class=\"highcharts-figure\">\n";
echo " <div id=\"control_maturity_spider_chart\"></div>\n";
echo "</figure>\n";
echo "<script type=\"text/javascript\">";
echo $chart->render("control_maturity_spider_chart");
echo "</script>\n";
The actual chart renders just fine, but if you mouse over it, you just get this message in the javascript console over and over again:
HighCharts Error Message
If we comment out these two lines of code, the mouseover works:
$chart->tooltip->shared = true;
$chart->tooltip->pointFormat = '<span style="color:{series.color}">{series.name}: <b>{point.y}</b><br/>';
Any thoughts on what we are doing wrong here, or what changed, would be greatly appreciated. Thank you.
this is the bug which you can track here: https://github.com/highcharts/highcharts/issues/17472
As a temporary workaround, add the following wrap function to your code:
(function(H) {
const isObject = H.isObject;
H.Pointer.prototype.findNearestKDPoint = function(series, shared, e) {
var chart = this.chart;
var hoverPoint = chart.hoverPoint;
var tooltip = chart.tooltip;
if (hoverPoint &&
tooltip &&
tooltip.isStickyOnContact()) {
return hoverPoint;
}
var closest;
/** #private */
function sort(p1, p2) {
var isCloserX = p1.distX - p2.distX,
isCloser = p1.dist - p2.dist,
isAbove = ((p2.series.group && p2.series.group.zIndex) -
(p1.series.group && p1.series.group.zIndex));
var result;
// We have two points which are not in the same place on xAxis
// and shared tooltip:
if (isCloserX !== 0 && shared) { // #5721
result = isCloserX;
// Points are not exactly in the same place on x/yAxis:
} else if (isCloser !== 0) {
result = isCloser;
// The same xAxis and yAxis position, sort by z-index:
} else if (isAbove !== 0) {
result = isAbove;
// The same zIndex, sort by array index:
} else {
result =
p1.series.index > p2.series.index ?
-1 :
1;
}
return result;
}
series.forEach(function(s) {
var noSharedTooltip = s.noSharedTooltip && shared,
compareX = (!noSharedTooltip &&
s.options.findNearestPointBy.indexOf('y') < 0),
point = s.searchPoint.call(s.polar, e, compareX);
if ( // Check that we actually found a point on the series.
isObject(point, true) && point.series &&
// Use the new point if it is closer.
(!isObject(closest, true) ||
(sort(closest, point) > 0))) {
closest = point;
}
});
return closest;
};
}(Highcharts))
Demo:
https://jsfiddle.net/BlackLabel/8b2mhqf0/

Autocomplete on phone - AS3

I've made an autocomplete that work very well in the swf file.
very simple, when the user write the first letter, a suggestion is made with words.
I've published my project for IOS.
When I'm trying it on the Iphone, nothing is suggested when I'm typing the first letter.
I have to write the first letter and then clicked on "enter" in order to display the suggestions...
I don't want the users to "validate" in order to have the suggestion but simply by typing a letter.
Do you know what could be the problem ? Anyone can help me ?
Weirdly, I've tried on an Android device, and it's working perfectly well ! (like my swf).
Here is my code :
urlLoader.load(new URLRequest("test.txt"));
urlLoader.addEventListener(Event.COMPLETE, loadComplete);
inputField.addEventListener(KeyboardEvent.KEY_UP, suggest);
function loadComplete(e:Event):void
{
suggestions = e.target.data.split(",");
}
function suggest(e:KeyboardEvent):void
{
suggested = [];
for (var i:int = 0; i < textfields.length; i++)
{
removeChild(textfields[i]);
}
textfields = [];
for (var j:int = 0; j < suggestions.length; j++)
{
if (suggestions[j].indexOf(inputField.text.toLowerCase()) == 0)
{
var term:TextField = new TextField();
term.width = 300;
term.height = 20;
term.x = 70;
term.y = (20 * suggested.length) + 314;
term.border = true;
term.borderColor = 0x353535;
term.background = true;
term.backgroundColor = 0xFF9900;
term.textColor = 0x4C311D;
term.defaultTextFormat = format;
term.addEventListener(MouseEvent.MOUSE_UP, useWord);
term.addEventListener(MouseEvent.MOUSE_OVER, hover);
term.addEventListener(MouseEvent.MOUSE_OUT, out);
term.addEventListener(MouseEvent.CLICK, tellMe);
addChild(term);
textfields.push(term);
suggested.push(suggestions[j]);
term.text = suggestions[j];
}
}
if (inputField.length == 0)
{
suggested = [];
for (var k:int = 0; k < textfields.length; k++)
{
removeChild(textfields[k]);
}
textfields = [];
}
if(e.keyCode == Keyboard.DOWN && currentSelection < textfields.length-1)
{
currentSelection++;
textfields[currentSelection].textColor = 0x4C311D;
}
if(e.keyCode == Keyboard.UP && currentSelection > 0)
{
currentSelection--;
textfields[currentSelection].textColor = 0x4C311D;
}
if(e.keyCode == Keyboard.ENTER)
{
inputField.text = textfields[currentSelection].text;
suggested = [];
for (var l:int = 0; l < textfields.length; l++)
{
removeChild(textfields[l]);
}
textfields = [];
currentSelection = 0;
}
}
function useWord(e:MouseEvent):void
{
inputField.text = e.target.text;
suggested = [];
for (var i:int = 0; i < textfields.length; i++)
{
removeChild(textfields[i]);
}
textfields = [];
}
Thank you
EDIT
Here's my new code with Stagetext
var myTextField:StageText = new StageText();
var stageTextInitOptions:StageTextInitOptions;
var urlLoader:URLLoader = new URLLoader();
var suggestions:Array = new Array();
var suggested:Array = new Array();
var textfields:Array = new Array();
var format:TextFormat = new TextFormat();
var currentSelection:int = -1;
var searchChannel:SoundChannel = new SoundChannel();
myTextField.returnKeyLabel = ReturnKeyLabel.SEARCH;
myTextField.addEventListener(KeyboardEvent.KEY_UP, suggest);
stageTextInitOptions = new StageTextInitOptions(false);
myTextField = new StageText(stageTextInitOptions);
myTextField.softKeyboardType = SoftKeyboardType.DEFAULT;
myTextField.returnKeyLabel = ReturnKeyLabel.DONE;
myTextField.autoCorrect = true;
myTextField.fontSize = 20;
myTextField.color = 0x000000;
myTextField.fontWeight = "bold";
myTextField.stage = this.stage;
myTextField.viewPort = new Rectangle(25, 108, stage.stageWidth-40, 28);
urlLoader.load(new URLRequest("Sports2.txt"));
urlLoader.addEventListener(Event.COMPLETE, loadComplete);
myTextField.addEventListener(KeyboardEvent.KEY_UP, suggest);
KeyboardEvent.KEY_UP, KeyboardEvent.KEY_DOWN, TextEvent.TEXT_INPUT doesn't really works at iOS. KeyboardEvent.KEY_DOWN is dispatching only on some keys. I saw only key "Enter".

My while-loop in Swift won't terminate, what am i missing?

I have a pretty detailed while-loop and have been really resisting having to ask for help once again but I am at a loss! I really apologize for this while-loop code, there's a lot going on but I don't want to leave anything out just in case you viewers require to see it all.
func retRhoCurrent() -> Double {
while abs(pTarget - pCalc) > 1 {
//
// SET rhoCurrent
//
if (pTarget > pCalc) {
rhoLow = rhoCurrent
if(rhoCurrent >= rhoHigh) {
rhoCurrent += RHO_C
rhoHigh = rhoCurrent
} else {
rhoCurrent += ((rhoHigh - rhoCurrent)/2)
}
} else {
rhoHigh = rhoCurrent
rhoCurrent += ((rhoLow - rhoCurrent)/2)
}
//
// SET rhoR
//
rhoR = rhoCurrent / RHO_C
//
// SET DIFFERENTIAL
//
diffAggregate = 0
for var kIndex = 0; kIndex < NK_VALUES.count; ++kIndex {
diffSegment = NK_VALUES[kIndex]
diffSegment *= pow(rhoR, IK_VALUES[kIndex])
diffSegment *= pow(tempR, JK_VALUES[kIndex])
iFactor = 0
if LK_VALUES[kIndex] > 0 {
diffSegment *= exp(-1 * pow(rhoR, LK_VALUES[kIndex]))
iFactor = LK_VALUES[kIndex] * pow(rhoR, LK_VALUES[kIndex])
}
if PK_VALUES[kIndex] > 0 {
diffSegment *= exp(-1 * PK_VALUES[kIndex] * pow(rhoR, 2) - BK_VALUES[kIndex] * pow(tempR - UK_VALUES[kIndex], 2))
iFactor = 2 * rhoR * PK_VALUES[kIndex] * (rhoR - 1)
}
diffAggregate += (diffSegment * (IK_VALUES[kIndex] - iFactor))
}
//
// SET pCalc
//
zDiff + 1 + diffAggregate
pCalc = zDiff * R_CONST * 1000 * tempK * rhoCurrent
}
return rhoCurrent
}
I guess another question i have also is the main value I want out of this while loop is rhoCurrent (as I will use this final # to calculate something else). Doing the "return rhoCurrent" will work, correct?
Thanks so much in advance!!
I think you intended this line to assign a value, but it doesn't:
zDiff + 1 + diffAggregate
Because it doesn't assign a value, diffAggregate is not used in the body of your while loop. rhoR is also not used because of this. Your function can be simplified as such:
func retRhoCurrent() -> Double {
while abs(pTarget - pCalc) > 1 {
//
// SET rhoCurrent
//
if (pTarget > pCalc) {
rhoLow = rhoCurrent
if(rhoCurrent >= rhoHigh) {
rhoCurrent += RHO_C
rhoHigh = rhoCurrent
} else {
rhoCurrent += ((rhoHigh - rhoCurrent)/2)
}
} else {
rhoHigh = rhoCurrent
rhoCurrent += ((rhoLow - rhoCurrent)/2)
}
//
// SET pCalc
//
pCalc = zDiff * R_CONST * 1000 * tempK * rhoCurrent
}
return rhoCurrent
}
I'm really suspicious of the variables that you use in the loop but do not initialize within the function. What are the initial values for rhoLow, rhoHigh, rhoCurrent, tempK, zDiff, pTarget, and pCalc?
This method is very messy and depends on a lot of magic. It is modifying values that it does not own, meaning you could experience unexpected things in other areas of your app. It is definitely not thread-safe (though this may not be a concern for you).

Best Practice to Avoid Too Many If Statements in ios

You all might have came across the scenarios like the following ones:
-(int) fightMath(int one, int two) {
if(one == 0 && two == 0) { result = 0; }
else if(one == 0 && two == 1) { result = 0; }
else if(one == 0 && two == 2) { result = 1; }
else if(one == 0 && two == 3) { result = 2; }
else if(one == 1 && two == 0) { result = 0; }
else if(one == 1 && two == 1) { result = 0; }
else if(one == 1 && two == 2) { result = 2; }
else if(one == 1 && two == 3) { result = 1; }
else if(one == 2 && two == 0) { result = 2; }
else if(one == 2 && two == 1) { result = 1; }
else if(one == 2 && two == 2) { result = 3; }
else if(one == 2 && two == 3) { result = 3; }
else if(one == 3 && two == 0) { result = 1; }
else if(one == 3 && two == 1) { result = 2; }
else if(one == 3 && two == 2) { result = 3; }
else if(one == 3 && two == 3) { result = 3; }
return result;
}
In short, how to effectively simplify the above scenario in Objective-C's ambience?
Any Suggestions/Ideas/Solutions ?
Cheers :)
Edit: For reference , scenario taken from here.I hope this question might save even one sec of needy developer.
Objective C is built over C. So any good C solution will be also appropriate for Objective C. Like
int result[][4] = {
{ 0, 0, 1, 2 },
{ 0, 0, 2, 1 },
{ 2, 1, 3, 3 },
{ 1, 2, 3, 3 }
};
return result[one][two]
As I know there is no Objective C - specific good practices for such problems.

Help with ratio and frequency

I have the following:
package {
import flash.display.MovieClip;
public class Ratio extends MovieClip {
private var counter:Number;
private var frequency:Number;
private var ratio:String;
private var max:Number;
public function Ratio() {
ratio ="2/8";
var arr = ratio.split("/");
max = arr[1];
frequency = arr[0];
counter = 0;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event):void {
trace(counter + ": " + (counter < frequency));
counter++;
if (counter == max) {
counter = 0;
}
}
public function destroy():void {
removeEventListener(Event.ENTER_FRAME, loop);
}
}
}
This outputs something like:
0: true
1: true
2: false
3: false
0: true
1: true
2: false
3: false
But what I would actually like is:
0: true
1: false
2: true
3: false
0: true
1: false
2: true
3: false
i.e. more even dispersion (alternation)...is there a way to do this?
I think I may have missed the point here, but this should give the desired output:
private function loop(e:Event):void {
trace(counter + ": " + !Boolean(counter % 2));
counter++;
if (counter == max) counter = 0;
}
Update:
Although it's getting a little heavy-handed, the following creates an alternating dispersion where it is exactly possible. Everything else runs in a straight sequence. I'm no mathematician though, and I feel that it could be done better. For example this will treat 3/9 as 1,1,1,0,0,0,0,0,0, whereas a 'nicer' distribution might be 0,1,0,0,1,0,0,1,0
var ratio:String = "3/5";
var counter:int = 0;
var arr = ratio.split("/");
var frequency:int = arr[0];
var max:int = arr[1];
addEventListener(Event.ENTER_FRAME,loop);
function loop(e:Event):void {
if((max+1) / frequency == 2 || max / frequency == 2) {
trace(counter + ": " + !Boolean(counter % 2));
} else if((max-1) / frequency == 2) {
trace(counter + ": " + Boolean(counter % 2));
} else {
trace(counter < frequency);
}
counter++;
if (counter == max) removeEventListener(Event.ENTER_FRAME,loop);
}

Resources