Feb 28, 2014

いつも忘れるので。

public T DeepCopy(T target)
{
    object result = null;

    // シリアル化した情報を格納する stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        // 指定されたオブジェクトをシリアライズ
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = 
            new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        formatter.Serialize(stream, target);

        // デシリアライズ
        stream.Position = 0;
        result = formatter.Deserialize(stream);
    }

    return (T)result;
}

Feb 26, 2014

『"ファイル名"の保存中にエラーが検出されました。いくつかの機能を削除または修復することにより、ファイルを保存できる場合があります。新しいファイルで修復を実行するには、[継続]をクリックしてください。ファイルの保存を中止するには、[キャンセル]をクリックしてください』 継続しても保存できない。 レジストリの更新で復旧するとのこと。コマンドプロンプトで以下のコマンドをたたく。
regsvr32 c:\windows\System32\MSCOMCTL.OCX
regsvr32, 64 ビット版については以下を参照
注: 64 ビット版の Windows オペレーティング システムには、次の 2 つのバージョンの Regsv32.exe ファイルがあります。
  • 64 ビット版は %systemroot%\System32\regsvr32.exe です。
  • 32 ビット版は %systemroot%\SysWoW64\regsvr32.exe です。
Regsvr32 ツールを使用する方法および Regsvr32 のエラー メッセージをトラブルシューティングする方法
WordPress で個別ページの記事部分を ajax で取得して任意の場所に挿入するコードを書いたのでメモ。(※ jQuery の内容としては WordPress に特化したものではない)流れとしてはこんな感じ。
  • クリックイベントをバインド
  • ajax のリクエスト先 URL を初期化
  • ajax リクエストを行いページ内容を格納
  • ページ全体から目的の部分だけを取得して格納
  • 目的の部分をいったん非表示にしてから挿入
  • アニメーションで表示
コードはこんな感じ。
$(document).ready(function () {

 // クリックイベントをバインド
 $(".hoge").click(function(){
  var url, html, target;
  
  // ajax のリクエスト先 URL を初期化
  url = $(this).find("a.hoge").attr("href");
  
  // ajax リクエストを行いページ内容を格納
  html = $.ajax({
   url: url,
   async: false
  }).responseText;
  
  // ページ全体から目的の部分だけを取得して格納
  target = $(html).find(".fuga");
  
  // 目的の部分をいったん非表示にしてから挿入
  target.css({display: "none", opacity: "toggle"});
  $(this).after(target);
  
  // アニメーションで表示
  target.animate({height: "show", opacity: "toggle"});
 });
 
});
2013-02-27 追記: responseText からそのまま jQuery オブジェクトを生成するような書き方は IE だとダメみたい。(IE9 は動いてる)IE で同じことをやろうと思ったら上のサンプルだと全然だめ。手抜きしすぎ。 なんもないところから jQuery オブジェクトを生成したいときは、

