2008/09/25

Hello Visitor!!

Hello! I can't use my server on 24 hours when 1 year later, so I decide to find free web hosting service.

Now, my sites & blog are moving to the http://sokoyo.co.cc

You can find new archives in the new space.

Of course, you can read the old archives in the new place, too.

But you may feel strange about the archives.

I will modify the archives when I have time in the future.

Thanks for your attention. :)





Hello! 因為我一年後就沒辦法架 server 了,所以我打算用免費的網路空間。

我的 Blog 和網站正移往 http://sokoyo.co.cc

你可以在那找到我新的東西

當然你也可以在那找到舊東西

不過可能會看起來怪怪的

我以後有時間會修改它

感謝您注意我的網站 :)

2008/09/12

PHP Hosting-免費PHP空間

PHP Hosting-免費PHP空間

再一年就要畢業了 @o@

畢業後就沒有可以用的網路空間了

有空來試試這些空間

2008/09/01

判斷三點間順時針或逆時針的關係


若有三點如上圖所示

要判斷 P2 在 P1 的哪個方向

可以先利用 V1.V2 = |V1| |V2| cosθ 和 arccos(cosθ) 求得角度

要判斷順時針與逆時針則先求得 vector_P0P1 與 vector_P0P2

Let

vector_P0P1 = (vx1, vy1) = (P1x - P0x, P1y - P0y)
vector_P0P2 = (vx2, vy2) = (P2x - P0x, P2y - P0y)



vector_P0P1 X vector_P0P2 > 0 為圖左方,逆時針(counter-clockwise)
vector_P0P1 X vector_P0P2 < 0 為圖右方,順時針(clockwise)

vector_P0P1 X vector_P0P2 = (vx1, vy1) X (vx2, vy2) =

| vx1 vx2 |
| vy1 vy2 | = vx1*vy2 - vx2*vy1 = |P1P0| |P2P0| sinθ

所以

vx1*vy2 - vx2*vy1 > 0 為圖左方,逆時針(counter-clockwise)
vx1*vy2 - vx2*vy1 < 0 為圖右方,順時針(clockwise)

參考資料:http://www.csie.nctu.edu.tw/~sctsai/adprog/notes/CompGeo.ppt

2008/08/02

2008/07/20

2008/07/12

NS2 Energy Model

在 ns2 中套用有 energy 限制的環境方法如下

使用舊的 trace format 則可以看到

[energy 979.917000 ei 20.074 es 0.000 et 0.003 er 0.006]

這種 trace log

分別代表

energy: total remaining energy
ei: energy consumption in IDLE state
es: energy consumption in SLEEP state
et: energy consumed in transmitting packets
er: energy consumed in receiving packets

[code]
# ...

set opt(energymodel) EnergyModel ;# EnergyModel type
set opt(initenergy) 1.0 ;# Initial Energy, specified in Joules
set opt(rP) 1.0 ;# Receiving Power, specified in Watts
set opt(tP) 1.1 ;# Transmitting Power, specified in Watts
set opt(iP) 0.01 ;# Idle Power, specified in Watts
set opt(sP) 0.001 ;# Sleep Power, specified in Watts
set opt(transP) 0.2 ;# Transition Power, specified in Watts
set opt(transT) 0.005 ;# Transition Time, specified in Seconds

# ...

#$ns use-newtrace

# ...

$ns node-config -adhocRouting $opt(rp) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channel [new $opt(chan)] \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON \
-energyModel $opt(energymodel) \
-initialEnergy $opt(initenergy) \
-rxPower $opt(rP) \
-txPower $opt(tP) \
-idlePower $opt(iP) \
-sleepPower $opt(sP) \
-transitionPower $opt(transP) \
-transitionTime $opt(transT)

# ...
[/code]

2008/07/11

Change wireless color as in wire network

Reference: http://mailman.isi.edu/pipermail/ns-users/2002-May/023058.html?

NS2 Wireless Simulation 中要讓 node 有顏色

只能用下面這種方法

[code]
# ...

# let the node has a color property
$node($i) color black

# change the color during animation
$ns at 0.0 "$node($i) color green"

# ...
[/code]



如果設定 node 的 label-color 會讓 node 的位置變成 (0, 0)



如果想要兩 node 間有線(ex: node 1, 2 之間)

在 tcl 檔內可以用下面這種方法

[code]$ns puts-nam-traceall "l -t * -s 1 -d 2 -c RoyalBlue -S COLOR"

