2014年1月21日火曜日

開発環境

Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の3章(型を定義し、関数を単純化する)、3.13(ガードの条件節の評価)、練習問題 9, 10.をDartで解いてみる。

その他参考書籍

練習問題9, 10.

コード

sample.dart

import 'dart:html';

void main(){
  for(InputElement input in [p1x, p1y, p2x, p2y, p3x, p3y]){
    input.onKeyUp.listen((KeyboardEvent event){
      pre.text = window.navigator.userAgent + '\n';
      try{
        var ax = double.parse(p1x.value);
        var ay = double.parse(p1y.value);
        var bx = double.parse(p2x.value);
        var by = double.parse(p2y.value);
        var cx = double.parse(p3x.value);
        var cy = double.parse(p3y.value);
        var a = new Point(ax, ay);
        var b = new Point(bx, by);
        var c = new Point(cx, cy);
        var d = new Direction(a, b, c);
        pre.text += '$d\n';
      } catch (e){
        pre.text += '$e\n';
      }
    });
  }
}

InputElement p1x = querySelector('#p1x');
InputElement p1y = querySelector('#p1y');
InputElement p2x = querySelector('#p2x');
InputElement p2y = querySelector('#p2y');
InputElement p3x = querySelector('#p3x');
InputElement p3y = querySelector('#p3y');
PreElement pre = querySelector('#pre0');

class Point{
  num x;
  num y;
  Point(this.x, this.y);
}

class Direction{
  var direction;
  Direction(Point a, Point b, Point c){
    var cow = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
    direction = cow < 0 ?
        'Clockwise' : cow > 0 ?
            'Counterclockwise' : 'Collinear';
  }
  String toString(){
    return direction;
  }
}
, ,















						

0 コメント:

コメントを投稿