開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)の11章(ビット演算)、11.8(ビットマップグラフィックス)、11.10(プログラミング実習)、実習11-1をDartで解いてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
実習 11-1.
コード
sample.dart
import 'dart:html';
final int x_size = 40;
final int y_size = 60;
List<List<int>> graphics = [];
var result;
void main(){
var run = querySelector('#run'),
clear = querySelector('#clear'),
pre0 = querySelector('#pre0');
for(var i = 0; i < x_size ~/ 8; i += 1){
graphics.add([]);
for (var j = 0; j < y_size; j += 1){
graphics[i].add(0);
}
}
run.onClick.listen((MouseEvent event){
result = window.navigator.userAgent + '\n';;
int loc;
for (loc = 0; loc < x_size; loc += 1){
setBit(loc, loc);
}
printGraphics();
result += '\n';
clearBit(19, 19);
clearBit(0, 5);
printGraphics();
result += '\n';
result += '0 × 0: ${testBit(0, 0)}\n';
result += '0 × 5: ${testBit(0, 5)}\n';
result += '19 × 19: ${testBit(19, 19)}\n';
result += '20 × 20: ${testBit(20, 20)}\n';
pre0.text = result;
});
clear.onClick.listen((MouseEvent event) => pre0.text = '');
}
void setBit (int x, int y){
graphics[x ~/ 8][y] |= (0x80 >> (x % 8));
}
void clearBit(int x, int y){
var n = 0x80 >> (x % 8);
var bit = n == 0 ? 1 : (~n).toUnsigned(n.bitLength);
graphics[x ~/ 8][y] &= bit;
}
bool testBit(x, y){
return graphics[x ~/ 8][y] & (0x80 >> (x % 8)) > 0;
}
void printGraphics(){
int x;
int y;
int bit;
for (y = 0; y < y_size; y += 1){
for (x = 0; x < x_size / 8; x += 1){
for (bit = 0x80; bit > 0; bit = (bit >> 1)){
if ((graphics[x][y] & bit) != 0){
result += 'X';
} else {
result += '.';
}
}
}
result += '\n';
}
}
0 コメント:
コメントを投稿