Reputation: 39
I'm writing a program that will change the coordinates of a triangle (one coordinate at a time) in order to create a new triangle and then measure that new perimeter (>100 triangles total).
I have the perimeter function below, but I'm confused as to how I can change only one point of the triangle at a time while still changing each of the three points at some point in time (right now I'm only changing ptA 100 times, but I would rather change ptA 35 times, ptB 35 times, etc). Also, when I run the program as below, why does it only give 5 perimeters instead of 110?
double find3Ddistance (double pt0[], double pt1[]);
double find3Dperimeter (double pt0[], double pt1[], double pt2[]);
int main(){
double ptA[3] = {0,0,0}; //values for testing perimeter function
double ptB[3] = {6,0,0};
double ptC[3] = {0,8,0};
for (i = 0; i < 110; ++i) {
find3Dperimeter(ptA, ptB, ptC);
++ptA[i];
}
return 0;
}
double find3Ddistance (double pt0[], double pt1[]) {
double x = pt0[0] - pt1[0];
double y = pt0[1] - pt1[1];
double z = pt0[2] - pt1[2];
double dist;
dist = pow(x,2) + pow(y,2) + pow(z,2);
dist = sqrt(dist);
return dist;
}
double find3Dperimeter (double pt0[], double pt1[], double pt2[]) {
double one = find3Ddistance(pt0, pt1);
double two = find3Ddistance(pt1, pt2);
double three = find3Ddistance(pt2, pt0);
double perimeter = one + two + three;
printf("Perimeter: %lf", perimeter);
return perimeter;
}
Upvotes: 1
Views: 72
Reputation: 348
You modify ptA[i] for i between 0 and 109. That's incorrect, you should change only ptA[0], ptA[1] or ptA[2]. For example, you could change this part:
for (i = 0; i < 110; ++i) {
find3Dperimeter(ptA, ptB, ptC);
++ptA[i];
}
to something like that:
for (i = 0; i < 35; ++i) {
find3Dperimeter(ptA, ptB, ptC);
++ptA[0];
++ptB[1];
++ptC[2];
find3Dperimeter(ptA, ptB, ptC);
++ptA[1];
++ptB[2];
++ptC[0];
find3Dperimeter(ptA, ptB, ptC);
++ptA[2];
++ptB[0];
++ptC[1];
}
This is just an example, but you don't have to call find3Dperimeter only once per loop.
Upvotes: 1
Reputation: 2105
Try this code:
double find3Ddistance (double pt0[], double pt1[]);
double find3Dperimeter (double pt0[], double pt1[], double pt2[]);
int main(){
double ptA[3] = {0,0,0}; //values for testing perimeter function
double ptB[3] = {6,0,0};
double ptC[3] = {0,8,0};
for (int i = 0; i < 110; ++i) {
int j = i%3;
find3Dperimeter(ptA, ptB, ptC);
if(j==0) ++ptA[j];
else if (j==1) ++ptB[j];
else ++ptC[j];
}
return 0;
}
double find3Ddistance (double pt0[], double pt1[]) {
/*is the same*/
}
double find3Dperimeter (double pt0[], double pt1[], double pt2[]) {
/*is the same */
}
This way you can iterate 110 times changing only 35 times every ptX.
The module operator should help you changing one array per time . It makes j
to cycle the values 0,1,2
that are used as indexes of the three points.
Upvotes: 1
Reputation: 223689
Here's your problem:
++ptA[i];
The array ptA
only has 3 elements, but i
ranges from 0 to 109, so you're referencing the array out of bounds. This leads to undefined behavior.
Assuming you want to increase one of the three dimensions, you would do this:
for (i = 0; i < 110; ++i) {
find3Dperimeter(ptA, ptB, ptC);
++ptA[0];
}
Or for all three:
for (i = 0; i < 110; ++i) {
find3Dperimeter(ptA, ptB, ptC);
++ptA[0];
++ptA[1];
++ptA[2];
}
If you want to do each point 35 times, just use 3 separate loops:
for (i = 0; i < 35; ++i) {
find3Dperimeter(ptA, ptB, ptC);
++ptA[0];
++ptA[1];
++ptA[2];
}
for (i = 0; i < 35; ++i) {
find3Dperimeter(ptA, ptB, ptC);
++ptB[0];
++ptB[1];
++ptB[2];
}
for (i = 0; i < 35; ++i) {
find3Dperimeter(ptA, ptB, ptC);
++ptC[0];
++ptC[1];
++ptC[2];
}
Upvotes: 2