大久保弘崇
7章メモ4
最終更新:
hirotakaohkubo
-
view
§7.9
hybridを動かすためにはビットマップが必要。
これの扱いを確定させるために§7.10を先行する。
これの扱いを確定させるために§7.10を先行する。
境界条件を厳密化するために、以下の定義を変更する。
inBounds :: Int -> Int -> Region
inBounds w h (x,y) = 0 <= x && x < fromIntegral (w-1) &&
0 <= y && y < fromIntegral (h-1)
さて、ファイルにある画像データを読み込んでPanもどきで扱えるようにするために、
本格的なライブラリを起用するのは面倒なので、やはり適当に済ませる。
具体的には、IrfanViewの書きだすASCII PPMに形式を限定(コメントの扱いも手抜きしたため)した
次のファイル読み込み関数をでっち上げた。
本格的なライブラリを起用するのは面倒なので、やはり適当に済ませる。
具体的には、IrfanViewの書きだすASCII PPMに形式を限定(コメントの扱いも手抜きしたため)した
次のファイル読み込み関数をでっち上げた。
readPPM3 :: String -> IO (Array2 Colour)
readPPM3 fn = do
fh <- openFile fn ReadMode
hGetLine fh -- "P3"
hGetLine fh -- #comment
sizes <- hGetLine fh
let [w, h] = str2nums sizes
hGetLine fh -- "255"
datas <- hGetContents fh
let vals = str2nums datas
return $ Array2 w h (\(x,y) -> let r:g:b:_ = drop ((y*w+x)*3) vals in (fromIntegral b/255,fromIntegral g/255,fromIntegral r/255,1))
str2nums :: String -> [Int]
str2nums [] = []
str2nums (x:xs) | '0'<=x && x <= '9' = str2nums1 [x] xs
| otherwise = str2nums xs
str2nums1 ds [] = [read (reverse ds)]
str2nums1 ds (x:xs) | '0'<=x && x<='9' = str2nums1 (x:ds) xs
| otherwise = read (reverse ds) : str2nums xs
これらを用いて、hybrid関数を引数にとり、IOアクションを返すような関数として実行するべき作業を与えると
それを行う実行関数と、これに当てはめて図7.25~27を出力する関数は次のようになった。
それを行う実行関数と、これに当てはめて図7.25~27を出力する関数は次のようになった。
exec action = do
beckyA <- readPPM3 "beckyA.ppm"
let becky = reconstruct beckyA
fraidyA <- readPPM3 "fraidyA.ppm"
let fraidy = reconstruct fraidyA
let hybrid f t = cond (f t) fraidy becky
action hybrid
fig725 hybrid = do
let w = 80
writeppma (hybrid turningXPos) "fig725" 1 (-w,-w) (w,w) pi 8
fig726 hybrid = do
let w = 80
writeppma (hybrid swirlingXPos) "fig726" 1 (-w,-w) (w,w) pi 8
fig727 hybrid = do
let w = 80
writeppma (hybrid roamingDisk) "fig727" 1 (-w,-w) (w,w) pi 8
GHCiではさすがに動作が遅いが、何とか動く。
図7.25は exec fig725 を評価することでフレームが出力される。しかし回転方向がサンプルと逆になってしまった。
ちなみに、女性は "becky girl" 猫は "fraidy cat" でGoogle画像検索してヒットしたものを素材としている。著作権侵害であるが、学術目的として容赦願いたい。
図7.25は exec fig725 を評価することでフレームが出力される。しかし回転方向がサンプルと逆になってしまった。
ちなみに、女性は "becky girl" 猫は "fraidy cat" でGoogle画像検索してヒットしたものを素材としている。著作権侵害であるが、学術目的として容赦願いたい。

図7.26

図7.27 ただしuscaleの引数を40に増やした。
