samedi 11 janvier 2020

plotting points along square spiral pattern

I am trying to find coordinates of points along square spiral on based this post: https://www.geeksforgeeks.org/program-to-print-spiral-pattern/. I created matrices of x and y coordinates of points, but it doesn't work - only the first row of each matrices have proper coordinates.In switch case instruction I decided to update x or y coordinate of points based on move variable. If move = r it is being overwrited by 'd'and as the result the y coordinate of point will by tempY - distance. Thank you in advance for help! This is the code in C++:

#include <iostream>
using namespace std;

void printSpiral(int size)
{

    // Create row and col
    // to traverse rows and columns
    int row = 0, col = 0;
    int distance = 5 ; // how far move in horizontal or vertical direction 
    int startX = -10 ; // x-coordinate of starting point
    int startY = 10;  // y-coordinate of starting point
    int tempX =  startX; 
    int tempY =  startY;
    int pointX[size][size]= {0};
    int pointY[size][size]= {0};
    pointX[0][0] = startX;//matrix of x-coordinates 
    pointY[0][0] = startY;//matrix of y-coordinates

    int boundary = size - 1;
    int sizeLeft = size - 1;
    int flag = 1;

    // Variable to determine the movement
    // r = right, l = left, d = down, u = upper
    char move = 'r';

    // Array for matrix
    int matrix[size][size] = {0};

    for (int i = 1; i < size * size + 1; i++)
    {

        // Assign the value
        //matrix[row][col] = i;

        // switch-case to determine the next index
        switch (move)
        {

            // If right, go right
            case 'r':
                col += 1;
                pointX[row][col] = tempX + distance;
                break;

            // if left, go left
            case 'l':
                col -= 1;
                pointX[row][col] = tempX - distance;
                break;

            // if up, go up
            case 'u':
                row -= 1;
                pointY[row][col] = tempY + distance;
                break;

            // if down, go down
            case 'd':
                row += 1;
                pointY[row][col] = tempY - distance;
                break;
        }

        // Check if the matrix
        // has reached array boundary
        if (i == boundary)
        {

            // Add the left size for the next boundary
            boundary += sizeLeft;

            // If 2 rotations has been made,
            // decrease the size left by 1
            if (flag != 2)
            {

                flag = 2;
            }
            else
            {

                flag = 1;
                sizeLeft -= 1;
            }

            // switch-case to rotate the movement
            switch (move)
            {

                // if right, rotate to down
                case 'r':
                    move = 'd';
                    break;

                // if down, rotate to left
                case 'd':
                    move = 'l';
                    break;

                // if left, rotate to up
                case 'l':
                    move = 'u';
                    break;

                // if up, rotate to right
                case 'u':
                    move = 'r';
                    break;
            }
        }
        tempX = pointX[row][col];
        tempY = pointY[row][col];
    }

    // Print the pointX matrix 

    cout << endl << "PointX matrix" << endl << endl;
    for (row = 0; row < size; row++)
    {
        for (col = 0; col < size; col++)
        {


            cout << pointX[row][col] << " " ;

        }

        cout << endl;
    }

    cout << endl << endl;

      // Print the pointY matrix
      cout << endl << "PointY matrix" << endl << endl;
    for (row = 0; row < size; row++)
    {
        for (col = 0; col < size; col++)
        {

          cout << pointY[row][col] << " " ;

        }

        cout << endl;
    }
}

int main()
{
    int size = 5 ;
    printSpiral(size);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire