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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int amm;
double * x;
double * y;
int * point;
//This is just the distance formula.
double distance (double _x1, double _x2, double _y1, double _y2)
{
double xx,yy;;
xx = pow( _x2 - _x1 , 2);
yy = pow( _y2 - _y1 , 2);
return (sqrt (xx+yy));
}
void generate_path();
long factorial();
double find_distance(int, int);
int closest(int);
int not_equal_to(int,int);
int main ()
{
cout << "Enter the number of points?\n";
//amm is ammount of points there will be
cin >> amm;
if (amm < 3)
{
return 0;
}
//the x and y arrays store all of the coordinates
x = new (nothrow) double [amm];
y = new (nothrow) double [amm];
if (x == 0 || y == 0)
{
cout << "Error has occured while initiazling x and y variables";
return 0;
}
//Here they enter all of the coordinates
for (int k=0;k<amm;k++)
{
cout << "\n>>Ordered pair for coordinate number "<<(k+1)<<": (0 to Quit.)\nX:";
cin >> x[k];
if (x[k] == 0)
{
break;
return 0;
}
cout << "\nY:";
cin >> y[k];
}
generate_path();
return 0;
}
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
void generate_path()
{
int paths;
//the paths variable stores the total number of paths possible
paths = factorial(amm);
point=new int[paths];
srand (time(NULL));
//Determine three random points to start from.
point[0]= rand() % amm;
point[1]= rand() % amm;
point[2]= rand() % amm;
cout << "\nChoosing initial points at random...\n";
//this next part makes sure that neither point[1] or point[2] is
//the same as point[0]
while
(point[1]=point[0])
{
point[1]=+ rand() % amm;
if (point[0]!=point[1]) break;
}
while
((point[2]=point[0])||(point[2]=point[1]))
{
point[2]=+ rand() % amm;
if ((point[2]!=point[1])&&(point[2]!=point[0])) break;
}
//Initial points chosen.
cout << "Starting points: (" << point[0] << ") (" <<
point[1] << ") (" << point[2] << ")\n";
cout << "\nInitial Points Chosen.\n\n";
//Now choose all of the other points. This is the part that is messing up.
//A for loop starts with point 3 and continues on, each time finding the
//closest point to the last point chosen (point [h-1]) and adds it to
//point[h]. But choosing the point always returns 0. Why??
for (int h=3;h<amm;h++)
{
int jj;
jj=closest(h-1);
point[h]=jj;
cout << "Point no. " << h+1 << " selected. Value: " << point[h] << ".\n";
}
cin >> amm;
}
double find_distance (int _A,int _B)
{
//This just finds the distance between point[__A] and point [__B].
double _D;
_D=distance (x[_A],x[_B],y[_A],y[_B]);
return _D;
}
int not_e(int _h, int value)
{
//This is the function that tests whether or not value
//has yet been repeated before in the series of points
//from point[0] to point [__h].
int result;
result=1;
cout << _h << "!!!";
for (int u=0;u<_h;u++)
{
cout << value;
cout << "/" << point[u] << " ";
if (value==point[u])
{
result=0;
}
}
return result;
}
int closest(int __a)
{
//The closest function was written by Helios for me, but I slightly
//modified it. It is designed to find the closest point to point[__a],
//that is not already one of points[0....__a] because I can't repeat a
//point in the series. But when looking for a point it always returns 0?
int find_closest_to=__a;
int closest=-1;
for (int i=0;i<amm;i++){
if ( not_e(__a,i) == 0 )
continue;
if (closest<0 || find_distance(closest,point[find_closest_to])>find_distance(i,point[find_closest_to]))
{closest=i; cout<<"AAA";}
}
if (closest<0)
{
cout << "Error occured in locating points.";
}
return closest;
}
|