redsquid
@redsquid (いとのり)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

座標変換についての質問

Q&A

Closed

解決したいこと

趣味で、とあるゲームの自動操作を行っていますが座標変換で悩んでいます。
下記のコードで、before_xyをafter_xyに変換したいです。
コードだとわかりにくいと思うのでイメージを添付します。
一つの菱形の下段が入力xy(ピクセル座標:左上から右上がx、左上から左下がy)で上段がゲームの座標です。下段の座標を上段の座標に変換したいです
例:左上菱形の場合、入力が(-416, -208)なら(-4, 0)に変換。尚、一つの菱形の大きさは、幅が208、高さが104です。
xy一時変換.png

できればpythonがうれしいです。
よろしくお願いいたします。

該当するソースコード

before_xy = [       # after_xy = [
    [-416, -208],   # ->  [-4, 0],
    [-416, -104],   # ->  [-3, 1],
    [-416, 0],      # ->  [-2, 2],
    [-416, 104],    # ->  [-1, 3],
    [-416, 208],    # ->  [0, 4],
    [-312, -156],   # ->  [-3, 0],
    [-312, -52],    # ->  [-2, 1],
    [-312, 52],     # ->  [-1, 2],
    [-312, 156],    # ->  [0, 3],
    [-208, -208],   # ->  [-3, -1],
    [-208, -104],   # ->  [-2, 0],
    [-208, 0],      # ->  [-1, 1],
    [-208, 104],    # ->  [0, 2],
    [-208, 208],    # ->  [1, 3],
    [-104, -156],   # ->  [-2, -1],
    [-104, -52],    # ->  [-1, 0],
    [-104, 52],     # ->  [0, 1],
    [-104, 156],    # ->  [1, 2],
    [0, -208],      # ->  [-2, -2],
    [0, -104],      # ->  [-1, -1],
    [0, 0],         # ->  [0, 0],
    [0, 104],       # ->  [1, 1],
    [0, 208],       # ->  [2, 2],
    [104, -156],    # ->  [-1, -2],
    [104, -52],     # ->  [0, -1],
    [104, 52],      # ->  [1, 0],
    [104, 156],     # ->  [2, 1],
    [208, -208],    # ->  [-1, -3],
    [208, -104],    # ->  [0, -2],
    [208, 0],       # ->  [1, -1],
    [208, 104],     # ->  [2, 0],
    [208, 208],     # ->  [3, 1],
    [312, -156],    # ->  [0, -3],
    [312, -52],     # ->  [1, -2],
    [312, 52],      # ->  [2, -1],
    [312, 156],     # ->  [3, 0],
    [416, -208],    # ->  [0, -4],
    [416, -104],    # ->  [1, -3],
    [416, 0],       # ->  [2, -2],
    [416, 104],     # ->  [3, -1],
    [416, 208],     # ->  [4, 0],
]                   # ]

自分で試したこと

一次変換でできるかなと思い、いろいろ試していますが、なかなかうまくいきません。
ご教示頂ければ幸いです。

0

1Answer

こんな感じでしょうか?
斜交座標系の座標変換ですね。

befores = [
        [-416, -208],
        [-416, -104],
        [-416, 0],
        [-416, 104],
        [-416, 208],
        [-312, -156],
        [-312, -52],
        [-312, 52],
        [-312, 156],
        [-208, -208],
        [-208, -104],
        [-208, 0],
        [-208, 104],
        [-208, 208],
        [-104, -156],
        [-104, -52],
        [-104, 52],
        [-104, 156],
        [0, -208],
        [0, -104],
        [0, 0],
        [0, 104],
        [0, 208],
        [104, -156],
        [104, -52],
        [104, 52],
        [104, 156],
        [208, -208],
        [208, -104],
        [208, 0],
        [208, 104],
        [208, 208],
        [312, -156],
        [312, -52],
        [312, 52],
        [312, 156],
        [416, -208],
        [416, -104],
        [416, 0],
        [416, 104],
        [416, 208],
]

answers = [
        [-4, 0],
        [-3, 1],
        [-2, 2],
        [-1, 3],
        [0, 4],
        [-3, 0],
        [-2, 1],
        [-1, 2],
        [0, 3],
        [-3, -1],
        [-2, 0],
        [-1, 1],
        [0, 2],
        [1, 3],
        [-2, -1],
        [-1, 0],
        [0, 1],
        [1, 2],
        [-2, -2],
        [-1, -1],
        [0, 0],
        [1, 1],
        [2, 2],
        [-1, -2],
        [0, -1],
        [1, 0],
        [2, 1],
        [-1, -3],
        [0, -2],
        [1, -1],
        [2, 0],
        [3, 1],
        [0, -3],
        [1, -2],
        [2, -1],
        [3, 0],
        [0, -4],
        [1, -3],
        [2, -2],
        [3, -1],
        [4, 0],
]

def transform(bef):
    aft = [
            1. / 208. * (+1. * bef[0] + 2. * bef[1]),
            1. / 208. * (-1. * bef[0] + 2. * bef[1]),
    ]
    return aft

for answer, before in zip(answers, befores):
    after = transform(before)
    print(answer[0] - after[0], answer[1] - after[1])
0Like

Comments

  1. @redsquid

    Questioner

    回答ありがとうございました。解決しました。深謝いたします。

Your answer might help someone💌