Sunday, 28 June 2009

Twistable Flash object with ActionScript3

Hi Readers,

It came to my mind I've never made a twistable element in Flash. So I wrote one. The basics:
It needs an item (descendant of the DisplayObject class) to twist. Don't forget to position this element to the point you want to twist around. (Most cases it is the center point.) My idea is when you click on the object, you store the current angle relative to center of ui element and store the current angle of the object. Then if you move your cursor, you will know the difference between the initial angle and the current angle. And you add this difference to the stored angle of the object. [It is easy, however, my english makes it hard to understand.] The interesting thing is to get the angle from a cursor position. I thought that the good old trigonometrics functions give the shade between 0 and 360 but, it came to light, I was wrong. Some recall:
[http://en.wikipedia.org/wiki/Sine#Sine]
A triangle with a 90 degree angle has 3 side: adjacent, opposite, hypotenuse. You can calculate the tangent alpha by division of the oposite with adjacent. Arctangent gives the reverse of tangent, so atan(tan(alpha)) = aplha. But if you make some test you realize its not the whole 360 degree (or 2PI in rad) but 2 peaces of 180. Strange, but I have never been a math guy.

Anyway, here you are my quick source. Just paste to a Flash IDE as it is and run it.
var angle_onDown:Number;
var angle_potmeter:Number;
var is_turn:Boolean = false;

var potmeter = new Sprite();
potmeter.graphics.beginFill(0xAA4422);
potmeter.graphics.drawRoundRect(-100, -100, 200, 200, 20, 20);
potmeter.graphics.endFill();
potmeter.x = potmeter.y = 200;
addChild(potmeter);

potmeter.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
potmeter.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
potmeter.addEventListener(MouseEvent.MOUSE_UP, onUp);

function onDown(event:MouseEvent):void {
is_turn = true;
angle_onDown = getCurrentAngle(event.stageX - potmeter.x, event.stageY - potmeter.y);
angle_potmeter = potmeter.rotation;
}

function onMove(event:MouseEvent):void {
if (is_turn) {
potmeter.rotation = angle_potmeter - (angle_onDown - getCurrentAngle(event.stageX - potmeter.x, event.stageY - potmeter.y));
}
}

function onUp(event:MouseEvent):void {
is_turn = false;
}

function getCurrentAngle(relX:Number, relY:Number):Number {
var relDegree:Number = Math.atan(relY / relX) / Math.PI * 180;
return relX < 0 ? 180.0 + relDegree : relDegree;
}

You can grab the rectangle and twist.

I would be happy if anyone can explain why the formula in the getCurrentAngle() function gives 2 peaces of 180 interval? (That is why I added the ternary operator for the return command.)

Night,
Peter

Wednesday, 24 June 2009

PHP Beauty

Hi Readers,

Google announced this article collection recently: http://code.google.com/speed/articles/optimizing-php.html I thought 'What the heck, let's bechmark something.', and took a small experiment:

<div style="display:none;">
<?php

$sum = 0;

$count = 10000;
$iteration = 100;

for ($j = 0; $j < $iteration; $j++) {
$t1 = microtime(TRUE);
for ($i = 0; $i < $count; $i ++) {
//print "aaa"."bbb"."ccc"."ddd"."eee"."fff"."ggg"."hhh";
echo 'aaa','bbb','ccc','ddd','eee','fff','ggg','hhh';
}
$t2 = microtime(TRUE);
$sum += $t2 - $t1;
}
?>
</div>
<?php

var_dump($sum / $iteration);

And the results were quite nice: 0.026 against 0.017. So that is why echo still rocks, because with echo you can use comma instead of period. Never heard of commas for echo before. This is a PHP magic:)

Night,
Peter

Monday, 22 June 2009

OS X + PHP Part 2

Hi Readers,