$ns at 0.3 "$ns puts-nam-traceall \"l -t 0.30000000000000 -s 1 -d 2 -c blue -S COLOR\""
$ns at 0.4 "$ns puts-nam-traceall \"l -t 0.40000000000000 -s 1 -d 2 -c red -S COLOR\""
$ns at 0.5 "$ns puts-nam-traceall \"l -t 0.50000000000000 -s 1 -d 2 -S out\""
$ns at 0.6 "$ns puts-nam-traceall \"l -t 0.60000000000000 -s 1 -d 2 -c blue -S COLOR\""
$ns at 0.7 "$ns puts-nam-traceall \"l -t 0.70000000000000 -s 1 -d 2 -c red -S COLOR\""
$ns at 0.8 "$ns puts-nam-traceall \"l -t 0.80000000000000 -s 1 -d 2 -S out\""
[/code]

或是寫在 Agent 用下面這種方法

[cpp]Tcl::instance().evalf("[Simulator instance] puts-nam-traceall \"l -t "TIME_FORMAT" -s %d -d %d -c %s -S COLOR\"", Scheduler::instance().clock(), 1, 2, "blue");[cpp]

其他可以使用的顏色請參考 /usr/lib/X11/rgb.txt 檔案

關於 nam 讀取的格式,可以在 X window 的環境下
用 nam -p 指令查詢

如果想要用 man 查詢 nam 請將 nam.1 複製到 /usr/man/man1
ex: cp ~/ns-allinone-2.33/nam-1.13/nam.1 /usr/man/man1/

ns2 中移動 node

1. 隨意移動

[code]
# ...

$node($i) random-motion 1

# ...

$ns at 1.0 "$node($i) start"

# ...
[/code]

把 NAM 模擬結果作成動畫

用 ns2 寫模擬很無聊 !?

不方便到沒有安裝 ns2 的電腦 demo !?

如果能把模擬結果變成 demo video 應該很讚吧 !?

以下介紹如何把 nam 看到的結果變成模擬影片 (avi)

處理 NS2 Scenarios Generator 產生的 tcl

NS2 Scenarios Generator
是個不錯的 ns2 scenario 產生工具

但是產生出來的 tcl 可能在命名上不是我所慣用的

舉例來說

他會產生以下這種表示 node 的方式

[code]$n0 set X_ 907
$n0 set Y_ 975
$n0 set Z_ 0.0[/code]

但我比較習慣寫成類似 array 的方式

例如

[code]$node(0) set X_ 907
$node(0) set Y_ 975
$node(0) set Z_ 0.0[/code]

這個時候問題來了

如果有 1000 個點

總不可能一個一個手動改吧 囧

這個時候 vim 的搜尋取代功能就非常好用了

利用以下寫法就能一口氣換過來喔

:%s/\$n\([0-9]\+\)/$node(\1)/g


想要瞭解更多 vim 的用法請參考這篇文章

以下介紹我覺得最常用到的

1. 單純置換字串

:%s/old string/new string/g
以上用法會把檔案內所有的 old string 變成 new string

2. 利用 regular expression 置換字串

:%s/[0-9]\+/number/g
以上用法會把數字(ex: 1, 12, 130, ... etc)變成字串 number

:%s/a.b/string/g
以上用法會把字串 aab, abb, a0b, a9b, a!b, ... 等,a開頭b結尾,三個字為一組的字串變成字串 string

3. 利用 regular expression 把比對出的字串重新組合出新的字串置換

:%s/\(abc\) \(def\)/This is \1 & \2/g
以上用法會把字串 abc def 變成字串 This is abc & def



vim 功能非常強大

只要用習慣了

就會覺得非常方便喔

2008/07/10

執行 nam 時出現 "Missing required flag" error

Reference: http://mailman.isi.edu/pipermail/ns-users/2003-April/031477.html

Problem: ns2 在執行 nam 在模擬結束時出現出現以下錯誤


Missing required flag -x in: W -t 5.0

Missing required flag -y in: W -t 5.0

Parsing error in event.



Solution:
發生的原因是因為找不到 nam 檔寫入
也就是說寫了關閉 nam 檔後
仍然有資料要寫入 nam 檔
因此要解決就是關閉 nam 檔前
tcl 要先執行到 $ns at $opt(stop) "$ns nam-end-wireless $opt(stop)"

關閉模擬的 example:
[code]set opt(stop) 5.0 ;# time of simulation end

# Create a ns simulator
set ns [new Simulator]

# ...

# Open the NS trace file
$ns use-newtrace
set tracefile [open out.tr w]
$ns trace-all $tracefile

# Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $opt(x) $opt(y)

# ...

#===================================
# Termination
#===================================

# Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile opt
$ns at $opt(stop) "$ns nam-end-wireless $opt(stop)"
$ns flush-trace
close $tracefile
close $namfile

puts "done"
$ns halt

puts "running nam..."
exec nam out.nam &
exit 0
}
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns at $opt(stop) "$node($i) reset"
}
$ns at $opt(stop) "finish"

puts "Starting Simulation..."
$ns run[/code]

[Solution] Warning: Tracefile events are not sorted by time

Reference: [patch] NAM Time precison and wireless packet coloring (cmu-trace.cc)

Problem: NS2 shows the "Warning: Tracefile events are not sorted by time" warning.

Solution: Modify file "ns-allinone-2.32/ns-2.32/trace/cmutrace.cc"

Any %.9f characters in the file change to "TIME_FORMAT" characters.

AWK

中文:
http://linux.tnc.edu.tw/techdoc/shell/x792.html
gawk 手冊 (mirror)
http://www.study-area.org/cyril/scripts/scripts/node61.html
AWK Tutorial Guide

English:
The GNU Awk User's Guide

2008/06/28

三種解 Recursive Function 的方法

1. The iteration method (疊代法, 或代入展開法)
2. 畫 Recursion Tree
3. Master Theorem

2008/06/27

[轉錄]讓男女都覺得窩心的事

讓男生感到窩心的事:
1. 在朋友面前很給他面子
2. 像小孩子般對他撒嬌
3. 對他很放心,不要一天到晚疑神疑鬼
4. 常稱讚他的優點,把他當成妳的英雄
5. 親手做一些點心或小禮物,讓他的朋友羡慕
6. 穿著他喜歡的衣服樣式,做他欣賞的打扮
7. 聽他說話時,會一副很崇拜他的樣子
8. 就算有別人追求或看見帥哥時,都會說他比那些人棒多了
做到這些,想變心都難,因為這樣的女孩不好找了!!~~~

讓女生感到窩心的事:
1. 了解她心中的感受,不要表現出無聊或不耐煩
2. 常說溫柔體貼的話
3. 記得她的服裝、髮型,也要知道她今天做了什麼改變
4. 到哪裡都能告訴她和誰在一起?何時回家?
5. 吵架時會讓她,先說對不起
6. 讓她有安全感
7. 不用讓她擔心你會出軌
8. 偶爾浪漫的送她花或小禮物如此一來會更恩愛喔!!~~~

2008/05/16

使用標準的 DOM 取代 innerHTML

innerHTML 可以用來取代 HTML 的內容

以下介紹另外一種使用標準的 DOM 取代 HTML 的方法

2008/05/15

XML DOM (Document Object Model)

XML DOM (Document Object Model) 定義存取和操作 XML 文件的標準方法

XML file example:

[xml]


Text

 
[/xml]

2008/05/10

Windows XP 無法 update

正版的 Windows XP 無法 update

依照下面的方法解決:

Re-register the Windows Update DLL with the commands below

1.Click Start, click Run, type cmd, and then click OK.

2.Type the following commands. Press ENTER after each command.


regsvr32 wuapi.dll
regsvr32 wuaueng.dll
regsvr32 wuaueng1.dll
regsvr32 wucltui.dll
regsvr32 wups.dll
regsvr32 wups2.dll
regsvr32 wuweb.dll

2008/05/09

2008/04/14

緊握著妳的手
伴隨著妳的夢

珍惜和妳在一起的每一秒鐘

靠著妳的肩膀
聞著妳的髮香

在夢醒前的一煞那

聽妳述說著故事
看妳描繪著天堂

NS2 週期性執行某個工作

1. 設計 Handler Class

2008/03/28

[轉錄] 一手好牌,卻越打越糟。


作者 malignancy (惡性腫瘤) 看板 CATCH
標題 Re: [分享] 一手好牌,卻越打越糟 2
時間 Fri Mar 28 00:25:04 2008
───────────────────────────────────────


給原 po:

非常恭喜你,我覺得你已經學到了非常重要的一件事:
察覺自己的需要和感覺.

成績好的男人,醬料生,或者電子狗,
最大最大的問題通常都是理性過頭.

回到最最開始的時候,你也跟包括我在內的其他男人一樣,
喜歡一個女人也許就是某一個模門特觸動了某些東西,
然後就開始努力的盤算要怎樣才會贏.

