• 0

help me in C++ code


Question

Need some help on figuring out how to get the minDist variable to be used to give feedback on the distance from the first sphere at every check through the loop as well as at the final output.

Any ideas?

#include ..

#include ..

using namespace std;

int main(){

double s1x;

double s1y;

double s1z;

double s1rad;

double s2x;

double s2y;

double s2z;

double s2rad;

double dir;

double dx;

double dy;

double dz;

float count;

double distance;

double minDist;

cout << "sphere 1 starts at (100,50,75) and the radius is 10 ..n";

cout << "input location for sphere 2 below a range of 100 and enter the radius ..n" << "x? ";

cin >> s2x;

cout << "y? ..n";

cin >> s2y;

cout << "z? ..n";

cin >> s2z;

cout << "radius? ..n";

cin >> s2rad;

cout << " ..n";

s1x = 100;

s1y = 50;

s1z = 75;

count = 0;

dx = 1.

09287;

dy = 0.

34751;

dz = 0.

6;

distance = sqrt(pow((s1x-s2x),2) + pow((s1y-s2y),2) + pow((s1z-s2z),2));

while ((count < 101) & (distance > s1rad + s2rad)){

count = count + 1;

s2x = s2x + dx;

s2y = s2y + dy;

s2z = s2z + dz;

cout << count << " : x " << s2x << " : y " << s2y << " : z " << s2z << endl;

if (count == 101){

cout << "They will never collide.

";

break;

}

if (distance == s1rad + s2rad){

cout << count << "it made contact at " << " : x " << s2x << " : y " << s2y << " : z " << s2z << endl;

break;

}

}

return 0;

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

#include &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;

int main(){
double s1x,s1y,s1z,s1rad,s2x,s2y,s2z,s2rad,dir,dx,dy,dz,distance,minDist;
float count;

cout &lt;&lt; "sphere 1 starts at (100,50,75) and the radius is 10 ..n";
cout &lt;&lt; "input location for sphere 2 below a range of 100 and enter the radius ..n" &lt;&lt; "x? ";
cin &gt;&gt; s2x;
cout &lt;&lt; "y? ..n";
cin &gt;&gt; s2y;
cout &lt;&lt; "z? ..n";
cin &gt;&gt; s2z;
cout &lt;&lt; "radius? ..n";
cin &gt;&gt; s2rad;
cout &lt;&lt; " ..n";

s1x = 100;
s1y = 50;
s1z = 75;
count = 0;
dx = 1.09287;
dy = 0.34751;
dz = 0.6;
distance = sqrt(pow((s1x-s2x),2) + pow((s1y-s2y),2) + pow((s1z-s2z),2));

while ((count &lt; 101) &amp; (distance &gt; s1rad + s2rad)){
	count = count + 1;
	s2x = s2x + dx;
	s2y = s2y + dy;
	s2z = s2z + dz;
	cout &lt;&lt; count &lt;&lt; " : x " &lt;&lt; s2x &lt;&lt; " : y " &lt;&lt; s2y &lt;&lt; " : z " &lt;&lt; s2z &lt;&lt; endl;

	if (count == 101){
		cout &lt;&lt; "They will never collide.
		";
	break;
	}

	if (distance == s1rad + s2rad){
		cout &lt;&lt; count &lt;&lt; "it made contact at: x " &lt;&lt; s2x &lt;&lt; " : y " &lt;&lt; s2y &lt;&lt; " : z " &lt;&lt; s2z 
		&lt;&lt; endl;
	break;
	}	
  }
	return 0;
}

now that it's a little more readable....can you explain what you need minDist to do programatically rather than just saying "how to get the minDist variable to be used to give feedback on the distance from the first sphere at every check through the loop as well as at the final output."... this gives us no clue where to start.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.