As I promised, this my second explanation about how to build a PHP web development environment in OS X (10.5).
As I mentioned a very basic configuration ships with the distro by default. But for me it was not enough. Going further from my last setup (http://itarato.blogspot.com/2009/06/first-macbook-webdeveloper-enviroment.html) I noticed there isn't APC for PHP. No GD2 support (neither GD1). I depressed till found this miracle: http://www.entropy.ch/software/macosx/php/
Marc Liyanage compiled a very good PHP 5.2.9 with a lot of modules. If you step through his description you can install it quite easily. At the end you get a new php.ini. You need to add your previous xdebug settings to it. For APC see this article: http://discussions.apple.com/thread.jspa?threadID=1578979. As it turned out, the pecl related APC is wrong on OS X, but the original APC compilation will work.
Lately my favorite high end IDE is the stable NetBeans (6.7 RC3 has a StackOverflow for me). For PHP it works out of the box but for any Java related project I had to change the default java classpath: http://statistics.netbeans.org/analytics/detail.do?id=150231

In a nutshell thats all about it. The MySQL and Apache server works as I wrote before. By the way just for play I tried the free MAMP application, but I found it a little bit old for my claims. It has a pretty cool minimalistoc control interface, works fine but doesn't contain some imprtant module. To be honest I was't bother with it so much. (My bad.)

One thing. Last week I began to use NetBeans for debugging PHP projects. The reason I didn't use it for debug I didn't know how I can examine pages other than index.php. But as a matter of fact if it starts and finish it's first debug run you can enter any subpage into the browser (the same has the xdebug session) and the debug cycle will start again. So its really cool. There is a big advantage against ViM+XDebug, I like more NetBeans's variable browser. In ViM you are restricted to the php xdebug config (ex maximum variable nested level) and the limited height of the editor. But sake of NB's tree visualizaton you can traverse arrays and objects quite convenient.
(Other tiny minus sign for osx - the default key bindings messes the F2..F6 key usages in vim.)

Thats it. Any webdev environment related question would be welcomed:)

Bye,
Peter

Sunday, 14 June 2009

SQL dump with each database separately

Hi Readers,

This morning I missed my old MySQL tables from Windows XP, but for some reason I needed separate database dumps for each db. As far as I know I've 2 options to do that, dump each db in command line or to write a script in command line. No way. So I made a quick & dirty PHP script to generate the dump commands:
<?php

$usr = 'YOURUSER';
$pwd = 'YOURPASS';
$date = date('Ymd');

$conn = mysql_connect('localhost', $usr, $pwd);
$resource = mysql_list_dbs();

while ($row = mysql_fetch_object($resource)) {
echo 'mysqldump -u '.$usr.' --password='.$pwd.' '.$row->Database.' > mysqldump_'.$date.'_'.$row->Database.'.sql'."\n";
}


And then:
c:\php dumpscript.php > dumpcmd.bat
c:\dumpcmd.bat

Thats it.

Gbai!

Saturday, 13 June 2009

First Macbook - webdeveloper enviroment

Hi Readers,