對我們這種人來說,玩學業遊戲或者工作,
要贏,是一種最基本的信念和態度.
所以我們會不斷的收集資訊,瞭解遊戲規則,
然後去收集"贏"的定義下所需要的東西.

我們習慣了這種模式之後,常常就會委屈了自己.
在考場上,在工作場合裡,誰會管你有什麼感受?

因為太習慣,所以我們也很容易用同樣的模式套用在感情上,
光看你下的標題就知道你是這樣.
所以你不懂,為什麼一副好牌被你打到爛掉.

身為醬料前輩,我想與你分享,
醬料生要好好為自己想,真的.

以前的醬料生有很多很多的悲劇,
比如說,每天每天忙的要命,不停的開刀,看診,賺好多好多的錢,
買大房子,讓妻小住美國,從小穿好吃好住好,念的都是超貴的貴族學校...

醬料生呢?歹勢,一個人孤伶伶的在台灣賣老命.
好一點的每天拖著疲憊的身軀回到住處入睡.
背德一點的就跟其他的小護士睡了.

我想說,乍看來好像是很幸福,一門和樂富裕.
但是事實上,對醬料人來說真的只是一場惡夢.

走上這條路,我只能說前方還有無限的挑戰和操勞.
沉重的課業和壓力沉重的工作會佔滿你的青春.
所以你更應該多為了自己考慮.

我寫了之前的文章,也有一些醬料人來信,
問我應不應該在這疲累到要死人的十年中談戀愛.
(大三開始到升主治...漫長到看不到終點...)

回首那十年,我只能說,我多麼渴望有一個人在我疲倦欲死的時候給我一點溫暖,
哪怕是一個笑容,一個擁抱,一個親吻,或者相擁而眠都好...

我當年就因為不懂得說出自己的需要,
白白浪費大把的青春在無緣的女人身上,換來的是無邊的寂寞和痛苦.
傻等數年之後還自以為痴情.

當你還是學生的時候,你什麼都不會是;
除非你很帥,否則護理站的護士還會嫌你擋路.
隨著你的年資越來越老,收入相對增多,
你也會發現開始有人會注意到你的一手好牌,然後明示暗示你可以怎樣怎樣.

可是,那真的是你要的嗎?

我還年輕的時候很渴望這種征服的感覺,
可是真的等我擁有了一手還可以的牌,
開始在牌桌上有了主動權的時候,
我想起了前面提過的"內在美"(內人在美國帶小孩),"台獨"(在台孤單一人),
這一類的醬料悲劇...

你的一手好牌的確可以讓你選中的人幸福,
可是重點是,那個人能讓你幸福嗎?

回到我自己,有人曾經質疑我,我真的喜歡現在的閃光,快成為我老婆的這個人嗎?
我承認這是很難回答的問題.
一開始我可能只因為她單純可愛,相處愉快就開始喜歡她.
也曾經覺得她頭腦太簡單,一度想過要換人做看看.
但是,人生有太多的起起伏伏和莫名其妙的不幸.
經過了很多事之後,她還是陪在我身邊.

我想我們現在的感覺已經不是像當初那樣了.
我們彼此都熟知對方的脾氣和個性,也知道對方的喜惡和優缺點,
已經不需要太多的矯飾和偽裝,就像家人一樣.
就因為覺得是家人,所以才會想娶她進門啊.

我想說的是,就像自己媽媽,她不會因為你的好牌而覺得你應該付出更多,
她會盡力的照顧你,只因為她是你媽.
我們應該要娶的女人,應該也是這樣:
她會盡力的照顧陪伴幫忙你,只因為她是你老婆,她知道你也會這樣對她.

很感慨,如果十年前,有人跟我說過這些話,我想我的人生會有很大的不同.
也希望這篇文章可以彌補我當年的遺憾....

--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.114.15.22

2008/02/26

extern "C"

Reference: http://blog.anglee.org/2007/02/extern-c.html

簡單來說

有個 function 定義為 void func(int a, float b);

用 C Compiler compile 後內部名稱會類似 _func
用 C++ Compiler compile 後內部名稱會類似 _func_int_float

C++ compiler 會在 function call 出現在 extern "C" scope 中時,產生並使用 C style 的內部名稱。

要內部名稱一樣 Linking 時才不會出錯。

2008/01/20

NS2 快速入門: DV-distance 模擬 (7)

最後完成的 code 與參考文件

有問題歡迎一起討論

DV-Distance NS2 Simulation

加入這個 Agent 的方法請參考這篇文章