Sunday, March 5, 2023

using .map() to iterate and modify objects in array

 const myUsers = [

    { name: 'shark', likes: 'ocean' },
    { name: 'turtle', likes: 'pond' },
    { name: 'otter', likes: 'fish biscuits' }
]

const usersByLikes = myUsers.map(item => {
    const container = {};

    container[item.name] = item.likes;
    container.age = item.name.length * 10;

    return container;
})

console.log(usersByLikes);

Source: https://www.digitalocean.com/community/tutorials/4-uses-of-javascripts-arraymap-you-should-know 

Saturday, September 21, 2019

The world's full of lonely people afraid to make the first move. - Tony Lip. Green Book (2018)

Monday, November 26, 2018

Do not remain nameless to yourself - it is too sad a way to be. Know your place in the world and evaluate yourself fairly, not in terms of the naive ideals of your own youth, nor in terms of what you erroneously imagine your teacher's ideals are. - Richard Feynman
Perfectly Reasonable Deviations from the Beaten Track: The Letters of Richard P. Feynman (2005)

Sunday, May 27, 2018

Headless Pi setup

Model used: Raspberry Pi 3 Model B.

1. Install Raspbian onto a micro SD card using etcher. (https://etcher.io/)
2. After installing, create a file in the boot directory, name it ssh.
3. Create another file in the boot directory, name it wpa_supplicant.conf. Within this file, insert the following text, using your ssid and password for your wifi network.

country=AU
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="your_real_wifi_ssid"
    scan_ssid=1
    psk="your_real_password"
    key_mgmt=WPA-PSK
}

4. Turn on the Raspberry Pi.
5. Find out the IP Address of the Raspberry Pi, either by using your router's web interface, or installing a program such as Wireless Network Watcher (http://www.nirsoft.net/utils/wireless_network_watcher.html). 
6. Download Putty from https://www.putty.org/
7. Run Putty, and type in the IP Address where it says Host Name. Click Open
8. The terminal of the Raspberry Pi will open. The default username is pi, and the default password is raspberry. You now have headless access to the Raspberry Pi.

Sources:
https://www.raspberrypi.org/forums/viewtopic.php?t=191252
https://www.howtogeek.com/204057/how-to-see-who%E2%80%99s-connected-to-your-wi-fi-network/

Tuesday, December 26, 2017

Unity: checking errors

If you cannot build to a mobile device for some reason, it may be due to a compiler error. To check, open the console window, click the bottom on the top right corner and 'open editor log'. This will contain detailed information as to why the project could not build.

Friday, December 22, 2017

Unity: Access child of a gameobject

GameObject ChildGameObject = ParentGameObject.transform.GetChild(i).gameObject;

where i is the index of the child transform.

Tuesday, October 31, 2017

Different ways of enabling mouse click as event trigger in Unity via scripting

For a 2D object/sprite:
Option 1:
1. In the script file, include using UnityEngine.EventSystems; at the top.
2. Implement the PointerClick interface 
3. Attach the script to the 2D sprite and make sure that a 2D collider (e..g box collider 2D) is attached to it.
4. Select the camera and attach Physics 2D raycaster to it.

using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

public class clickTester : MonoBehaviour, IPointerClickHandler  
 {  
      public void OnPointerClick (PointerEventData eventData)  
      {  
           //do something here, e.g. change colour of sprite.  
           GetComponent<SpriteRenderer>().color = new Color(1f,0.3f, 0.30196078f);  
           print ("Pointer click");  
      }  
 }  

Source: https://stackoverflow.com/questions/37452457/unity-3d-enabling-mouse-pointer-as-a-event-triggers-pointer-enter-exit/37456107#37456107


Option 2:
1. Ensure the 2D sprites have a 2D collider attached to them.
2. Attach a script file to the camera with the code below.
 using System.Collections;  
 using UnityEngine;  

 public class clickTester: MonoBehaviour {  
   void Update()  
   {  
     if (Input.GetMouseButtonDown(0)) //This is left mouse button click 
     {  
       //Converting Mouse Pos to 2D (vector2) World Pos  
       Vector2 rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);  
       RaycastHit2D hit = Physics2D.Raycast(rayPos, Vector2.zero, 0f);  
       if (hit)  
       {  
       //do something here, e.g. change colour of sprite.   
       GetComponent<SpriteRenderer>().color = new Color(1f,0.3f, 0.30196078f);   
       print ("Pointer click");   
       }  
     }  
   }  
 }  




For a UI button:
1. In the script file, include using UnityEngine.UI; at the top.
2. Make a public void method and give it a name
3. In the button, add an Event Trigger component -> Add New Event Type -> Pointer Click.
4. Drag the gameobject the script is attached to into the pointer click.
5. Choose the public void method created.
 using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 using UnityEngine.UI;  

 public class button : MonoBehaviour  
 {  
      public void changeColour(){  
           //Do something here, e.g. change colour of button.  
           GetComponent<Image>().color = Color.green;  
           print ("testing changeColour");  
      }  
 }  

Monday, October 16, 2017

Using Git

- Install Git from https://git-scm.com/download/win
- Navigate to the folder where you want to create the repository and type:
git init

