Neep help for an assignment please

Hey I need some help please.

I have this code, it works somewhat fine, when i compile in the terminal (mac), then it is supposed to print out 5 different radiances, the first and second are different, but the third, fourth and fifth is the same. I need to print out five different once. By the way, im a beginner and im using allosystem to compile my code. the code is below

#include <iostream>
#include <math.h>

class CirclePath{
public:

float x,y;


CirclePath(){
x = 0;
y = 0;
mAngle = 0;
mAngleInc = 0;
mRadius = 0;

}
void init(float startAngle, float angleInc, float radius){
mAngle = startAngle;
mAngleInc = angleInc;
mRadius = radius;
}

void generateNextPath(){

x = mRadius * cos(mAngle);
y = mRadius * sin(mAngle);
mAngle = mAngleInc;
}


private:
float mAngle;
float mAngleInc;
float mRadius;

};


int main(){

CirclePath path;

//Initialize parameters from our path
path.init(0,2*M_PI / 5, 1.);

for(int i=0;i<5;++i){
path.generateNextPath();
std::cout << path.x <<path.y << std::endl;
}
}
You always set mAngle to mAngleInc. I do not see any sense in this action

void generateNextPath(){

x = mRadius * cos(mAngle);
y = mRadius * sin(mAngle);
mAngle = mAngleInc;
}

Maybe you want to increase the value of mAngle. In this case you can write

mAngle += mAngleInc;
thanks, I get these values when I compile, I need 5 different!


10
0.3090170.951057
-0.8090170.587785
-0.809017-0.587785
0.309017-0.951056
Your question was a little bit ambiguous. If you are saying that 3, 4 and 5 were all the same as 2, then I think I see the problem. If you are saying that 3, 4 and 5 are all the same but distinct from 2 (and 1), then there is more to this than I am seeing right now.

Your problem is that you are always overwriting mAngle rather than incrementing it. So the first time through it is 0 at start and then gets set to 2/5 PI. The second time through it starts out as 2/5 PI and gets reassigned to the same value. So loops 2 - 5 all use 2/5 PI.

You probably meant to assign mAngle += mAngleInc;

By the way, please post code inside code tags. Just click the "<>" button on the Format: options and post your code between the tags. This will make it easier to read your code and refer to it in our responses.
Can you explain me why the numbers are like this??,

I really appreciate your help!
Hey again i did this <mAngle += mAngleInc;> got these numbers again

10
0.3090170.951057
-0.8090170.587785
-0.809017-0.587785
0.309017-0.951056

Can you show in the code how to do what you decribe underneath.

"Your problem is that you are always overwriting mAngle rather than incrementing it. So the first time through it is 0 at start and then gets set to 2/5 PI. The second time through it starts out as 2/5 PI and gets reassigned to the same value. So loops 2 - 5 all use 2/5 PI."

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
x#include <iostream>
#include <math.h>

class CirclePath{
public:

	float x,y;

	
	CirclePath(){
		x = 0;
		y = 0;
		mAngle = 0;
		mAngleInc = 0;
		mRadius = 0;
		
}
	void init(float startAngle, float angleInc, float radius){
		mAngle = startAngle;
		mAngleInc = angleInc;
		mRadius = radius;
	}	
	
	void generateNextPath(){
		
		x = mRadius * cos(mAngle);
		y = mRadius * sin(mAngle);
		mAngle += mAngleInc;
	}

	
private:
	float mAngle;	
	float mAngleInc;
	float mRadius;

};


int main(){
	
	CirclePath path;
	
	//Initialize parameters from our path
	path.init(0,2*M_PI / 5, 1.);
	
	for(int i=0;i<5;++i){
	path.generateNextPath();
	std::cout << path.x <<path.y << std::endl;
	}
}
Last edited on
now the code should look fine
Where is the definition of M_PI?
I run this code and do not see any problem.

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
#include <iostream>
#include <cmath>



class CirclePath
{
public:
 
	double x, y;
 
	CirclePath() :x( 0 ), y( 0 ), mAngle( 0 ), mAngleInc( 0 ), mRadius( 0 )
	{
	}

	void init( double startAngle, double angleInc, double radius )
	{
		mAngle = startAngle;
		mAngleInc = angleInc;
		mRadius = radius;
	} 

	void generateNextPath()
	{
		x = mRadius * cos( mAngle );
		y = mRadius * sin( mAngle );
		mAngle += mAngleInc;
	}
 
private:
	double mAngle; 
	double mAngleInc;
	double mRadius;
};
 

int main()
{
	CirclePath path;

 //Initialize parameters from our path
	path.init( 0, 2 * 3.14 / 5, 1.0 );

	for ( int i = 0; i < 5; ++i )
	{
		path.generateNextPath();
		std::cout << path.x << '\t' << path.y << std::endl;
	}

	return 0;
}
Topic archived. No new replies allowed.