Help us understand the problem. What is going on with this article?

NSAffineTransformのメモ

More than 5 years have passed since last update.

場所をずらしながら繰り返し描画を行おうとして、 NSAffineTransformで原点移動すればいいじゃないかと思い実装して失敗した時のメモ。

サンプルのスクリーンショットは以下のとおり。左のようなものを作ろうとして正しく実装できたのが真ん中、勘違いで正しく描画出来てなかったのが右側。

2014_04_27_11_59.png

コード : https://github.com/Nunocky/AffineTransformStudy001

正しい書き方

    NSAffineTransform *trans1 = [NSAffineTransform transform];
    [trans1 translateXBy: 10 yBy: 10];

    [trans1 concat];
    [self drawLineWithColor:[NSColor redColor]];

    [trans1 concat];
    [self drawLineWithColor:[NSColor greenColor]];

     // .....

間違った書き方

    NSAffineTransform *trans = [NSAffineTransform transform];

    [self drawLineWithColor:[NSColor blackColor]];

    [trans translateXBy: 10 yBy: 10];
    [trans concat];
    [self drawLineWithColor:[NSColor redColor]];

    [trans translateXBy: 10 yBy: 10];
    [trans concat];
    [self drawLineWithColor:[NSColor greenColor]];

     // .....

反省

分かってしまえばそりゃそうだろってことなんだけど

  • NSAffineTransformは変換行列を定義するもの
  • concatメソッドは現在のコンテキストに対して座標変換を適用するもの。感覚的には [[NSColor blackColor] set]に近い。
  • translateXBy:yBy: メソッドは 座標変換の行列を追加するもの。間違った例のようにすると、変換行列 = A * A * A*.... とどんどんずれ幅が大きくなっていく。
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away