0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Texture2Dにドット単位で線を引く

Posted at

前回のClassLineを応用して
ブレゼンハムのアルゴリズムをUnity用に改変したものです。
全ての線を引いたあと、

Texture2D.Apply();

でテクスチャに反映されます

    private Color DrawColor = Color.white;
    private void DrawLine(Texture2D tex, ClassLine line)
    {
        Vector2 v = line.v;
        int dx = (int)Mathf.Abs(v.x);
        int dy = (int)Mathf.Abs(v.y);
        int sx = (int)Mathf.Sign(v.x);
        int sy = (int)Mathf.Sign(v.y);

        int px = (int)line.p1.x;
        int py = (int)line.p1.y;

        if (dx > dy)
        {
            int err = -dx;
            for (int i = 0; i < dx; i++)
            {
                tex.SetPixel(px, py, DrawColor);
                px += sx;
                err += 2 * dy;
                if (err >= 0)
                {
                    py += sy;
                    err = -2 * dx;
                }
            }
        }
        else
        {
            int err = -dy;
            for (int i = 0; i < dy; i++)
            {
                tex.SetPixel(px, py, DrawColor);
                py += sy;
                err += 2 * dx;
                if (err >= 0)
                {
                    px += sx;
                    err = -2 * dy;
                }
            }
        }
    }

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?