Monthly Archives: October 2010

Qt C++ Screen Capture using BitBlt 2

So I got to thinking I wanted to be able to take abitrary screen captures based on some startx/starty -> endx/endy input. I found various sites showing how to do this, but most of them were pretty confusing. I thought I would post the final working code here. void RSystem::snap(int startx, int starty,int endx,int endy) { HDC hdcScreen,hdcMemDC = NULL; HBITMAP hbmScreen = NULL; BITMAP bmpScreen; HWND hDesktopWnd = NULL; hDesktopWnd = GetDesktopWindow(); hdcScreen = GetDC(hDesktopWnd); hdcMemDC = CreateCompatibleDC(hdcScreen); if (!hdcScreen) { qDebug()

Samurai Sword with 1 million layers OMFGBBQTURNIP 2

I swear to Christ if I read one more comment by an idiot talking about Damascus Steel layered 1 million times, and the ultimate Japanese sword I am going to scream. How it’s done: You take four billets, if you don’t know what a billet is, then stop talking about forging. You fold those four, they become 8, hammer it down and fold those 8 they become 16, hammer it down and fold those 16 they become 32. As it turns out, to get 1 million layers you only need to fold it 18 times (Thanks to Dan for pointing out the error, at 28 folds, it would be 1 billion layers!). Fold: 1: 8 Fold: 2: 16 Fold: 3: 32 Fold: 4: 64 Fold: 5: 128 Fold: 6: 256 Fold: 7: 512 Fold: 8: 1024 Fold: 9: 2048 Fold: 10: 4096 Fold: 11: 8192 Fold: 12: 16384 Fold: 13: 32768 Fold: 14: 65536 Fold: 15: 131072 Fold: 16: 262144 Fold: 17: 524288 Fold: 18: 1048576 1. Japanese forging methods are not particularly better than any other. 2. The type of metal you use has a lot to do with it. 3. Basic types of steel used in crow bars, car springs, and such are leaps and bounds ahead of swords made more than 100 years ago. 4. There were no advanced secrets of metallurgy 500-1000 years ago that made those swords magically stronger than a sword you’d make today out of spring steel. STOP ACTING LIKE THERE IS. The inspiration for this rant came […]

HowTo JavaScript/jQuery place divs in a circle, calculate a cirlce etc. 4

So, I was working with my father, a physicist and mathematician, who wants to have a little math example done in javascript. We haven’t finished the meat and potatoes of the project, but one hurdle was the necessity to place 8 divs in a circle on the page. This is a variation on the Tirgrams and Clifford Algebras demonstration that comes with the Mathmatics software Wolfram. Essentially this displays the trigrams in a clock formation, right to left around a circle. I looked around for a few examples and none of them seemed to work out the way I wanted them too, so I asked him to help and this is what we came up with. //trigrams are a class that contain divs displaying 1 of 8 trigrams. function draw() { var windowWidth = $(window).width(); var windowHeight = $(window).height(); var center = {x: windowWidth / 2 – 100,y: windowHeight / 2}; //100 is the width of the div var doc = 150; // radius var cpos = {x: center.x, y: center.y – doc }; //because the first div needs to be on top for (var i in trigrams) { trigrams[i].css({top: cpos.y, left: cpos.x, position: ‘absolute’});//this method just sets the .css left and top cpos = {x: cpos.x – center.x, y: cpos.y – center.y}; cpos = get_position(cpos.x,cpos.y); cpos.x = center.x + cpos.x; cpos.y = center.y + cpos.y; $(‘body’).append(trigrams[i]); } } function get_position(x,y) { var z = x; x = 1/Math.sqrt(2) * (z – y); y = 1/Math.sqrt(2) * (z + y); return {x: x, y: y}; […]

Tip: Flex/AS3 keyboardEvent listener

If you are writing a flex app, you might get this error TypeError: Error #1009: Cannot access a property or method of a null object reference. if you do, it simply means that you are trying to do something with an object that hasn’t yet been created. If that object is the stage, then you are probably calling an init funtion on creationComplete. If you change this to applicationComplete, you’ll have the stage present. <?xml version=”1.0″ encoding=”utf-8″?> <mx:Application xmlns:mx=”” creationComplete=”init();” … >

HowTo Knock it off with crazy MYSQL queries and the qTranslate plugin unistall

We’ve all done it. Either we learn some tricks in MYSQL, or read a cool post about some obscure programming trick you can do with SQL and decide to try it in a pinch on a live database. Just don’t do it. 9 times out of 10 you have access to SSH, and PHP, and you can write a script that will do what you want. You can also “test” it on a small set of inputs and have it echo out the necessary queries to the console instead of sending them to MYSQL. This is called the poor man’s unit test, or poor man’s BDD. Now, on one word press site, before the whole wordpress MU thing, we made the mistake of using qTranslate, well, it’s not evil, it’s just not good, so we wanted to stop using it, however, there’s no real way out of it. Search around and you find this post on their boards DON’T YOU EVEN DARE DO THIS. It’s just bad programming. You will probably hose your database doing this. Instead, let’s look at a more sensible solution, using your favorite scripting language, Ruby or PHP will do fine, I’ll choose PHP. $connection = mysql_connect(‘’,’user’,’pass’); mysql_select_db(‘yourdb’); $prefix = ‘your_table_prefix’; $res = mysql_query(“select * from {$prefix}posts limit 5″); //limit for testing $wanted_language = ‘en’; function split_text($text,$quicktags = true) { //The following code is kluged from the actual qTranslate Plugin $split_regex = “#(<!–[^-]*–>|\[:[a-z]{2}\])#ism”; $current_language = “”; $result = array(); $blocks = preg_split($split_regex, $text, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); foreach($blocks as $block) { if(preg_match(“#^<!–:([a-z]{2})–>$#ism”, $block, […]