- Set your name and address:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

- Set your text editor. E.g. If your editor is sublime text, type:
git config --global core.editor "'C:/Program Files/Sublime Text/sublime_text.exe' -multiInst -nosession"

Common commands:

git status
Shows what files have changed since the last commit.

git log
Shows commit log

git add .
Adds all modified files to staging area

git commit
Commit/records changes to the repository. This will open up an associated editor (e.g. notepad, sublime text, etc).

git checkout -b [name_of_new_branch]
Create a new branch

git branch -d [name_of_branch]
Delete a branch

git ls-tree -r master --name-only
Shows a list of all files currently being tracked under the branch master.

git ls-tree -r HEAD --name-only
Shows a list of all files currently being tracked under the current branch.

git add .
git stash
git checkout master
This is to switch back to the head if you have checked out an earlier commit.

Other:
To ignore directories, i.e. directories you don't want to commit, edit the .git/info/exclude file and add the directory you want to ignore, e.g. if the folder you want to ignore is called Temp, add
Temp/ to the exclude file.

Sunday, October 15, 2017

Different ways of disabling game object/component in Unity via scripting

Example: HingeJoint2D. When the spacebar is pressed, the object/component is disabled.

1. To disable the entire game object, you can use Destroy. Make sure to tag the appropriate game object.
      private GameObject joint;  
   
      void Start(){  
           joint = GameObject.FindWithTag("joint");  
      }  
   
      void Update() {  
           if (Input.GetKeyDown ("space")) {  
                Destroy(joint);
           }  
      }  

2. To deactivate the game object, use SetActive(false)
      private GameObject joint;  
   
      void Start(){  
           joint = GameObject.FindWithTag("joint");  
      }  
   
      void Update() {  
           if (Input.GetKeyDown ("space")) {  
                joint.SetActive(false);
           }  
      }  

3. Use GetComponent to disable only the HingeJoint2D component, and not the entire game object.
      private HingeJoint2D hinge;  
   
      void Start(){  
           hinge = gameObject.GetComponent(typeof(HingeJoint2D)) as HingeJoint2D;  
      }  
   
      void Update() {  
           if (Input.GetKeyDown ("space")) {  
                hinge.enabled=false;  
           }  
      }  

Wednesday, September 27, 2017

Error: Windows is unable to complete disk drive format

While attempting to format a micro sd card, the error 'Windows is unable to complete disk drive format' occurred.
Solution: Use SD Memory Card Formatter (https://www.sdcard.org/downloads/formatter_4/index.html)

Sunday, August 27, 2017

Discarding unstaged changes in Git

To discard all unstaged changes, use:
git checkout -- .

Saturday, March 4, 2017

Copying and pasting data from Excel to Mathematica

Type into Mathematica ImportString["", "TSV"]

Paste the excel data between the empty double brackets.

Source: http://mathematica.stackexchange.com/questions/8997/cut-and-paste-data-from-a-spreadsheet

Friday, December 9, 2016

Quick guide to root Android phones

0. Google how to unlock the bootloader for the phone.
1. Download and install the universal adb drivers from https://developer.android.com/studio/releases/platform-tools.html. Alternatively, download them from https://adb.clockworkmod.com
2. To check if adb was correctly installed, follow instructions here.
2. Enable USB debugging on your phone.
3. Download the latest version of SuperSU and put it in the phone
4. Install TWRP using this guide http://androiding.how/how-to-install-twrp-recovery-via-fastboot/ or this guide http://www.howtogeek.com/240047/how-to-flash-twrp-recovery-on-your-android-phone/
5. Install SuperSU using TWRP

Android root related words

ROM: modified version of android
Flash: To install something on your device. To replace the OS that is installed on your phone.
Recovery: the software on the phone that lets you make backups, flash ROMS, and other system-level tasks.
TRWP: A custom recovery
SuperSU: A management tool app. It prevents and provides access to root facilities.

Sources: http://lifehacker.com/5789397/the-always-up-to-date-guide-to-rooting-any-android-phone
http://android.stackexchange.com/questions/6183/what-do-root-flash-and-rom-mean

Tuesday, December 6, 2016

opening and editing .tex files

Method 1 (long install time):
1. Download and install TeXstudio http://www.texstudio.org/
2. Download and install all of TeX Live https://www.tug.org/texlive/acquire-netinstall.html, choosing the 'simple install (big) option.
3. Open the .tex file using TeXstudio

If the above method fails, attempt method 2 (short install time):
1. Download and install TeXstudio http://www.texstudio.org/
2. Download and install TeX Live from https://www.tug.org/texlive/acquire-netinstall.html. Choose the install-tl-windows.exe file
3. Choose the 'unpack only' option and install.
4. Run install-tl-advanced.bat
5. Under basic information, click on change.
6. Select basic scheme (plain and latex) and click on OK. Under further customization, it should now show 3 collections out of 48.
7. Click on install Tex Live. This installs a basic version of TeX live.
8. To install packages you need, use TeX Live Manager
9. Open the .tex file using Texstudio

Source:
http://tex.stackexchange.com/questions/250820/installation-of-texlive-on-windows-fails