太陽と方角
聞かれたけど答えられなかった質問 - 檜山正幸のキマイラ飼育記 (はてなBlog)
次男:「おとうさん、北はどっち?」
父親:「えーと、太陽があそこに見えるだろう。で、今は午後3時だから…」とか言ってみたが結局わからなかった。
主観方向感覚が残念な俺的には太陽と時間から方角を算出することは比較的日常に使うテクニックです。
これ時計の12時を太陽に向けてなんとかかんとかとか漫画で読んだことあるけど
そんなこといわれても腕時計持ってないしそもそもルールが回りくどいです。
そこで俺が実際意識することは「太陽は朝6時に東から昇り昼の12時に南を通って夕方6時に西に沈む」というこれだけ。
日の出日の入りが季節でどうこうとか細かいことは考えません。
これをもとにすれば午後3時なら南からだいぶ西に太陽はあるんだなって分かるし。
大まかにしか分からないけど結構便利な時は便利です。
urlclassifier3.sqliteのvacuum
Firefoxが終了が異常に遅いのが前から気になってたのでなんかどっかで読んだvacuumを試す。
C:\Documents and Settings\*\Local Settings\Application Data\Mozilla\Firefox\Profiles\*.default
にあるurlclassifier3.sqliteをlinux上にコピー。うちのWindowsにsqliteなんて入ってないから。
んでsqlite3 urlclassifier3.sqlite vacuum実行。
結果55M位のが13Mに。減りすぎだろ。
終了も多分早くなったよ。つーかなんかそういう機構を最初から持てと思った。
VMware server2.0をインストール
いままでは1.Xを使ってまして。そういやあ2が出てたなあということで。
インストールはまあ普通にクリックで大丈夫。ホスト環境はWindows。
そのあとVMwareアイコンクリックするとブラウザが立ち上がりなんかログインフォーム登場。
何入れるのよとvmware server2.0 login nameとかでグーグルしても良く分からない。
適当にやってるとWindowsへのログイン情報で大丈夫と判明。
んでそれ通るとWebアプリの管理ツールになって、
ここでも既存のデータを使う方法が良く分からない。
右の方のadd datastoreでlocalを選択、Directoryに既存フォルダってしたけどうまくいかない。
調べるに原因はパーミッションだった。cygwinからchmod 666とかやって解決。
既存フォルダ流用の場合注意。
これでとりあえず動いた。
msrubyをmake
Visual C++ 2005 Express Edition使用。
とりあえずwin32行ってcnofigure.bat実行。
C:\ruby-trunk\win32>configure.bat NMAKE : fatal error U1077: 'cl' : リターン コード '0x2' Stop.
何も分からないけどとにかく失敗。
なんかだらだらとファイルをみててsetup.makにこういう部分発見。
-runtime-: nul @$(CC) -MD <<rtname.c user32.lib > nul
nulにメッセージ捨ててるっぽいので消す。
-runtime-: nul @$(CC) -MD <<rtname.c user32.lib
再度実行。
C:\ruby-trunk\win32>configure.bat rtname.c rtname.c(1) : fatal error C1083: include ファイルを開けません。'windows.h': No such file or directory NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 8\VC\BIN\cl.EXE"' : リターン コード '0x2' Stop.
ええなんでwindows.hないの?つーかそもそもなんでメッセージ捨ててるの?
間際のダイイングメッセージ全力スルー的な。
ともかく環境変数とかwindows.hのありかとかチェックしてるに
パスが通ってないね。コンソールからVCとかめったに使わないから気づいてなかったよ。
つーことで起動バッチを以下に書き換え。
@echo off call "%VS80COMNTOOLS%vsvars32.bat" @set PATH=%PATH%;c:\cygwin\bin @set INCLUDE=%INCLUDE%;C:\Program Files\Microsoft Platform SDK\Include @set LIB=%LIB%;C:\Program Files\Microsoft Platform SDK\Lib cmd.exe
さいどconfigure、で通ったのでnmake。ぶじおわったっぽい。
ruby -v ruby 1.9.1 (2008-12-05 patchlevel 5000 revision 20534) [i386-mswin32_80]
make testは大丈夫でmake test-allするとまたも謎に死亡。
NMAKE : fatal error U1077: '.\ruby.exe' : リターン コード '0xc0000005' Stop.
ダイイングメッセージは0件です。
Ruby-1.9.1 preview2
Ruby 1.9.1 preview 2出たのでtest-allしてみた。VMware上。
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 8.10 Release: 8.10 Codename: intrepid $ uname -a Linux * 2.6.27-9-generic #1 SMP Thu Nov 20 21:57:00 UTC 2008 i686 GNU/Linux
1) Error: test_class_build(TestGemExtExtConfBuilder): Gem::InstallError: extconf failed: /usr/local/bin/ruby extconf.rb /home/*/src/ruby-1.9.1-preview2/rbconfig.rb:7: ruby lib version (1.9.1) doesn't match executable version (1.8.7) (RuntimeError) from /home/*/src/ruby-1.9.1-preview2/lib/mkmf.rb:4:in `require' from /home/*/src/ruby-1.9.1-preview2/lib/mkmf.rb:4 from extconf.rb:1:in `require' from extconf.rb:1 /home/*/src/ruby-1.9.1-preview2/test/rubygems/test_gem_ext_ext_conf_builder.rb:24:in `block in test_class_build' /home/*/src/ruby-1.9.1-preview2/test/rubygems/test_gem_ext_ext_conf_builder.rb:23:in `chdir' /home/*/src/ruby-1.9.1-preview2/test/rubygems/test_gem_ext_ext_conf_builder.rb:23:in `test_class_build' 2) Failure: test_class_build_extconf_fail(TestGemExtExtConfBuilder) [/home/*/src/ruby-1.9.1-preview2/test/rubygems/test_gem_ext_ext_conf_builder.rb:66]: Expected "extconf failed:\n\n/usr/local/bin/ruby extconf.rb\n/home/*/src/ruby-1.9.1-preview2/rbconfig.rb:7: ruby lib version (1.9.1) doesn't match executable version (1.8.7) (RuntimeError)\n\tfrom /home/*/src/ruby-1.9.1-preview2/lib/mkmf.rb:4:in `require'\n\tfrom /home/*/src/ruby-1.9.1-preview2/lib/mkmf.rb:4\n\tfrom extconf.rb:1:in `require'\n\tfrom extconf.rb:1\n" to match /\Aextconf failed: \/usr\/local\/bin\/ruby extconf.rb.* checking for main\(\) in .*?nonexistent/m. 3) Failure: test_handle_special_CROSSREF_no_underscore(TestRDocMarkupToHtmlCrossref) [/home/*/src/ruby-1.9.1-preview2/test/rdoc/test_rdoc_markup_to_html_crossref.rb:87]: Expected "<p> <a href=\"../files/home/*/src/ruby-1_9_1-preview2/test/rdoc/rdoc_markup_to_html_crossref_reference_rb.html\">/home/*/src/ruby-1.9.1-preview2/test/rdoc/rdoc_markup_to_html_crossref_reference.rb</a> </p> ", not "<p> /home/*/src/ruby-1.9.1-preview2/test/rdoc/rdoc_markup_to_html_crossref_reference.rb </p> ". 4) Failure: test_exit_action(TestSignal) [/home/*/src/ruby-1.9.1-preview2/test/ruby/test_signal.rb:52]: [ruby-dev:26128]. Exception raised: <#<Timeout::Error: execution expired>>. 5685 tests, 2467622 assertions, 3 failures, 1 errors, 0 skips
配列シャッフル
単純で正しそうなものが正しいとは限らない - Radium Software
英語のもとページ読んでないけど。
インデックスを足していくか引いていくかはどうでもよくて、むしろ本質が紛らわしくなってる気がする。
def shuffle1(a) a.size.times{|i| n = rand(a.size) t = a[i]; a[i] = a[n]; a[n] = t } a end def shuffle2(a) a.size.times{|i| n = i + rand(a.size - i) t = a[i]; a[i] = a[n]; a[n] = t } a end def test cards = 1..4 h = Hash.new{0} 100000.times{ a = yield(cards.to_a) h[a[0]]+=1 } p h end p "shuffle1" test{|cards| shuffle1(cards)} p "shuffle2" test{|cards| shuffle2(cards)}
ruby shuffle.rb "shuffle1" {1=>24886, 2=>29201, 3=>24514, 4=>21399} "shuffle2" {1=>24809, 2=>25052, 3=>25215, 4=>24924}
俺的ナイーブなシャッフルは、配列からランダムな要素を切り出して新しい配列を作る感じ。
def shuffle3(a) b = a.dup ret = [] until b.empty? ret << b.slice!(rand(b.size)) end ret end
メモリがーとかは考えません。安いから買え。
Star Wars書いてみた
えーセルオートマトンがそもそも分かってないですがStar Wars書いてみた。
ルールはこちらの説明オンリーで作らせていただきましたありがとうございます。
http://yowaken.dip.jp/tdiary/20081122.html#p02
動きはこちらを見て確認させていただきましたありがとうございます。
セルオートマトンを用いたシューティングゲーム - Tosikの雑記
動いているのを見てて楽しいですね。デフラグずっと見ちゃう的ですね。
実装はIronPython+XNAでやろうと思ったんだけどTexture2Dでのべた矩形がなぜが出なくて。
なので普通にC#+XNAになってます。しかしC#でも同意のコード書いてんのになんで出ないんだ。
#region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; #endregion namespace starwars { public class Cell { StarWars parent; public int x; public int y; public int state; int next_state; public Cell(StarWars p, int _x, int _y) { parent = p; x = _x; y = _y; state = 0; next_state = 0; } int count_around(int target_state) { int count = 0; Cell[] a = new Cell[8]; a[0] = parent.get(x - 1, y - 1); a[1] = parent.get(x - 1, y); a[2] = parent.get(x - 1, y + 1); a[3] = parent.get(x, y - 1); a[4] = parent.get(x, y + 1); a[5] = parent.get(x + 1, y - 1); a[6] = parent.get(x + 1, y); a[7] = parent.get(x + 1, y + 1); for (int i = 0; i < 8; i++) { if (a[i] != null && a[i].state == target_state) count++; } return count; } public int reserve_state() { next_state = state; switch (state) {//TODO magic number case 0: if (count_around(1) == 2) next_state++; break; case 1: if (count_around(1) >= 3 && count_around(1) <= 5) { } else { next_state++; } break; case 2: next_state++; break; case 3: next_state = 0; break; } return next_state; } public int update() { return state = next_state; } } public class StarWars{ public Cell[] map; public int width, height; Random rand = new Random(); public StarWars(int _w, int _h) { width = _w; height = _h; map = new Cell[width * height]; } public Cell get(int x, int y) { //回り込み x = (x + width) % width; y = (y + height) % height; if (x < 0 || y < 0 || x >= width || y >= height) return null; return map[y * width + x]; } public Cell get(int i) { return get(i % width, i / width); } public Cell make(int x, int y) { Cell c = new Cell(this, x, y); c.state = rand.Next(3); return map[y * width + x] = c; } public Cell make(int i) { return make(i % width, i / width); } void reserve_state() { for (int i = 0; i < width * height; i++) { if (map[i] != null) { map[i].reserve_state(); } } } public void update() { reserve_state(); for (int i = 0; i < width * height; i++) { if (map[i] != null) { map[i].update(); } } } } public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; Texture2D tile; StarWars starwars; const int tile_size = 4; public Game1() { graphics = new GraphicsDeviceManager(this); } protected override void Initialize() { int w = 800 / tile_size, h = 600 / tile_size; starwars = new StarWars(w, h); for (int i = 0; i < w*h;i++) starwars.make(i); base.Initialize(); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { tile = init_tile(); } } Texture2D init_tile() { Color[] image = new Color[tile_size * tile_size]; for (uint i = 0; i < tile_size * tile_size; i++) image[i] = Color.White; Texture2D t = new Texture2D(graphics.GraphicsDevice, tile_size, tile_size, 1, ResourceUsage.Tiled, SurfaceFormat.Color); t.SetData(image); return t; } void put_tile(SpriteBatch batch, int x, int y, Color c) { batch.Draw(tile, new Vector2(x*tile_size, y*tile_size), c); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); SpriteBatch spriteBatch = new SpriteBatch(graphics.GraphicsDevice); spriteBatch.Begin(); starwars.update(); for (int i = 0; i < starwars.width * starwars.height; i++) { Cell c = starwars.get(i); if (c == null) continue; Color cl = new Color[] { Color.Black, Color.Orange, Color.OrangeRed, Color.Red }[c.state]; put_tile(spriteBatch, c.x, c.y, cl); } spriteBatch.End(); base.Draw(gameTime); } } static class Program { static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }