Saturday, February 15, 2014
A neat little result.
We already know that (assuming integer division) that
\[\left( \frac{x}{{10}^i} \right) \mod 10 \]
gives us the \((n-i)\)-th digit of a number (\(n =\left\lfloor{\log_{10}(x)} \right\rfloor \), and \( i = 0 \) gives you the last digit) when counting in a decimal system. But going over the homework batch where the students were supposed to implement radix sort, I noticed that
\[ \frac{x \mod {10}^{i+1}}{{10}^i} \]
gives you the \(i\)-th digit (where 0 gives you the first)! Pretty cool!
I'm not going to prove this, as I have far to little experience with integer division and the modulus operation, but it seems to work, judging by that my students queues all turn out sorted.
Thursday, November 21, 2013
Orbital Mechanics
You design a space ship, and then you fly around in it, trying to land on the moon, or what have you. It's a pretty open sandbox game.
During it's development, I had to dive into orbital mechanics, to figure out the orbit of the spaceship, and then to render that orbit.
I though it was pretty neat, but it was hard to figure out from wikipedia, so I thought I'd write a post about it.
Lets dive into the function:
Ship.prototype.updateOrbit = function() {
//Calculate orbit from orbital state vectors
//Get the primary body the ship is orbiting
var terr = entityManager.getTerrain(this.cx,this.cy);
//One focus is in the center of the primary body
var focus = terr.center;
var M = terr.mass;
//Mu in the equations
var mu = consts.G*(M+this.mass)
//The posistion and velocity vectors.
//Note that posistion is relative to the focus,
//But the velocity is just the current velocity of the ship
var r = util.vecMinus(this.center,focus)
var v = [this.velX,this.velY];
//Calculate the orbit specific energy, and from that we find the
//semi-major axis. Note that this makes sense, as the semi major axis
//basically tells us how high we'll go.
var speed = this.getSpeed();
var eng = (speed*speed)/2 - (mu/util.lengthOfVector(r));
var a = -mu/(2*eng);
//Calculate the eccentricity vector, and from that the eccentricity
var tripleprod = util.tripleProduct(v,r,v);
var vtimeshovermu = util.mulVecByScalar(1/mu,tripleprod);
var unitr = util.normalizeVector(r)
var eccVec = util.vecMinus(vtimeshovermu,unitr);
var ecc = util.lengthOfVector(eccVec);
//ae is the distance from the center to a;
var ae = a*ecc;
//semi minor axis
var b = a*Math.sqrt(1-ecc*ecc);
//Find the center from the distance
var cen = [f[0]-ae,f[1]];
//Here we find the rotation of the eccentricity,
//which tells us where the orbit is from the focus
var angl = util.angleOfVector(eccVec);
//rotate the center around the focus according to the angle
cen = util.rotatePointAroundPoint(cen,angl,focus[0],focus[1]);
//Set the properties of the orbit
this.orbit = [cen[0], cen[1],a,b,angl,focus[0],focus[1]];
}
Now, using this we can plot our ellipse, knowing the center, and the angle of it. And it works, just zoom far enough out in the game, and take off! Happy flying!
Monday, October 21, 2013
Function for quick adding tasks or events to google calendar.
"use strict";
/*
01234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
/*
* @author Matthias Pall Gissurarson
* @usage quickAddTaskOrEventFromString(quickadd);
* @param quickadd A String describing the event to quickadd
*/
function quickAddTaskOrEventFromString(quickadd) {
//Add a task if the string includes " due ", otherwise add an event.
if(quickadd.indexOf(" due ") !== -1){
//Use the already implemented quickAdd to create an event.
var event = CalendarApp.createEventFromDescription(quickadd);
//This event gets automatically added to the users calendar, remove it.
event.deleteEvent();
/*
Create a new task, and have it have the same properties as the event.
Tasks don't have dueTime as of now, so we add that to the note of the task.
Also add the events location and description, these are often empty.
*/
var task = Tasks.newTask();
task.setDue(event.getStartTime().toISOString());
task.setTitle(event.getTitle());
task.setNotes([event.getStartTime().toString(),
event.getLocation(),
event.getDescription()].join("\n"));
//Add to the users default tasklist.
Tasks.Tasks.insert(task,'@default');
} else {
//Add the event as normal
CalendarApp.createEventFromDescription(quickadd);
}
}
function main() {
var s = "Homework due tomorrow";
var e = quickAddTaskOrEventFromString(s);
}
This function takes in a string, and if it contains " due ", it creates a task instead of an event on the default task list. Might this get implemented in the actual calendar?I have posted a topic for this on the Google product forums. Let's see what happens.
Monday, June 10, 2013
Markdown blog posts via emacs/vim and pandoc
Using Markdown on google blogs from the Commandline
As per the comment on my last post from Matthías Ragnarsson, I'm using pandoc to generate html from a md file, albeit I'm not using vim, but rather emacs (as setting up markdown-mode is simpler, and with evil, it's pretty much the same thing, only more easily extensible. It also fits better with the Unix philosophy of using external tools to do tasks, rather than hardcoding them into the program itself.
Saturday, June 08, 2013
Google calendar from the desktop, and posting through vim
Posting trough vim
Here I am, writing a blog post in vim. If this actually gets posted, I'll be pretty impressed, as this makes posting to the google blogs extremely simple to implelement via a server. The api program itself is just a python program, so porting the functionality over to haskell shouldn't be hard at all. I might even make it and put it on hackage. It'd be neat to have a package there!
Anyway, I hope this test works, as it'll make code examples way simpler.
Google calendar on the desktop
I recently made some commits so that it can display arbitrary colors in conky, and used it to create a neat display of my google calendar on the desktop, and you can see a picture of it included in the pull.
My .gcalclirc is as follows:
--calendar
icetritlo@gmail.com
--military
--monday
#Tomorrow-Night Palette
--conky_color0
#282A2E
--conky_color8
#373B41
--conky_color1
#A54242
--conky_color9
#CC6666
--conky_color2
#8C9440
--conky_color10
#B5BD68
--conky_color3
#F0C674
--conky_color11
#DE935F
--conky_color4
#5F819D
--conky_color12
#81A2BE
--conky_color5
#85678F
--conky_color13
#B294BB
--conky_color6
#5E8D87
--conky_color14
#8ABEB7
--conky_color7
#707880
--conky_color15
#F0C673
#Viljum auka vidd ef a ad vera a isl
#--locale
#is_IS.UTF-8
#-w
#12
I have that last part if I should want to have the dates and day names in Icelandic, but as neither are capitalized (correct in the middle of a sentence, but not standalone), and the english version suits me fine, I don't think I'll bother fixing it.
[Edit: Rewrote this post in markdown, and used pandoc to translate it into html, as per the comment below. N.B. you need my fork of gcalcli for this to work, as the --conky_color options were added by me.]
Wednesday, June 05, 2013
Using GoogleCL to post from the terminal
Saturday, April 20, 2013
Using xmobar to display rcirc status from emacs
Run Com "~/.scritpstouse/rcircActivity" [] "rcirc" 100
the script is as follows:
#!/usr/bin/env bash
if emacsclient -e rcirc-activity-string > rcirc-string;then
if ircstr=$(cat rcirc-string | tr '"' '\n' | grep '\[');then
echo "<fc=#00ff00>"$ircstr"</fc>"
else
echo "<fc=#0000ff>[]</fc>"
fi
else
echo "<fc=#ff0000>[]</fc>"
fi
This contacts my running emacs server and asks for the status string, and then displays it. I'd have it in the .xmobarrc, but it doesn't handle escaped " so well. If my server isn't running or rcirc isn't, there is no field that contains [, so the output will be empty and my xmobar will just have a few spaces. The echos give me different colors on the xmobar based on what happened, green and the status if rcirc on, blue if emacs on but rcirc not, and red if emacs turned off. Try it!
[Edit: I've decided to try and use erc instead. The command is almost the same (the ircActivity), but the if clause is replaced with:
i
f ercBuffers=$(emacsclient -e "(if (functionp erc-track-shorten-function) (funcall erc-track-shorten-function (mapcar 'buffer-name (mapcar 'car erc-modified-channels-alist))) (nil))");then
if ircstr=$(echo ${ercBuffers:1:${#ercBuffers}-2} | tr ' ' ',' | tr -d '"');then
echo "<fc=#00ff00>["$ircstr"]</fc>"
A little more complicated, but it works, and I believe erc is more feature rich.]
I'll later try to explain my xmonad config, and include the (current) file. It handles everything for me, from handling my workpaces, windows etc.. to changing my keyboardlayout and handling my media keys. It rocks!