var hoge = $("
");
じゃなくて、

var hoge = $(document.createElement("div"));

こう書く。
jQuery オブジェクトから親要素の jQuery オブジェクトを取得したい。これすぐ忘れる。 以下の例は、クラス "hoge" とマッチする要素の直近の親要素(a)に対して css をセットしている。
$(".hoge").closest("a").css("border", "none");
自身からもっとも近い親要素(a)の jQuery オブジェクトを格納する。
var target = $(this).closest("a");
親要素を選択したいだけなら parent で良い。closest は "もっとも近い親要素" であることがポイント。 parent([expr]) - jQuery 日本語リファレンス closest([expr]) - jQuery 日本語リファレンス
jQuery の toggle と既に表示されている要素(インデックス)の管理。
jQuery でインデックスをとってくる方法。
css からは指定できません。ってことで、html はこんな感じで。

canvas を指定してプロパティにセット。
$(document).ready(function(){
    document.getElementById('can').width = 400;
    document.getElementById('can').height = 350;
});​

Feb 25, 2014

Delaunay(ドロネー)分割の勉強。まずは基本的な描画(線、円)とドロネー分割で必要な外接円の求め方から。 以下、JavaScript だけ。

$(document).ready(function () {

    var SAMPLE = {};

    SAMPLE.Main = (function () {

        // 頂点
        function Vertex(x, y) {
            this.x = x;
            this.y = y;
        }

        // 三角形
        function Triangle(v0, v1, v2) {

            this.v0 = v0;
            this.v1 = v1;
            this.v2 = v2;

            // 三角形を描画
            this.draw = function () {

                var drawLine = function (vs, vd) {
                    // パスのリセット
                    ctx.beginPath();
                    // 線の太さ
                    ctx.lineWidth = 1;
                    // 線の色
                    ctx.strokeStyle = "#454545";
                    // 開始位置
                    ctx.moveTo(vs.x, vs.y);
                    // 次の位置
                    ctx.lineTo(vd.x, vd.y);
                    // 描画 
                    ctx.stroke();
                };

                drawLine(this.v0, this.v1);
                drawLine(this.v1, this.v2);
                drawLine(this.v2, this.v0);

            };

            // 外接円を描画
            this.drawCircle = function () {

                // 外接円の求め方
                var x1 = this.v0.x,
                    y1 = this.v0.y,
                    x2 = this.v1.x,
                    y2 = this.v1.y,
                    x3 = this.v2.x,
                    y3 = this.v2.y,
                    c = 2.0 * ((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)),
                    x = ((y3 - y1) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1) + (y1 - y2) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1)) / c,
                    y = ((x1 - x3) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1) + (x2 - x1) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1)) / c,
                    center = new Vertex(x, y),                    // 外接円の中心
                    dx = center.x - v0.x,
                    dy = center.y - v0.y,
                    radius = Math.sqrt((dx * dx) + (dy * dy)),    // 外接円の半径
                    circle = new Circle(center, radius);

                // 外接円を描画
                circle.draw();
                
                // 外接円の中心を描画
                circle.drawCenter();
            };
        }

        // 円
        function Circle(center, radius) {
            
            // 中心座標と半径  
            this.center = center;
            this.radius = radius;

            // 円を書く
            this.draw = function () {
                
                ctx.beginPath();
                ctx.arc(this.center.x, this.center.y, this.radius, 0, 360, true);
                ctx.stroke();
                
            };
            
            // 円の中心を書く
             this.drawCenter = function () {
                
                ctx.beginPath();
                ctx.arc(this.center.x, this.center.y, 2, 0, 360, true);
                ctx.fill();
                
            };

        }

        var canJqObj = $("#can"),
            canDom = document.getElementById('can'),
            ctx = document.getElementById('can').getContext("2d"),
            // 三角形の定義
            triangle = new Triangle(
            new Vertex(100, 120),
            new Vertex(220, 270),
            new Vertex(300, 160));

        // 開始
        function init() {

            // canvas のサイズを指定        
            canDom.width = 400;
            canDom.height = 350;

            // 三角形を描画
            triangle.draw();
            
            // 円を描画
            triangle.drawCircle();
        }

        // 開始
        init();

    })();


});

jQuery でフッタ要素を強引に最下部に固定してみる。定石がわからん。


$(document).ready(function () {

     // 基準の高さを格納
     var bodyBaseH = $('body').height();
     var windowH = 0;
    
     // リサイズ時の css 切り替え
     function ReSize() {

          winH = $(window).height();

          // ウィンドウの方が高い場合フッタを最下部に固定
          if (winH > bodyBaseH)
          {
               $('#footer').css("position", "fixed");
               $('#footer').css("bottom", "0");
          }
          else
          {
               $('#footer').css("position", "relative");
          }
     }
    
     // リサイズイベントにハンドラをバインド
     $(window).resize(function(){
          ReSize();
     });
    
     // 切り替え
     ReSize();
});