This week I've got my first Macbook. I don't want to say any advertisement for the machine. Apple already has a very ugly campaign, just check www.apple.com and watch some commercial. Anyway. I love it even if it's so hard to setup.
Some quick steps, how you can setup a minimal web developer environment if you've never seen OS X.
  1. OS - by default
  2. Apache (2.2) - Enable it in System preferences / Sharing (Web) [http://www.procata.com/blog/archives/2007/10/28/working-with-php-5-in-mac-os-x-105/]
  3. Apache rewrite - overwrite appropriate line in httpd.conf to: AllowOverride All
  4. PHP - by default
  5. MySQL - download the official 5.06 version package (install both two dmg-s), and link the /tmp/mysql.sock to /var/mysql/mysql.sock
  6. MySQL - if mysql doesn't allow to get in, you have to hack an account [http://www.linuxquestions.org/linux/answers/Security/MySQL_root_password_recovey]
  7. XDebug : download macvim, xdebug plugin for vim and compile the xdebug extension [http://www.chrissearle.org/blog/technical/running_xdebug_204_osx_leopard_apachephp]
Important paths / files:
  • /etc/php.ini
  • /etc/apache2/httpd.conf
  • /etc/apache2/users/youruser.conf
  • /private/etc/... [almost the same as in /etc]
  • apachectl restart/start/stop
  • /usr/local/mysql/bin/mysql
  • /Library/StartupItems/MySQLCOM/MySQLCOM start/stop/restart
It just an overview, it's highly suggested to read some external materials. My intention was giving a small insight what you should expect. For me (as a first-time macer) was kinda hard to setup the basic stuffs. Of course there are easier ways. If you want to find some seamless solution, just check out these projects: MAMP, Fink.

I want to write about my other experiences about the mac, but for now there are tons of works to do, so back to mac.

Night!

Monday, 1 June 2009

Tiny Hanoi solver C in a tweet

Hi Readers,

Recently I've started to re-learn C. (You know, always fun:) Unfortunately I've never know the language well. So, that is my smallest code that can resolve the so-called Tower of Hanoi problem:
http://en.wikipedia.org/wiki/Tower_of_Hanoi

Actually the code could fit in a tweet:) Check this out:

#include
main(){h(1,4,3);}h(int f,int t,int c){if(c==1)printf("%d>%d\n",f,t);else{int b=7^(f|t);h(f,b,c-1);h(f,t,1);h(b,t,c-1);}}


Night,
Peter

Pattern recognision based Drupal trigger

Hi Readers,

Last week I contribute my second module on drupal.org:
webcam_trigger

I used the so popular technology came up recently: augmented reality with a small difference, not there isn't any 3D magic. (But I'm thinking on it.) You can find a detailed description on the link above.



Night,
Peter

Drupal sandbox creator shell script

Hi Readers,

I'm a really bad blogger. First, I never write. Secondly, I never write about new stuffs. I have my reason and to be honest, I've seen too many very good article so I had to realise, it's not going to be a tech blog. I'm really curious about how my coming MacBook affects to my reading habit. I've got a lot of hope in it. And another big news for me. I'll be a proud cat holder? Call me crazy but she will be my first child. (Besides that if her teacher says she is the smartest in the class - I won't believe it:) Anyway. To restart this stuff I intend to share a small Drupal site generator script with you.

What is the problem?
I'm a Drupal developer. I need to test modules before using it on production sites. I need to try out a lot of things. And the best for doing that is a Drupal sandbox. But it takes 1 or 2 minutes to create one. (At least for me.) ((It should be a Drupal install contest - yeah:))

Solution?
Create a sandbox once and pre-populate it when needs a new one.

Prepaire:
  1. Download the latest Drupal install: http://drupal.org/
  2. Install it in a directory called: drupal_sandbox
  3. Install all the modules you want by default (contrib ones as well)
  4. Use the database name of: drupal_sandbox in the settings.php
  5. Tar the drupal_sandbox directory: tar -cvzf drupal_sandbox.tar.gz drupal_sandbox
  6. Create an sql dump: mysqldump -u YOURUSER --password=YOURPASS drupal_sandbox > drupal_sandbox.sql


The script:

#!/usr/bin/env bash

# Directory where the tar.gz and sql file lies
package_dir=~/download
# Base file name for the tar.gz and sql files
package_name=drupal_sandbox
# Your web root
www_dir=~/www
# Your MySQL user
mysql_pwd=YOURMYSQLUSER
mysql_user=YOURMYSQLPASSWORD

cd $www_dir
tar -xvzf $package_dir/$package_name.tar.gz
mv $package_name $1
cd $1/sites/default
mv settings.php settings.php.bckup
sed "s/$package_name/$1/g" settings.php.bckup > settings.php

mysql -u $mysql_user --password=$mysql_pwd -e "create database $1"
mysql -u $mysql_user --password=$mysql_pwd $1 < $package_dir/$package_name.sql


Usage:
Call the script with a parameter (the name of the new drupal site (directory & sql))
sh ./drupal_creator.sh drupal_site_for_testing


Thats it. I hope it works for you. On my environment its fine.

Good luck,
Peter