samedi 14 janvier 2017

Rotating Random Points Around Their Center of Gravity

I am working in C, and I have generated a set of random x,y points. I have found the center of gravity for the points and translated all the points so that the center i found would be their origin. Now all I need to do to is rotate all the points 2.42 radians (and calculate the total distance traveled by all the points, but I think that should be fairly simple). Also I think I know the equation needed to do this, but I have no idea why my script is not working. Any help would be appreciated, Thanks!

//#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <stdarg.h>


int CenterG(int arrx[100], int arry[100], int m);
int Translate(int arrx[100], int arry[100],int  m, int resx, int resy);
int Rotate(int arrx2[100], int arry2[100], int m);


void main()
{
    //Just genereates the random points and appends them to a list.
    srand(time(NULL));
    int m;
    printf("\nHow many points do you want to generate?: ");
    scanf_s("%d", &m);


    int xmin = 0;
    int xmax = 101;

    int ymin = 0;
    int ymax = 101;

    int count = 0;


    int arrx[100];
    int arry[100];

    while (count < m)
    {
        int x = (rand() % xmax) + xmin;
        int y = (rand() % ymax) + ymin;
        printf("\nx: %d\ny:%d", x, y);
        printf("\n");



        arrx[count] = x;
        arry[count] = y;

        count++;
    }
    printf("\nX List: ");
    for (int i = 0; i < m; i++) {
        printf("%d, ", arrx[i]);
    }
    printf("\nY List: ");
    for (int i = 0; i < m; i++) {
        printf("%d, ", arry[i]);
    }

    //Passes the length of the lists and the actual 
    //lists to the next function
    CenterG(arrx, arry,m);
}


int CenterG(int arrx[100], int arry[100],int m){    
    //Finds the center of gravity

    printf("\n\n\nCenter of Gravity: \n\n");
    int resx = arrx[0];
    int resy = arry[0];

    for (int i = 1; i < m; i++) { //sums the x list 
        resx = resx + arrx[i];
    }
    for (int i = 1; i < m; i++) { //sums the y list
        resy = resy + arry[i];
    }
    resx = resx / m;//finds the averages
    resy = resy / m;
    printf("\nX Average: %d\n", resx);
    printf("\nY Average: %d\n", resy);

    Translate(arrx,arry,m,resx,resy);//Passes lists and center of G to the next function
    return(0);
}

int Translate(int arrx[100], int arry[100], int  m, int resx, int resy) {
    //Translates the new orgin to the center of gravity point that we found earlier
    printf("\nTranslate:\n\n");
    int arrx2[100];
    int arry2[100];
    //Adds the translated points to a new list. arrx2 and arry2
    for (int i = 0; i < m; i++) {
        arrx2[i] = arrx[i] - resx;
        }
    for (int i = 0; i < m; i++) {
        arry2[i] = arry[i] - resy;
    }

    printf("\n\nList X Trans:\n");
    for (int i = 0; i < m; i++) {
        printf("%d, ",arrx2[i]);
    }
    printf("\n\nList Y Trans:\n");
    for (int i = 0; i < m; i++) {
        printf("%d, ",arry2[i]);
    }
    Rotate(arrx2, arry2, m);
    return(0); 

}


int Rotate(int arrx2[100], int arry2[100], int m) {
    printf("\n\n\nRotate:\n\n");
    double arrx3[100];
    double arry3[100];
    double r = 2.42;
    double b;

    for (int i = 0; i < m; i++) {//changes the int variable type to double type
        b = arrx2[i];
        arrx3[i] = b;
        b = arry2[i];
        arry3[i] = b;
    }


    for (int i = 0; i > m; i++){ //Should rotate them... here is the problem
        arrx3[i] = arrx3[i] * cos(r) - arry3[i] * sin(r);
        arry3[i] = arrx3[i] * sin(r) + arry3[i] * cos(r);
    }
    //Should print the newly rotated points.
    printf("LIST ONE:\n");
    for (int i = 0; i < m; i++) {
        printf("%lf, ", arrx3[i]);
    }
    printf("\n\n");
    for (int i = 0; i < m; i++) {
        printf("%lf, ", arry3[i]);
    }
    return(0);

}




Aucun commentaire:

Enregistrer un commentaire