1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include<stdio.h>
#include <time.h>
#include <cstdlib>
#include <math.h>
int randfun(int arrX[], int arrY[],int size);
void cenfun(int arrX[], int arrY[],int size);
void rotatefun(int arrX[], int arrY[],int size);
int distfun(int arrX[], int arrY[],int size);
int main()
{
const int size = 5;
int arrX[size];
int arrY[size];
randfun(arrX, arrY,size);
cenfun(arrX, arrY,size);
rotatefun(arrX, arrY,size);
double distance = distfun(arrX, arrY,size);
printf("total distance=");
printf("%lf\n", distance);
return 0;
}
int randfun(int arrX[], int arrY[],int size)
{
srand((unsigned)time(NULL));
int x, y;
bool xsign, ysign;
int arrX[5];
int arrY[5];
for (int i = 0; i < 5; i++)
{
x = rand() % 100;
y = rand() % 100;
xsign = rand() % 2;
ysign = rand() % 2;
if (xsign) x *= -1;
if (ysign) y *= -1;
arrX[i] = x;
arrY[i] = y;
}
for (int i = 0; i < 5; i++)
{
printf("%3d %3d\n", arrX[i], arrY[i]);
}
}
void cenfun(int arrX[], int arrY[],int size)
{
double cen_x, cen_y;
printf("centre of gravity is");
cen_x = (arrX[1] + arrX[2] + arrX[0] + arrX[3] + arrX[4]) / 5;
cen_y = (arrY[1] + arrY[2] + arrY[0] + arrY[3] + arrY[4]) / 5;
printf("%lf , %lf\n", cen_x, cen_y);
}
void rotatefun(int arrX[], int arrY[],int size)
{
for (int i = 0; i < 5; i++)
{
double rot_x, rot_y;
printf("rotate points are:");
rot_x = arrX[i] * cos(0.122) - arrY[i] * sin(0.122);
rot_y = arrX[i] * cos(0.122) + arrY[i] * sin(0.122);
printf("%lf %lf\n", rot_x, rot_y);
}
}
int distfun(int arrX[], int arrY[],int size)
{
double dist_0, dist_1, dist_2, dist_3, dist_4, dist_total, dist_00, dist_01, dist_02, dist_03, dist_04;
dist_0 = (arrX[0] * arrX[0] + arrY[0] * arrY[0]);
dist_1 = (arrX[1] * arrX[1] + arrY[1] * arrY[1]);
dist_2 = (arrX[2] * arrX[2] + arrY[2] * arrY[2]);
dist_3 = (arrX[3] * arrX[3] + arrY[3] * arrY[3]);
dist_4 = (arrX[4] * arrX[4] + arrY[4] * arrY[4]);
dist_00 = sqrt(dist_0);
dist_01 = sqrt(dist_1);
dist_02 = sqrt(dist_2);
dist_03 = sqrt(dist_3);
dist_04 = sqrt(dist_4);
dist_total = dist_00 + dist_01 + dist_02 + dist_03 + dist_04;
printf("total distance=");
printf("%lf\n", dist_total);
}
|