How to find direction with flutter compass? - flutter

Is there any way to find a specific direction with a Flutter compass. Does that compass indicate a specific long and lat coordinates?

This is the standard code for Qibla compasses I use in my apps to get the offset:
double getOffsetFromNorth(double currentLatitude, double currentLongitude,
double targetLatitude, double targetLongitude) {
var la_rad = radians(currentLatitude);
var lo_rad = radians(currentLongitude);
var de_la = radians(targetLatitude);
var de_lo = radians(targetLongitude);
var toDegrees = degrees(atan(sin(de_lo - lo_rad) /
((cos(la_rad) * tan(de_la)) - (sin(la_rad) * cos(de_lo - lo_rad)))));
if (la_rad > de_la) {
if ((lo_rad > de_lo || lo_rad < radians(-180.0) + de_lo) &&
toDegrees > 0.0 &&
toDegrees <= 90.0) {
toDegrees += 180.0;
} else if (lo_rad <= de_lo &&
lo_rad >= radians(-180.0) + de_lo &&
toDegrees > -90.0 &&
toDegrees < 0.0) {
toDegrees += 180.0;
}
}
if (la_rad < de_la) {
if ((lo_rad > de_lo || lo_rad < radians(-180.0) + de_lo) &&
toDegrees > 0.0 &&
toDegrees < 90.0) {
toDegrees += 180.0;
}
if (lo_rad <= de_lo &&
lo_rad >= radians(-180.0) + de_lo &&
toDegrees > -90.0 &&
toDegrees <= 0.0) {
toDegrees += 180.0;
}
}
return toDegrees;
}
then you add the offset to the flutter_compass rotation to get the offset for each
sensor update and apply the result to the special header.

Related

How to format a number into thousands, millions and billions with dart/flutter?

How to get a number converted into something like this: 12K, 1.5M, 4.2B from a normal number like: 134900.
This is a minimalist function, of course you'll have to add validation code to verify if the number is valid before executing the function. Otherwise Enjoy ...
void main() {
double num = 1250;
var myNumber = k_m_b_generator(num);
print(myNumber);
}
String k_m_b_generator(num) {
if (num > 999 && num < 99999) {
return "${(num / 1000).toStringAsFixed(1)} K";
} else if (num > 99999 && num < 999999) {
return "${(num / 1000).toStringAsFixed(0)} K";
} else if (num > 999999 && num < 999999999) {
return "${(num / 1000000).toStringAsFixed(1)} M";
} else if (num > 999999999) {
return "${(num / 1000000000).toStringAsFixed(1)} B";
} else {
return num.toString();
}
}
You can use flutter's NumberFormat class with the compact function.
formatNumber(dynamic myNumber) {
// Convert number into a string if it was not a string previously
String stringNumber = myNumber.toString();
// Convert number into double to be formatted.
// Default to zero if unable to do so
double doubleNumber = double.tryParse(stringNumber) ?? 0;
// Set number format to use
NumberFormat numberFormat = new NumberFormat.compact();
return numberFormat.format(doubleNumber);
}
The answer is not entirely correct. If you test it, you will see what i meant. Base on the answer above, I created this solution:
String numberFormat(int n) {
String num = n.toString();
int len = num.length;
if (n >= 1000 && n < 1000000) {
return num.substring(0, len - 3) + '.' + num.substring(len - 3, 1 + (len - 3)) + 'k';
} else if (n >= 1000000 && n < 1000000000) {
return num.substring(0, len - 6) + '.' + num.substring(len - 6, 1 + (len - 6)) + 'm';
} else if (n > 1000000000) {
return num.substring(0, len - 9) + '.' + num.substring(len - 9, 1 + (len - 9)) + 'b';
} else {
return num.toString();
}
}

Is there any plugin that support fade in and out in Flutter

I am working with the flutter_sound in Flutter. I simply want to fade in and fade out sound file in flutter
I have created a method that allows you to fade in or fade out.
If you want to fade you must enter this:
3 seconds to increase the volume from 0.0 to 1.0.
Fade in:
fade (1.0, 0.0, 3 * 1000);
Fade out:
fade (0.0, 1.0, 3 * 1000);
If it works for you, don't forget to rate my answer, regards.
void fade( double to, double from, int len ) {
double vol = from;
double diff = to - from;
double steps = (diff / 0.01).abs() ;
int stepLen = Math.max(4, (steps > 0) ? len ~/ steps : len);
int lastTick = DateTime.now().millisecondsSinceEpoch ;
// // Update the volume value on each interval ticks
Timer.periodic(new Duration(milliseconds: stepLen), ( Timer t ) {
var now = DateTime.now().millisecondsSinceEpoch;
var tick = (now - lastTick) / len;
lastTick = now;
vol += diff * tick;
vol = Math.max(0, vol);
vol = Math.min(1, vol);
vol = (vol * 100).round() / 100;
player.setVolume(vol); // change this
if ( (to < from && vol <= to) || (to > from && vol >= to) ) {
if (t != null) {
t.cancel() ;
t = null;
}
player.setVolume(vol); // change this
}
});
}

Programming on the Nintendo DS - issue with collisions

Sorry if this question has already been asked, but I have not managed to find any advice on the internet for my issue. I am currently trying to program a little game on the Nintendo DS, in which the player has to move a sprite (currently a square) until it reaches the exit. For this, I use a sprite I have included using a grit file, and also a background enabled in tiled mode. However, I am having a problem when it comes to checking if the sprite is going to collide with a wall. Here is the code I have both for the background configuration (where I declare the tiles and the map) and also for the sprite movements (I didn't add the condition for all cases yet, as it didn't work well) :
void configureMaze_Sub() {
int row, col;
for (row = 0; row < 24; row ++) {
for (col = 0; col < 32; col ++) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 1;
if (col == 15 && (row != 12 && row !=4 && row != 19)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((row == 1 || row == 22) && (col > 2 && col < 29)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((col == 3 || col == 28) && (row > 1 && row < 22 && row != 12)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((row == 3 || row == 20) && (col > 4 && col < 27)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((col == 5 || col == 26) && (row != 9 && row != 15 && row > 3 && row < 20)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if (row == 8 && (col > 5 && col < 15)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if (row == 16 && (col > 15 && col < 26)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((row == 12) && (col > 5 && col < 26)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
}
}
}
void gameplayMaze() {
int x = 103, y = 41, keys;
int maze_success = 0;
while (maze_success == 0) {
scanKeys();
keys = keysHeld();
int xmod = x / 8;
int ymod = x / 8;
if ((keys & KEY_RIGHT) && BG_MAP_RAM_SUB(3)[xmod + 32 * ymod] == 1) {
x++;
printf("%d \n", x);
}
if ((keys & KEY_LEFT) && BG_MAP_RAM_SUB(3)[xmod + 32 * ymod] == 1) {
x--;
}
if (keys & KEY_UP) {
y--;
}
if (keys & KEY_DOWN) {
y++;
}
oamSet(&oamSub,
0,
x, y,
0,
0,
SpriteSize_8x8,
SpriteColorFormat_256Color,
gfxSub,
-1,
false,
false,
false, false,
false
);
swiWaitForVBlank();
oamUpdate(&oamSub);
}
The main problem I have is to try to change from the coordinates of the tiles (which are 8x8) to the ones of the map, as for the coordinates of the sprite (256x192). If any of you have any hint to help me, I would be very grateful! I am still new to programming on the NDS, so I am still struggling to get the hang of it.

Apparent Multiple Failures Using Google Chrome Versions 30 to 33

I've been developing the Image Slider Theme(dated 02/20/2015 - Jssor Slider 18.0 Development Kit)(Jssor.Slider.FullPack\demos-no-jquery\image-slider-2.source.html with jssor.slider.min.js). I have not modified the javascript code. I'm finding failures of various script implementations in Chrome versions 30 through 33 with this Image Slider and several other in the Development Kit. Previous January Development Kit also shows errors. No problems with any other browsers or versions of those browsers that are not already indicated and/or addressed.
I have found that tweaking the function SetPosition "elseif" from:
" else if ($Jssor$.$IsBrowserChrome() && $Jssor$.$BrowserVersion() >= 30 && $Jssor$.$BrowserVersion() < 34)"
to:
" else if ($Jssor$.$IsBrowserChrome() && $Jssor$.$BrowserVersion() >= 32 && $Jssor$.$BrowserVersion() < 34)"
function SetPosition(elmt, position) {
var orientation = _DragOrientation > 0 ? _DragOrientation : _PlayOrientation;
var x = _StepLengthX * position * (orientation & 1);
var y = _StepLengthY * position * ((orientation >> 1) & 1);
if ($Jssor$.$IsBrowserChrome() && $Jssor$.$BrowserVersion() < 38) {
x = x.toFixed(3);
y = y.toFixed(3);
}
else {
x = Math.round(x);
y = Math.round(y);
}
if ($Jssor$.$IsBrowserIE() && $Jssor$.$BrowserVersion() >= 10 && $Jssor$.$BrowserVersion() < 11) {
elmt.style.msTransform = "translate(" + x + "px, " + y + "px)";
}
else if ($Jssor$.$IsBrowserChrome() && $Jssor$.$BrowserVersion() >= 30 && $Jssor$.$BrowserVersion() < 34) {
elmt.style.WebkitTransition = "transform 0s";
elmt.style.WebkitTransform = "translate3d(" + x + "px, " + y + "px, 0px) perspective(2000px)";
}
else {
$Jssor$.$CssLeft(elmt, x);
$Jssor$.$CssTop(elmt, y);
}
}
I'm using this as a quick-fix until I can delve into it further. Has anyone noticed anything along this line?
The following code is to improve performance of sliding left<->right for chrome (version 30 to 33).
else if ($Jssor$.$IsBrowserChrome() && $Jssor$.$BrowserVersion() >= 30 && $Jssor$.$BrowserVersion() < 34) {
elmt.style.WebkitTransition = "transform 0s";
elmt.style.WebkitTransform = "translate3d(" + x + "px, " + y + "px, 0px) perspective(2000px)";
}
After about a year, chrome went to version 40. Now it's time to remove it.
Thanks.

Can I tell CoffeeScript to retain newlines in it's vanilla javascript output

CoffeeScript Input:
noob = [
'learning'
'new'
'language'
]
or
vector.x >=0 &&
vector.x < this.width &&
vector.y >=0 &&
vector.y < this.height
Rendered Output:
noob = ['learning','new','language'];
or
vector.x >= 0 && vector.x < this.width && vector.y >= 0 && vector.y < this.height;
Desired, as more readable
noob = [
'learning',
'new',
'language'
];
or
(identical to input)
Is it possible to have CoffeeScript retain newlines in the output js?