网站/小程序/APP个性化定制开发,二开,改版等服务,加扣:8582-36016

偶然间发现了一个图形感觉挺有意思的!写个Demo计算图形坐标!

阿基米德螺旋线 小Demo 简单 易懂 实现 vc++8.0

index 点的下标

step 步长相邻两个点的间隔(根据自己的画布选择,这里取60px)


x,y 原点的坐标

direction 方向(4个方向一个方向两种 共8种)

下左上 下右上

左上右 右上左

上右下 上左下

右下左 左下右


POINT FindNextPosByAHMDSpiral(int index, int step = 50, int x = 0, int y = 0,int direction = 0)//默认方向 下左上
{
    POINT nextPos,lastPos;
    if(index < 1)
    {
        //第一个点默认原点
        nextPos.x = x,nextPos.y = y;
    }
    else
    {
        lastPos.x = x,lastPos.y = y;//最后一个点的坐标
        int count = 1;//找第几个点
        while(count <= index)
        {
            if(count != 1)
            {
                lastPos.x = nextPos.x;
                lastPos.y = nextPos.y;
            }
            int sum = 1;
            int n = 0;
            while(count > sum)
            {
                sum += n + 1;
                n++;
            }
            int nextDir = n % 4;// 0:左下 1:左上 2:右上 3:右下
            if(direction < 4)//顺时针4种
            {
                nextDir += direction;
            }
            else if (direction < 8)//逆时针的4种
            {
                int d = direction - 4;
                nextDir = abs(nextDir - 3); //取相反值
                nextDir += d;
            }
            nextDir = nextDir >= 4 ? nextDir - 4 : nextDir;
            switch(nextDir)
            {
                case 0:
                    {
                        nextPos.x = lastPos.x - step / 2;
                        nextPos.y = lastPos.y + step / 2;
                        break;
                    }
                case 1:
                    {
                        nextPos.x = lastPos.x - step / 2;
                        nextPos.y = lastPos.y - step / 2;
                        break;
                    }
                case 2:
                    {
                        nextPos.x = lastPos.x + step / 2;
                        nextPos.y = lastPos.y - step / 2;
                        break;
                    }
                case 3:
                    {
                        nextPos.x = lastPos.x + step / 2;
                        nextPos.y = lastPos.y + step / 2;
                        break;
                    }
            }
            count++;
        }
    }
    return nextPos;
}


然后调用即可求出下个点的坐标。

POINT nextPos = FindNextPosByAHMDSpiral(index, 60, 512, 384, type);

下面是两张同一方向的两种图:

1562897470344730.png

1562897471901305.png


评论 0

暂无评论
0
0
0
立即
投稿
发表
评论
返回
顶部